KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > web > html > Impl > HTMLParser > LabelBuilderVisitor


1 package org.jahia.clipbuilder.html.web.html.Impl.HTMLParser;
2
3 import org.htmlparser.visitors.NodeVisitor;
4 import org.htmlparser.*;
5 import org.htmlparser.nodes.TextNode;
6 import org.htmlparser.util.NodeList;
7 import javax.swing.text.html.HTML JavaDoc;
8 import org.htmlparser.tags.*;
9 import org.htmlparser.nodes.TagNode;
10 import org.htmlparser.scanners.ScriptScanner;
11
12 /**
13  * Description of the Class
14  *
15  *@author Tlili Khaled
16  */

17 class LabelBuilderVisitor extends NodeVisitor {
18     int count = 0;
19     private HTMLParserTransformer transformer;
20     private Node firstNode = null;
21
22
23     /**
24      * Constructor for the LabelBuilder object
25      *
26      *@param transformer Description of Parameter
27      */

28     public LabelBuilderVisitor(HTMLParserTransformer transformer) {
29         this.transformer = transformer;
30         ScriptScanner.STRICT = false;
31     }
32
33
34     /**
35      * Sets the FirstNode attribute of the TransformBuilderVisitor object
36      *
37      *@param firstNode The new FirstNode value
38      */

39     public void setFirstNode(Node firstNode) {
40         this.firstNode = firstNode;
41     }
42
43
44     /**
45      * Gets the FirstNode attribute of the TransformBuilderVisitor object
46      *
47      *@return The FirstNode value
48      */

49     public Node getFirstNode() {
50         return firstNode;
51     }
52
53
54
55     /**
56      * Description of the Method
57      *
58      *@param tag Description of Parameter
59      */

60     public void visitTag(Tag tag) {
61         if (count == 0) {
62             setFirstNode(tag);
63         }
64
65         //process only input element
66
if (tag.getTagName().equalsIgnoreCase("form")) {
67             //logger.debug("[ Process inputTag ]");
68

69             processFormChildren(tag);
70
71         }
72         count++;
73     }
74
75
76     /**
77      * Description of the Method
78      *
79      *@param tag Description of Parameter
80      */

81     private void processFormChildren(Tag tag) {
82         NodeList newChildren = new NodeList();
83         NodeList children = tag.getChildren();
84         if (children != null) {
85             for (int i = 0; i < children.size(); i++) {
86                 Node child = children.elementAt(i);
87                 if (child instanceof Tag) {
88                     processFormChildren((Tag) child);
89                 }
90
91                 if (child instanceof InputTag) {
92                     String JavaDoc type = ((Tag) child).getAttribute(HTML.Attribute.TYPE.toString());
93                     if (transformer.editableInputParameter(type)) {
94                         TagNode label = buildParameterLabel((InputTag) child);
95                         newChildren.add(label);
96                     }
97                 }
98                 newChildren.add(child);
99             }
100             tag.setChildren(newChildren);
101         }
102     }
103
104
105     /**
106      * Adds a feature to the ParameterLabel attribute of the
107      * HTMLParserTransformer object
108      *
109      *@param tag The feature to be added to the ParameterLabel attribute
110      *@return Description of the Returned Value
111      */

112     private LabelTag buildParameterLabel(InputTag tag) {
113         String JavaDoc type = tag.getAttribute(HTML.Attribute.TYPE.toString());
114         String JavaDoc name = tag.getAttribute(HTML.Attribute.NAME.toString());
115         String JavaDoc value = tag.getAttribute(HTML.Attribute.VALUE.toString());
116
117         //create the label
118
// add the label element
119
LabelTag labelTag = new LabelTag();
120         labelTag.setTagName(HTML.Tag.I.toString());
121         LabelTag endLabelTag = new LabelTag();
122         endLabelTag.setTagName("/" + HTML.Tag.I.toString());
123         labelTag.setEndTag(endLabelTag);
124
125         TextNode t = new TextNode(transformer.getLabelText(name, value, type));
126         NodeList child = new NodeList();
127         child.add(t);
128         labelTag.setChildren(child);
129
130         return labelTag;
131     }
132
133 }
134
Popular Tags