View Javadoc

1   package net.trajano.twiff.renderer;
2   
3   import java.util.Collections;
4   import java.util.LinkedHashMap;
5   import java.util.LinkedList;
6   import java.util.List;
7   import java.util.Map;
8   
9   /***
10   * This is a simple element that has no components.
11   * 
12   * @author Archimedes Trajano
13   */
14  public class SimpleElement implements CompositePageElement {
15      /***
16       * Attributes used in the element. It is recommended that a
17       * {@link LinkedHashMap} is used to ensure order.
18       */
19      private final Map<String, String> attributes;
20  
21      /***
22       * Element name. Basically the tag.
23       */
24      private final String elementName;
25  
26      /***
27       * List of sub-elements in this component. It it is recommended that a
28       * {@link LinkedList} is used as it would only be read in order.
29       */
30      private final List<PageElement> subElements = new LinkedList<PageElement>();
31  
32      /***
33       * 
34       */
35      private int columnNumber;
36  
37      /***
38       * 
39       */
40      private int lineNumber;
41  
42      /***
43       * This is a simplified constructor used for testing.
44       * 
45       * @param elementName
46       */
47      public SimpleElement(final String elementName) {
48          this.elementName = new String(elementName);
49          this.attributes = Collections.EMPTY_MAP;
50      }
51  
52      /***
53       * Constructs the element.
54       * 
55       * @param elementName
56       *                   element name.
57       * @param attributes
58       *                   attributes in the element.
59       */
60      public SimpleElement(final String elementName, final Map<String, String> attributes) {
61          this.elementName = elementName;
62          this.attributes = attributes;
63      }
64  
65      /***
66       * Accepts a visitor that should visit the page data.
67       * 
68       * @param visitor
69       *                   visitor.
70       */
71      public void accept(final PageElementVisitor visitor) {
72          if (subElements.isEmpty()) {
73              visitor.visitSimpleEmptyElement(this, elementName, attributes);
74          } else {
75              visitor.startSimpleElement(this, elementName, attributes, subElements);
76              for (PageElement element : subElements) {
77                  element.accept(visitor);
78              }
79              visitor.endSimpleElement(this, elementName, attributes, subElements);
80          }
81      }
82  
83      /***
84       * @param element
85       */
86      public void addElement(final PageElement element) {
87          subElements.add(element);
88      }
89  
90      /***
91       * Returns the attributes.
92       * 
93       * @return attributes.
94       */
95      public Map<String, String> getAttributes() {
96          return Collections.unmodifiableMap(attributes);
97      }
98  
99      /***
100      * Returns the sub-elements in the element.
101      * 
102      * @return sub-elements.
103      */
104     public List<PageElement> getElements() {
105         return Collections.unmodifiableList(subElements);
106     }
107 
108     /***
109      * @see net.trajano.twiff.renderer.PageElement#getColumnNumber()
110      */
111     public int getColumnNumber() {
112         return columnNumber;
113     }
114 
115     /***
116      * @see net.trajano.twiff.renderer.PageElement#getLineNumber()
117      */
118     public int getLineNumber() {
119         return lineNumber;
120     }
121 }