001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text.html;
021:
022: import java.io.IOException;
023: import java.io.ObjectInputStream;
024: import java.io.ObjectOutputStream;
025: import java.io.Serializable;
026: import java.net.MalformedURLException;
027: import java.net.URL;
028: import java.util.HashMap;
029: import java.util.Map;
030:
031: import javax.swing.text.AttributeSet;
032: import javax.swing.text.StyleContext;
033:
034: import org.apache.harmony.x.swing.Utilities;
035:
036: /**
037: * Attributes and Tags of this class are defined by HTML 4.01 specification,
038: * see <a href="http://www.w3.org/TR/html401/">HTML 4.01 Specification</a>.
039: */
040: public class HTML {
041:
042: public static final class Attribute {
043: public static final Attribute ACTION = new Attribute("action");
044: public static final Attribute ALIGN = new Attribute("align");
045: public static final Attribute ALINK = new Attribute("alink");
046: public static final Attribute ALT = new Attribute("alt");
047: public static final Attribute ARCHIVE = new Attribute("archive");
048: public static final Attribute BACKGROUND = new Attribute(
049: "background");
050: public static final Attribute BGCOLOR = new Attribute("bgcolor");
051: public static final Attribute BORDER = new Attribute("border");
052: public static final Attribute CELLPADDING = new Attribute(
053: "cellpadding");
054: public static final Attribute CELLSPACING = new Attribute(
055: "cellspacing");
056: public static final Attribute CHECKED = new Attribute("checked");
057: public static final Attribute CLASS = new Attribute("class");
058: public static final Attribute CLASSID = new Attribute("classid");
059: public static final Attribute CLEAR = new Attribute("clear");
060: public static final Attribute CODE = new Attribute("code");
061: public static final Attribute CODEBASE = new Attribute(
062: "codebase");
063: public static final Attribute CODETYPE = new Attribute(
064: "codetype");
065: public static final Attribute COLOR = new Attribute("color");
066: public static final Attribute COLS = new Attribute("cols");
067: public static final Attribute COLSPAN = new Attribute("colspan");
068: public static final Attribute COMMENT = new Attribute("comment");
069: public static final Attribute COMPACT = new Attribute("compact");
070: public static final Attribute CONTENT = new Attribute("content");
071: public static final Attribute COORDS = new Attribute("coords");
072: public static final Attribute DATA = new Attribute("data");
073: public static final Attribute DECLARE = new Attribute("declare");
074: public static final Attribute DIR = new Attribute("dir");
075: public static final Attribute DUMMY = new Attribute("dummy");
076: public static final Attribute ENCTYPE = new Attribute("enctype");
077: public static final Attribute ENDTAG = new Attribute("endtag");
078: public static final Attribute FACE = new Attribute("face");
079: public static final Attribute FRAMEBORDER = new Attribute(
080: "frameborder");
081: public static final Attribute HALIGN = new Attribute("halign");
082: public static final Attribute HEIGHT = new Attribute("height");
083: public static final Attribute HREF = new Attribute("href");
084: public static final Attribute HSPACE = new Attribute("hspace");
085: public static final Attribute HTTPEQUIV = new Attribute(
086: "http-equiv");
087: public static final Attribute ID = new Attribute("id");
088: public static final Attribute ISMAP = new Attribute("ismap");
089: public static final Attribute LANG = new Attribute("lang");
090: public static final Attribute LANGUAGE = new Attribute(
091: "language");
092: public static final Attribute LINK = new Attribute("link");
093: public static final Attribute LOWSRC = new Attribute("lowsrc");
094: public static final Attribute MARGINHEIGHT = new Attribute(
095: "marginheight");
096: public static final Attribute MARGINWIDTH = new Attribute(
097: "marginwidth");
098: public static final Attribute MAXLENGTH = new Attribute(
099: "maxlength");
100: public static final Attribute METHOD = new Attribute("method");
101: public static final Attribute MULTIPLE = new Attribute(
102: "multiple");
103: public static final Attribute N = new Attribute("n");
104: public static final Attribute NAME = new Attribute("name");
105: public static final Attribute NOHREF = new Attribute("nohref");
106: public static final Attribute NORESIZE = new Attribute(
107: "noresize");
108: public static final Attribute NOSHADE = new Attribute("noshade");
109: public static final Attribute NOWRAP = new Attribute("nowrap");
110: public static final Attribute PROMPT = new Attribute("prompt");
111: public static final Attribute REL = new Attribute("rel");
112: public static final Attribute REV = new Attribute("rev");
113: public static final Attribute ROWS = new Attribute("rows");
114: public static final Attribute ROWSPAN = new Attribute("rowspan");
115: public static final Attribute SCROLLING = new Attribute(
116: "scrolling");
117: public static final Attribute SELECTED = new Attribute(
118: "selected");
119: public static final Attribute SHAPE = new Attribute("shape");
120: public static final Attribute SHAPES = new Attribute("shapes");
121: public static final Attribute SIZE = new Attribute("size");
122: public static final Attribute SRC = new Attribute("src");
123: public static final Attribute STANDBY = new Attribute("standby");
124: public static final Attribute START = new Attribute("start");
125: public static final Attribute STYLE = new Attribute("style");
126: public static final Attribute TARGET = new Attribute("target");
127: public static final Attribute TEXT = new Attribute("text");
128: public static final Attribute TITLE = new Attribute("title");
129: public static final Attribute TYPE = new Attribute("type");
130: public static final Attribute USEMAP = new Attribute("usemap");
131: public static final Attribute VALIGN = new Attribute("valign");
132: public static final Attribute VALUE = new Attribute("value");
133: public static final Attribute VALUETYPE = new Attribute(
134: "valuetype");
135: public static final Attribute VERSION = new Attribute("version");
136: public static final Attribute VLINK = new Attribute("vlink");
137: public static final Attribute VSPACE = new Attribute("vspace");
138: public static final Attribute WIDTH = new Attribute("width");
139:
140: static final Attribute ACCESSKEY = new Attribute("accesskey");
141: static final Attribute DISABLED = new Attribute("disabled");
142: static final Attribute LABEL = new Attribute("label");
143: static final Attribute READONLY = new Attribute("readonly");
144:
145: static final String IMPLIED_NEW_LINE = "CR";
146:
147: private final String id;
148:
149: private Attribute(final String id) {
150: this .id = id;
151: }
152:
153: public String toString() {
154: return id;
155: }
156: }
157:
158: public static class Tag {
159: public static final Tag A = new Tag("a", false, false);
160: public static final Tag ADDRESS = new Tag("address", false,
161: false);
162: public static final Tag APPLET = new Tag("applet", false, false);
163: public static final Tag AREA = new Tag("area", false, false);
164: public static final Tag B = new Tag("b", false, false);
165: public static final Tag BASE = new Tag("base", false, false);
166: public static final Tag BASEFONT = new Tag("basefont", false,
167: false);
168: public static final Tag BIG = new Tag("big", false, false);
169: public static final Tag BLOCKQUOTE = new Tag("blockquote",
170: true, true);
171: public static final Tag BODY = new Tag("body", true, true);
172: public static final Tag BR = new Tag("br", true, false);
173: public static final Tag CAPTION = new Tag("caption", false,
174: false);
175: public static final Tag CENTER = new Tag("center", true, false);
176: public static final Tag CITE = new Tag("cite", false, false);
177: public static final Tag CODE = new Tag("code", false, false);
178: public static final Tag COMMENT = new Tag("comment", false,
179: false);
180: public static final Tag CONTENT = new Tag("content", false,
181: false);
182: public static final Tag DD = new Tag("dd", true, true);
183: public static final Tag DFN = new Tag("dfn", false, false);
184: public static final Tag DIR = new Tag("dir", true, true);
185: public static final Tag DIV = new Tag("div", true, true);
186: public static final Tag DL = new Tag("dl", true, true);
187: public static final Tag DT = new Tag("dt", true, true);
188: public static final Tag EM = new Tag("em", false, false);
189: public static final Tag FONT = new Tag("font", false, false);
190: public static final Tag FORM = new Tag("form", true, false);
191: public static final Tag FRAME = new Tag("frame", false, false);
192: public static final Tag FRAMESET = new Tag("frameset", false,
193: false);
194: public static final Tag H1 = new Tag("h1", true, true);
195: public static final Tag H2 = new Tag("h2", true, true);
196: public static final Tag H3 = new Tag("h3", true, true);
197: public static final Tag H4 = new Tag("h4", true, true);
198: public static final Tag H5 = new Tag("h5", true, true);
199: public static final Tag H6 = new Tag("h6", true, true);
200: public static final Tag HEAD = new Tag("head", true, true);
201: public static final Tag HR = new Tag("hr", true, false);
202: public static final Tag HTML = new Tag("html", true, false);
203: public static final Tag I = new Tag("i", false, false);
204: public static final Tag IMG = new Tag("img", false, false);
205: public static final Tag IMPLIED = new Tag("p-implied", false,
206: false);
207: public static final Tag INPUT = new Tag("input", false, false);
208: public static final Tag ISINDEX = new Tag("isindex", true,
209: false);
210: public static final Tag KBD = new Tag("kbd", false, false);
211: public static final Tag LI = new Tag("li", true, true);
212: public static final Tag LINK = new Tag("link", false, false);
213: public static final Tag MAP = new Tag("map", false, false);
214: public static final Tag MENU = new Tag("menu", true, true);
215: public static final Tag META = new Tag("meta", false, false);
216: public static final Tag NOFRAMES = new Tag("noframes", true,
217: true);
218: public static final Tag OBJECT = new Tag("object", false, false);
219: public static final Tag OL = new Tag("ol", true, true);
220: public static final Tag OPTION = new Tag("option", false, false);
221: public static final Tag P = new Tag("p", true, true);
222: public static final Tag PARAM = new Tag("param", false, false);
223: public static final Tag PRE = new Tag("pre", true, true);
224: public static final Tag S = new Tag("s", false, false);
225: public static final Tag SAMP = new Tag("samp", false, false);
226: public static final Tag SCRIPT = new Tag("script", false, false);
227: public static final Tag SELECT = new Tag("select", false, false);
228: public static final Tag SMALL = new Tag("small", false, false);
229: public static final Tag SPAN = new Tag("span", false, false);
230: public static final Tag STRIKE = new Tag("strike", false, false);
231: public static final Tag STRONG = new Tag("strong", false, false);
232: public static final Tag STYLE = new Tag("style", false, false);
233: public static final Tag SUB = new Tag("sub", false, false);
234: public static final Tag SUP = new Tag("sup", false, false);
235: public static final Tag TABLE = new Tag("table", false, true);
236: public static final Tag TD = new Tag("td", true, true);
237: public static final Tag TEXTAREA = new Tag("textarea", false,
238: false);
239: public static final Tag TH = new Tag("th", true, true);
240: public static final Tag TITLE = new Tag("title", true, true);
241: public static final Tag TR = new Tag("tr", false, true);
242: public static final Tag TT = new Tag("tt", false, false);
243: public static final Tag U = new Tag("u", false, false);
244: public static final Tag UL = new Tag("ul", true, true);
245: public static final Tag VAR = new Tag("var", false, false);
246:
247: static final Tag ABBR = new Tag("abbr", false, false);
248: static final Tag ACRONYM = new Tag("acronym", false, false);
249: static final Tag BDO = new Tag("bdo", false, false);
250: static final Tag BUTTON = new Tag("button", false, false);
251: static final Tag COL = new Tag("col", false, false);
252: static final Tag COLGROUP = new Tag("colgroup", false, false);
253: static final Tag DEL = new Tag("del", false, false);
254: static final Tag FIELDSET = new Tag("fieldset", false, false);
255: static final Tag IFRAME = new Tag("iframe", true, true);
256: static final Tag INS = new Tag("ins", false, false);
257: static final Tag LABEL = new Tag("label", false, false);
258: static final Tag LEGEND = new Tag("legend", false, false);
259: static final Tag NOSCRIPT = new Tag("noscript", true, true);
260: static final Tag OPTGROUP = new Tag("optgroup", false, false);
261: static final Tag Q = new Tag("q", false, false);
262: static final Tag TBODY = new Tag("tbody", true, true);
263: static final Tag TFOOT = new Tag("tfoot", true, true);
264: static final Tag THEAD = new Tag("thead", true, true);
265:
266: String id;
267: boolean causesBreak;
268: boolean isBlock;
269:
270: public Tag() {
271: this (null, false, false);
272: }
273:
274: protected Tag(final String id) {
275: this (id, false, false);
276: }
277:
278: protected Tag(final String id, final boolean causesBreak,
279: final boolean isBlock) {
280: this .id = id;
281: this .causesBreak = causesBreak;
282: this .isBlock = isBlock;
283: }
284:
285: public boolean breaksFlow() {
286: return causesBreak;
287: }
288:
289: public boolean isBlock() {
290: return isBlock;
291: }
292:
293: public boolean isPreformatted() {
294: return this == PRE || this == TEXTAREA;
295: }
296:
297: public String toString() {
298: return id;
299: }
300: }
301:
302: public static class UnknownTag extends Tag implements Serializable {
303: public UnknownTag(final String id) {
304: super (id);
305: }
306:
307: public boolean equals(final Object obj) {
308: return obj instanceof UnknownTag
309: && toString().equals(obj.toString());
310: }
311:
312: public int hashCode() {
313: return toString().hashCode();
314: }
315:
316: private void writeObject(final ObjectOutputStream out)
317: throws IOException {
318:
319: out.defaultWriteObject();
320: out.writeObject(toString());
321: out.writeBoolean(breaksFlow());
322: out.writeBoolean(isBlock());
323: }
324:
325: private void readObject(final ObjectInputStream in)
326: throws IOException, ClassNotFoundException {
327:
328: in.defaultReadObject();
329: id = (String) in.readObject();
330: causesBreak = in.readBoolean();
331: isBlock = in.readBoolean();
332: }
333: }
334:
335: public static final String NULL_ATTRIBUTE_VALUE = "#DEFAULT";
336:
337: private static final Map attrMap = new HashMap();
338: private static final Map tagMap = new HashMap();
339:
340: private static final Attribute[] attrs = { Attribute.ACTION,
341: Attribute.ALIGN, Attribute.ALINK, Attribute.ALT,
342: Attribute.ARCHIVE, Attribute.BACKGROUND, Attribute.BGCOLOR,
343: Attribute.BORDER, Attribute.CELLPADDING,
344: Attribute.CELLSPACING, Attribute.CHECKED, Attribute.CLASS,
345: Attribute.CLASSID, Attribute.CLEAR, Attribute.CODE,
346: Attribute.CODEBASE, Attribute.CODETYPE, Attribute.COLOR,
347: Attribute.COLS, Attribute.COLSPAN, Attribute.COMMENT,
348: Attribute.COMPACT, Attribute.CONTENT, Attribute.COORDS,
349: Attribute.DATA, Attribute.DECLARE, Attribute.DIR,
350: Attribute.DUMMY, Attribute.ENCTYPE, Attribute.ENDTAG,
351: Attribute.FACE, Attribute.FRAMEBORDER, Attribute.HALIGN,
352: Attribute.HEIGHT, Attribute.HREF, Attribute.HSPACE,
353: Attribute.HTTPEQUIV, Attribute.ID, Attribute.ISMAP,
354: Attribute.LANG, Attribute.LANGUAGE, Attribute.LINK,
355: Attribute.LOWSRC, Attribute.MARGINHEIGHT,
356: Attribute.MARGINWIDTH, Attribute.MAXLENGTH,
357: Attribute.METHOD, Attribute.MULTIPLE, Attribute.N,
358: Attribute.NAME, Attribute.NOHREF, Attribute.NORESIZE,
359: Attribute.NOSHADE, Attribute.NOWRAP, Attribute.PROMPT,
360: Attribute.REL, Attribute.REV, Attribute.ROWS,
361: Attribute.ROWSPAN, Attribute.SCROLLING, Attribute.SELECTED,
362: Attribute.SHAPE, Attribute.SHAPES, Attribute.SIZE,
363: Attribute.SRC, Attribute.STANDBY, Attribute.START,
364: Attribute.STYLE, Attribute.TARGET, Attribute.TEXT,
365: Attribute.TITLE, Attribute.TYPE, Attribute.USEMAP,
366: Attribute.VALIGN, Attribute.VALUE, Attribute.VALUETYPE,
367: Attribute.VERSION, Attribute.VLINK, Attribute.VSPACE,
368: Attribute.WIDTH,
369:
370: // HTML 4.01-specific attributes
371: Attribute.ACCESSKEY, Attribute.DISABLED, Attribute.LABEL,
372: Attribute.READONLY };
373:
374: private static final Tag[] tags = {
375: Tag.A,
376: Tag.ADDRESS,
377: Tag.APPLET,
378: Tag.AREA,
379: Tag.B,
380: Tag.BASE,
381: Tag.BASEFONT,
382: Tag.BIG,
383: Tag.BLOCKQUOTE,
384: Tag.BODY,
385: Tag.BR,
386: Tag.CAPTION,
387: Tag.CENTER,
388: Tag.CITE,
389: Tag.CODE,
390: //Tag.COMMENT,
391: //Tag.CONTENT,
392: Tag.DD, Tag.DFN, Tag.DIR, Tag.DIV, Tag.DL, Tag.DT, Tag.EM,
393: Tag.FONT, Tag.FORM, Tag.FRAME, Tag.FRAMESET, Tag.H1,
394: Tag.H2,
395: Tag.H3,
396: Tag.H4,
397: Tag.H5,
398: Tag.H6,
399: Tag.HEAD,
400: Tag.HR,
401: Tag.HTML,
402: Tag.I,
403: Tag.IMG,
404: //Tag.IMPLIED,
405: Tag.INPUT, Tag.ISINDEX, Tag.KBD, Tag.LI, Tag.LINK, Tag.MAP,
406: Tag.MENU, Tag.META, Tag.NOFRAMES, Tag.OBJECT, Tag.OL,
407: Tag.OPTION, Tag.P, Tag.PARAM, Tag.PRE, Tag.S, Tag.SAMP,
408: Tag.SCRIPT, Tag.SELECT, Tag.SMALL, Tag.SPAN, Tag.STRIKE,
409: Tag.STRONG, Tag.STYLE, Tag.SUB, Tag.SUP, Tag.TABLE, Tag.TD,
410: Tag.TEXTAREA, Tag.TH, Tag.TITLE, Tag.TR, Tag.TT,
411: Tag.U,
412: Tag.UL,
413: Tag.VAR,
414: // HTML 4.01-specific tags
415: Tag.ABBR, Tag.ACRONYM, Tag.BDO, Tag.BUTTON, Tag.COL,
416: Tag.COLGROUP, Tag.DEL, Tag.FIELDSET, Tag.IFRAME, Tag.INS,
417: Tag.LABEL, Tag.LEGEND, Tag.NOSCRIPT, Tag.OPTGROUP, Tag.Q,
418: Tag.TBODY, Tag.TFOOT, Tag.THEAD };
419:
420: static {
421: for (int i = 0; i < attrs.length; i++) {
422: attrMap.put(attrs[i].toString(), attrs[i]);
423: StyleContext.registerStaticAttributeKey(attrs[i]);
424: }
425:
426: for (int i = 0; i < tags.length; i++) {
427: tagMap.put(tags[i].toString(), tags[i]);
428: StyleContext.registerStaticAttributeKey(tags[i]);
429: }
430: }
431:
432: public HTML() {
433: }
434:
435: public static Tag[] getAllTags() {
436: return tags;
437: }
438:
439: public static Tag getTag(final String tagName) {
440: return (Tag) tagMap.get(tagName);
441: }
442:
443: public static Attribute[] getAllAttributeKeys() {
444: return attrs;
445: }
446:
447: public static Attribute getAttributeKey(final String attrName) {
448: return (Attribute) attrMap.get(attrName);
449: }
450:
451: public static int getIntegerAttributeValue(final AttributeSet attr,
452: final Attribute key, final int def) {
453: final Object value = attr.getAttribute(key);
454: if (value != null) {
455: try {
456: return Integer.parseInt((String) value);
457: } catch (NumberFormatException e) {
458: return def;
459: }
460: }
461: return def;
462: }
463:
464: static URL resolveURL(final URL url, final URL base) {
465: if (url == null) {
466: return null;
467: }
468:
469: try {
470: return ((base != null) ? new URL(base, url.toString())
471: : url);
472: } catch (MalformedURLException e) {
473: return null;
474: }
475: }
476:
477: static URL resolveURL(final String url, final URL base) {
478: if (Utilities.isEmptyString(url)) {
479: return null;
480: }
481:
482: try {
483: return ((base != null) ? new URL(base, url) : new URL(url));
484: } catch (MalformedURLException e) {
485: return null;
486: }
487: }
488:
489: static URL resolveURL(final URL url, final String base) {
490: if (url == null) {
491: return null;
492: }
493:
494: try {
495: return ((base != null) ? new URL(new URL(base), url
496: .toString()) : url);
497: } catch (MalformedURLException e) {
498: return null;
499: }
500: }
501:
502: static URL resolveURL(final String url, final String base) {
503: if (Utilities.isEmptyString(url)) {
504: return null;
505: }
506:
507: try {
508: return ((base != null) ? new URL(new URL(base), url)
509: : new URL(url));
510: } catch (MalformedURLException e) {
511: return null;
512: }
513: }
514: }
|