KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > xhtml > XHTMLTag


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.wizards.xhtml;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import org.apache.lucene.demo.html.HTMLParserConstants;
17 import org.apache.lucene.demo.html.Token;
18
19 public class XHTMLTag {
20     
21     /*
22      * Empty XHTML Strict 1.0 elements
23      * derived from http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd
24      */

25     private static final ArrayList JavaDoc F_XHTML_S1_EE = new ArrayList JavaDoc(10);
26     static {
27         F_XHTML_S1_EE.add("base"); //$NON-NLS-1$
28
F_XHTML_S1_EE.add("meta"); //$NON-NLS-1$
29
F_XHTML_S1_EE.add("link"); //$NON-NLS-1$
30
F_XHTML_S1_EE.add("hr"); //$NON-NLS-1$
31
F_XHTML_S1_EE.add("br"); //$NON-NLS-1$
32
F_XHTML_S1_EE.add("param"); //$NON-NLS-1$
33
F_XHTML_S1_EE.add("img"); //$NON-NLS-1$
34
F_XHTML_S1_EE.add("area"); //$NON-NLS-1$
35
F_XHTML_S1_EE.add("input"); //$NON-NLS-1$
36
F_XHTML_S1_EE.add("col"); //$NON-NLS-1$
37
}
38     /*
39      * Empty XHTML Transitional 1.0 elements
40      * derived from http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
41      */

42     private static final ArrayList JavaDoc F_XHTML_T1_EE = new ArrayList JavaDoc(12);
43     static {
44         F_XHTML_T1_EE.add("base"); //$NON-NLS-1$
45
F_XHTML_T1_EE.add("meta"); //$NON-NLS-1$
46
F_XHTML_T1_EE.add("link"); //$NON-NLS-1$
47
F_XHTML_T1_EE.add("hr"); //$NON-NLS-1$
48
F_XHTML_T1_EE.add("br"); //$NON-NLS-1$
49
F_XHTML_T1_EE.add("basefont"); //$NON-NLS-1$
50
F_XHTML_T1_EE.add("param"); //$NON-NLS-1$
51
F_XHTML_T1_EE.add("img"); //$NON-NLS-1$
52
F_XHTML_T1_EE.add("area"); //$NON-NLS-1$
53
F_XHTML_T1_EE.add("input"); //$NON-NLS-1$
54
F_XHTML_T1_EE.add("isindex"); //$NON-NLS-1$
55
F_XHTML_T1_EE.add("col"); //$NON-NLS-1$
56
}
57     /*
58      * Empty XHTML Frameset 1.0 elements
59      * derived from http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
60      */

61     private static final ArrayList JavaDoc F_XHTML_F1_EE = new ArrayList JavaDoc(13);
62     static {
63         F_XHTML_F1_EE.add("base"); //$NON-NLS-1$
64
F_XHTML_F1_EE.add("meta"); //$NON-NLS-1$
65
F_XHTML_F1_EE.add("link"); //$NON-NLS-1$
66
F_XHTML_F1_EE.add("frame"); //$NON-NLS-1$
67
F_XHTML_F1_EE.add("hr"); //$NON-NLS-1$
68
F_XHTML_F1_EE.add("br"); //$NON-NLS-1$
69
F_XHTML_F1_EE.add("basefont"); //$NON-NLS-1$
70
F_XHTML_F1_EE.add("param"); //$NON-NLS-1$
71
F_XHTML_F1_EE.add("img"); //$NON-NLS-1$
72
F_XHTML_F1_EE.add("area"); //$NON-NLS-1$
73
F_XHTML_F1_EE.add("input"); //$NON-NLS-1$
74
F_XHTML_F1_EE.add("isindex"); //$NON-NLS-1$
75
F_XHTML_F1_EE.add("col"); //$NON-NLS-1$
76
}
77     private static final char F_TAG_OPEN = '<';
78     private static final char F_TAG_CLOSE = '>';
79     private static final char F_TAG_SLASH = '/';
80     private static final char F_TAG_SPACE = ' ';
81     private static final char F_TAG_DQUOTE = '"';
82     
83     
84     private String JavaDoc fTagName;
85     private String JavaDoc fCurrAttName;
86     private ArrayList JavaDoc fAttribNames;
87     private ArrayList JavaDoc fAttribValues;
88     private boolean fIsClosingTag;
89     private boolean fIsEmptyTag;
90     private int fDTDType;
91     
92     public XHTMLTag(String JavaDoc tagName, int type) {
93         fTagName = extractTagName(tagName);
94         fDTDType = type;
95         fAttribNames = new ArrayList JavaDoc();
96         fAttribValues = new ArrayList JavaDoc();
97     }
98
99     private String JavaDoc extractTagName(String JavaDoc tagName) {
100         if (tagName == null || tagName.length() == 0)
101             return null;
102         if (tagName.charAt(0) == F_TAG_OPEN) {
103             if (tagName.length() == 1)
104                 return null;
105             tagName = tagName.substring(1);
106         }
107         if (tagName.charAt(0) == F_TAG_SLASH) {
108             if (tagName.length() == 1)
109                 return null;
110             tagName = tagName.substring(1);
111             fIsClosingTag = true;
112         }
113         return tagName.toLowerCase();
114     }
115     
116     protected void eatToken(Token token) {
117         switch (token.kind) {
118         case HTMLParserConstants.ArgName:
119             expandLeftoverAttribute();
120             // set the attribute name or mark tag as empty
121
if (token.image.length() == 1
122                     && token.image.charAt(0) == F_TAG_SLASH) {
123                 fCurrAttName = null;
124                 fIsEmptyTag = true;
125             } else
126                 fCurrAttName = token.image;
127             break;
128         case HTMLParserConstants.ArgValue:
129         case HTMLParserConstants.Quote1Text:
130         case HTMLParserConstants.Quote2Text:
131             // found the value: add the attribute
132
addAttribute(fCurrAttName, token.image);
133             break;
134         }
135     }
136     
137     public void addAttribute(String JavaDoc attName, String JavaDoc attValue) {
138         if (attName == null)
139             return;
140         String JavaDoc name = attName.toLowerCase();
141         // remove old attribute - using list to maintain attribute index order
142
int index = fAttribNames.size();
143         if (fAttribNames.contains(name)) {
144             index = fAttribNames.indexOf(name);
145             fAttribNames.remove(index);
146             fAttribValues.remove(index);
147         }
148         fAttribNames.add(index, attName.toLowerCase());
149         fAttribValues.add(index, attValue != null ? attValue : attName);
150         // reset name
151
fCurrAttName = null;
152     }
153     
154     public void expandLeftoverAttribute() {
155         if (fCurrAttName != null)
156             addAttribute(fCurrAttName, null);
157     }
158
159     public void writeClosed(PrintWriter JavaDoc pw) {
160         if (fTagName == null)
161             return;
162         pw.write(F_TAG_OPEN);
163         pw.write(F_TAG_SLASH);
164         pw.write(fTagName);
165         pw.write(F_TAG_CLOSE);
166     }
167     
168     public void write(PrintWriter JavaDoc pw) {
169         String JavaDoc tag = toString();
170         if (tag != null)
171             pw.write(tag);
172     }
173     
174     public String JavaDoc toString() {
175         // don't write tags which weren't initialized properly
176
if (fTagName == null)
177             return null;
178         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
179         sb.append(F_TAG_OPEN);
180         if (fIsClosingTag)
181             sb.append(F_TAG_SLASH);
182         sb.append(fTagName);
183         if (!fIsClosingTag) {
184             for (int i = 0; i < fAttribNames.size(); i++) {
185                 sb.append(F_TAG_SPACE);
186                 sb.append(fAttribNames.get(i).toString());
187                 sb.append('=');
188                 sb.append(F_TAG_DQUOTE);
189                 sb.append(fAttribValues.get(i).toString());
190                 sb.append(F_TAG_DQUOTE);
191             }
192             if (isEmptyTag()) {
193                 sb.append(F_TAG_SPACE);
194                 sb.append(F_TAG_SLASH);
195             }
196         }
197         
198         sb.append(F_TAG_CLOSE);
199         return sb.toString();
200     }
201     
202     public boolean isClosingTag() {
203         return fIsClosingTag;
204     }
205     
206     public boolean isEmptyTag() {
207         if (fIsEmptyTag)
208             return true;
209         switch(fDTDType) {
210         case XHTMLConverter.XHTML_STRICT:
211             return F_XHTML_S1_EE.contains(fTagName);
212         case XHTMLConverter.XHTML_TRANSITIONAL:
213             return F_XHTML_T1_EE.contains(fTagName);
214         case XHTMLConverter.XHTML_FRAMESET:
215             return F_XHTML_F1_EE.contains(fTagName);
216         }
217         return false;
218     }
219
220 }
221
Popular Tags