001: package com.xoetrope.builder.w3c.html;
002:
003: import com.xoetrope.builder.w3c.html.tags.A;
004: import com.xoetrope.builder.w3c.html.tags.Address;
005: import com.xoetrope.builder.w3c.html.tags.BaseFont;
006: import com.xoetrope.builder.w3c.html.tags.BlockQuote;
007: import com.xoetrope.builder.w3c.html.tags.Br;
008: import com.xoetrope.builder.w3c.html.tags.Col;
009: import com.xoetrope.builder.w3c.html.tags.Div;
010: import com.xoetrope.builder.w3c.html.tags.Font;
011: import com.xoetrope.builder.w3c.html.tags.Form;
012: import com.xoetrope.builder.w3c.html.tags.Hr;
013: import com.xoetrope.builder.w3c.html.tags.XHeadingTag;
014: import com.xoetrope.builder.w3c.html.tags.Img;
015: import com.xoetrope.builder.w3c.html.tags.Input;
016: import com.xoetrope.builder.w3c.html.tags.Map;
017: import com.xoetrope.builder.w3c.html.tags.P;
018: import com.xoetrope.builder.w3c.html.tags.Script;
019: import com.xoetrope.builder.w3c.html.tags.Select;
020: import com.xoetrope.builder.w3c.html.tags.Span;
021: import com.xoetrope.builder.w3c.html.tags.Style;
022: import com.xoetrope.builder.w3c.html.tags.Table;
023: import com.xoetrope.builder.w3c.html.tags.Td;
024: import com.xoetrope.builder.w3c.html.tags.TextArea;
025: import com.xoetrope.builder.w3c.html.tags.XDataTagHandler;
026: import com.xoetrope.builder.w3c.html.tags.XDefaultTagHandler;
027: import com.xoetrope.builder.w3c.html.tags.XFormatTagHandler;
028: import com.xoetrope.builder.w3c.html.tags.XObjectTagHandler;
029: import com.xoetrope.builder.w3c.html.tags.XHtmlTagHandler;
030: import com.xoetrope.builder.w3c.html.tags.XStructuralTagHandler;
031: import java.awt.Color;
032: import java.awt.Component;
033: import java.io.Reader;
034: import java.util.Enumeration;
035: import java.util.Hashtable;
036: import java.awt.Container;
037:
038: import net.xoetrope.builder.XuiBuilder;
039: import net.xoetrope.debug.DebugLogger;
040: import net.xoetrope.xui.XPage;
041: import net.xoetrope.xui.PageSupport;
042: import net.xoetrope.xui.build.BuildProperties;
043: import java.io.BufferedReader;
044: import java.io.InputStream;
045: import java.io.InputStreamReader;
046: import java.net.MalformedURLException;
047: import java.net.URL;
048: import java.net.URLConnection;
049: import java.util.StringTokenizer;
050: import javax.swing.SwingConstants;
051: import javax.swing.text.MutableAttributeSet;
052: import javax.swing.text.html.HTML;
053: import javax.swing.text.html.HTMLDocument;
054: import javax.swing.text.html.HTMLEditorKit;
055: import javax.swing.text.html.parser.ParserDelegator;
056: import net.xoetrope.xui.XComponentConstructor;
057: import net.xoetrope.xui.XComponentFactory;
058: import net.xoetrope.xui.XProject;
059:
060: /**
061: * A build for W3C HTML forms/pages. The builder can create XUI pages on-the-fly
062: * or it can be used to convert and save an html page to a xui format.
063: * <p> Copyright (c) Xoetrope Ltd., 2002-2006</p>
064: * <p> $Revision: 1.8 $</p>
065: * <p> License: see License.txt</p>
066: */
067: public class XHtmlBuilder extends XuiBuilder {
068: protected static boolean debugLayout = false;
069:
070: private static Hashtable swingConstants;
071: private static Hashtable<Object, XHtmlTagHandler> htmlTags = null;
072: private Hashtable<String, XHtmlStyle> styles;
073:
074: protected String packageName;
075:
076: protected String selectStyle = XPage.RADIO;
077: protected XHtmlComponentFactory htmlFactory;
078: protected XHtmlTagHandler lastHandler;
079:
080: protected static URL documentUrl;
081:
082: /**
083: * Create a new builder
084: * @param project the current xui project
085: * @param factory the component factory
086: */
087: public XHtmlBuilder(XProject project) {
088: super (project, XPage.XUI_SWING_PACKAGE);
089: setupHtmlTags();
090: styles = new Hashtable<String, XHtmlStyle>();
091:
092: Hashtable componentFactories = XComponentFactory.getFactories();
093: Enumeration enumeration = componentFactories.keys();
094: while (enumeration.hasMoreElements()) {
095: XComponentConstructor factory = (XComponentConstructor) componentFactories
096: .get(enumeration.nextElement());
097: if (factory instanceof XHtmlComponentFactory) {
098: htmlFactory = (XHtmlComponentFactory) factory;
099: break;
100: }
101: }
102: }
103:
104: /**
105: * Is a debug layout to be used?
106: * @return true if the debug layout is to be used
107: */
108: public static boolean isDebugLayout() {
109: return debugLayout;
110: }
111:
112: /**
113: * Set the debug layout flag
114: * @param state true if the debug layout is to be used
115: */
116: public static void setDebugLayout(boolean state) {
117: debugLayout = state;
118: }
119:
120: /**
121: * Attempt to get an input stream from the specified path
122: * @param urlStr the url string
123: * @return the inputstream or null if the strean cannot be opened.
124: */
125: protected InputStream getUrlInputStream(String urlStr) {
126: try {
127: documentUrl = new URL(urlStr);
128:
129: // Open the connection and set the properties so that the input is loaded
130: // from the server and even if a cached version is available.
131: URLConnection conn = documentUrl.openConnection();
132: conn.setDefaultUseCaches(false);
133: conn.setIfModifiedSince(0);
134: conn.setDoInput(true);
135: conn.setDoOutput(false);
136: conn.setUseCaches(false);
137: conn.connect();
138:
139: InputStream is = conn.getInputStream();
140: return is;
141: } catch (MalformedURLException ex) {
142: } catch (Exception ex) {
143: if (BuildProperties.DEBUG)
144: DebugLogger
145: .logError("Unable to load the page from the server: "
146: + urlStr);
147: //ex.printStackTrace();
148: }
149: return null;
150: }
151:
152: /**
153: * Loads an XPage via a reader obtained from the XProject (searches
154: * the classpath). The pageName is assumed to be the name of an XML file. For
155: * example if the pageName is 'welcome' then the 'welcome.xml' file is read as
156: * a UTF8 encoded XML file (by default).
157: * @param defPackageName the package or path to the page
158: * @param pageName the page name or the name of the class implementing the page
159: * @param include true if the page to be loaded is being included in another
160: * page in which case any class attribute of the included page is ignored
161: * @return the page
162: */
163: public PageSupport loadPage(String defPackageName, String pageName,
164: boolean include) {
165: packageName = defPackageName;
166:
167: Reader r = null;
168: try {
169: InputStream is = getUrlInputStream(pageName);
170: if (is != null)
171: r = new BufferedReader(new InputStreamReader(is));
172: else {
173: String fileName = pageName;
174: if (pageName.indexOf(".htm") < 0)
175: fileName += ".html";
176: r = currentProject.getBufferedReader(fileName, null);
177: documentUrl = currentProject.findResource(fileName);
178: }
179: } catch (Exception ex) {
180: if (BuildProperties.DEBUG)
181: DebugLogger.logError("BUILDER", "File NOT found: "
182: + pageName);
183: }
184:
185: try {
186: if ((r == null) || !r.ready())
187: return null;
188:
189: return readPage(r, pageName, ".html", include);
190: } catch (Exception e) {
191: if (BuildProperties.DEBUG)
192: DebugLogger.logError("BUILDER", "File NOT found: "
193: + pageName);
194: } finally {
195: if (!include)
196: rootPage = null;
197: }
198: return null;
199: }
200:
201: /**
202: * Read an XML description of the page and construct a new XPage. An instance
203: * of the class specified by the class attribute is constructed or else an
204: * instance of XPage if no class attribute is specified. The new page is
205: * populated but is not yet added to its parent.
206: * <br>
207: * The startup file parameter 'DefaultClass' is used to obtain a default for
208: * each page's class if a class parameter is not specified in the page's XML
209: * <br>
210: * The startup file parameter 'Validations' is used to obtain a default for
211: * each page's set of validation rules
212: *
213: * @param reader a input stream from which to read the page
214: * @param pageName the name of the page
215: * @param ext the file extension
216: * @param include the page to be loaded is being included in another page
217: * @return the page
218: */
219: public PageSupport readPage(Reader reader, String pageName,
220: String ext, boolean include) {
221: try {
222: setupPage(pageName, ext, include);
223:
224: HTMLEditorKit editorKit = new HTMLEditorKit();
225: HTMLDocument doc = (HTMLDocument) editorKit
226: .createDefaultDocument();
227: HTMLEditorKit.ParserCallback callback = new XHtmlBuilderParserCallback(
228: this );
229: ParserDelegator pd = new ParserDelegator();
230: doc.setPreservesUnknownTags(true);
231: doc.setParser(pd);
232: doc.setPreservesUnknownTags(true);
233: pd.parse(reader, callback, true);
234: } catch (Exception e) {
235: if (BuildProperties.DEBUG)
236: DebugLogger
237: .logError("Exception while reading the page: "
238: + pageName);
239: e.printStackTrace();
240: }
241: return page;
242: }
243:
244: /**
245: * Loads the page based on the contents of the page tag or by using default
246: * values.
247: *
248: * @param pageName the name of the page
249: * @param ext the file extension
250: * @param include the page to be loaded is being included in another page
251: */
252: protected void setupPage(String pageName, String ext,
253: boolean include) {
254: String className = "net.xoetrope.xui.XPage";
255:
256: if (!include) {
257: if ((className.indexOf('.') <= 0)
258: && (packageName.length() > 1))
259: className = packageName + className;
260: try {
261: page = loadClass(className);
262: } catch (Exception e) {
263: if (BuildProperties.DEBUG)
264: DebugLogger
265: .trace("Unable to load the named class: "
266: + className);
267: page = new XPage();
268: }
269: setPageName(pageName);
270: setPageExtension(ext);
271: page.setLayout(new XHtmlFormLayout(this ));
272:
273: componentFactory.setParentComponent((Container) page);
274: }
275:
276: rootPage = (XPage) page;
277: }
278:
279: /**
280: * Find a resource URl from a resource string, resolving relative resource
281: * names in the process.
282: * @param fileName the resource string
283: * @return the resource url
284: */
285: public static URL findResource(String fileName) {
286: try {
287: return new URL(documentUrl, fileName);
288: } catch (Exception e) {
289: return null;
290: }
291: }
292:
293: /**
294: * <p>Set a named attributes. The attributes are stored in a hashtable owned by
295: * the page. Derived classes may access the hashtable directly but the
296: * preferred method of access is the getAttribute method. Attributes are used
297: * by the XuiBuilder class for component attributes other than those it handles
298: * directly. The attributes can be thought of as component properties or extra
299: * data and need not be used directly by the component.</p>
300: * <p>
301: * Attributes are stored using a key in the form attribName_compName or just
302: * the attribName if compName is null.
303: * </p>
304: * @param attribName the attribute name
305: * @param compName the component name or null if it is a page attribute
306: * @param attribValue the attribute value
307: */
308: public void setComponentAttribute(String compName,
309: String attribName, Object attribValue) {
310: page.setAttribute(compName, attribName, attribValue);
311: }
312:
313: /**
314: * <p>Get a named attributes. The attributes are stored in a hashtable owned by
315: * the page. Derived classes may access the hashtable directly but the
316: * preferred method of access is the getAttribute method. Attributes are used
317: * by the XuiBuilder class for component attributes other than those it handles
318: * directly. The attributes can be thought of as component properties or extra
319: * data and need not be used directly by the component.</p>
320: * <p>
321: * Attributes are stored using a key in the form attribName_compName or just
322: * the attribName if compName is null.
323: * </p>
324: * @param attribName the attribute name
325: * @param compName the component name or null if it is a page attribute
326: */
327: public Object getComponentAttribute(String compName,
328: String attribName) {
329: return page.getAttribute(compName, attribName);
330: }
331:
332: // Start script handling -----------------------------------------------------
333: /**
334: * Add a new script function to the current class
335: * @param componentName the name of the element being processed
336: * @param eventName the name of the html event or null if a page event is being processed
337: * @param methodName the name of the new method
338: * @param script the contents of the method - the original javascript (assuming it is javascript)
339: */
340: public void addScript(String componentName, String eventName,
341: String methodName, String script) {
342: }
343:
344: /**
345: * Process the event specification, specified as an attribute of an HTML element
346: * @param handler the tag handler
347: * @param attribName the attribute name
348: * @param attribValue the attribute value, containing the script
349: */
350: public void processEvent(XHtmlTagHandler handler,
351: String attribName, String attribValue) {
352: }
353:
354: // End script handling -------------------------------------------------------
355:
356: // Start tag handling --------------------------------------------------------
357: /**
358: * Create a hashtable of tags and ids to speed processing of HTML
359: */
360: protected void setupHtmlTags() {
361: if (htmlTags == null) {
362: htmlTags = new Hashtable<Object, XHtmlTagHandler>();
363: htmlTags.put(HTML.Tag.A, new A());
364: htmlTags.put(HTML.Tag.ADDRESS, new Address());
365: htmlTags.put(HTML.Tag.APPLET, new XObjectTagHandler(
366: HTML.Tag.APPLET));
367: htmlTags.put(HTML.Tag.AREA, new XDataTagHandler(
368: HTML.Tag.AREA));
369: htmlTags.put(HTML.Tag.B, new XFormatTagHandler(HTML.Tag.B));
370: // htmlTags.put( HTML.Tag.BASE, new Integer( XHtmlBuilder.BASE )); // Deprecated
371: htmlTags.put(HTML.Tag.BASEFONT, new BaseFont());
372: htmlTags.put(HTML.Tag.BIG, new XFormatTagHandler(
373: HTML.Tag.BIG));
374: htmlTags.put(HTML.Tag.BLOCKQUOTE, new BlockQuote());
375: htmlTags.put(HTML.Tag.BODY, new XStructuralTagHandler(
376: HTML.Tag.BODY));
377: htmlTags.put(HTML.Tag.BR, new Br());
378: htmlTags.put(HTML.Tag.CAPTION, new XDataTagHandler(
379: HTML.Tag.CAPTION));
380: htmlTags.put(HTML.Tag.CENTER, new Div(HTML.Tag.CENTER));
381: htmlTags.put(HTML.Tag.CITE, new XFormatTagHandler(
382: HTML.Tag.CITE));
383: htmlTags.put(HTML.Tag.CODE, new XFormatTagHandler(
384: HTML.Tag.CODE));
385: htmlTags.put("col", new Col("col"));
386: htmlTags.put("colgroup", new Col("colgroup"));
387: htmlTags.put(HTML.Tag.DD, new XDataTagHandler(HTML.Tag.DD));
388: htmlTags.put(HTML.Tag.DFN, new XFormatTagHandler(
389: HTML.Tag.DFN));
390: // htmlTags.put( HTML.Tag.DIR, new Integer( XHtmlBuilder.DIR )); // Deprecated
391: htmlTags.put(HTML.Tag.DIV, new Div(HTML.Tag.DIV));
392: htmlTags.put(HTML.Tag.DL, new XDataTagHandler(HTML.Tag.DL));
393: htmlTags.put(HTML.Tag.DT, new XDataTagHandler(HTML.Tag.DT));
394: htmlTags.put(HTML.Tag.EM,
395: new XFormatTagHandler(HTML.Tag.EM));
396: htmlTags.put(HTML.Tag.FONT, new Font());
397: htmlTags.put(HTML.Tag.FORM, new Form());
398: // htmlTags.put( HTML.Tag.FRAME, new Integer( XHtmlBuilder.FRAME ));
399: // htmlTags.put( HTML.Tag.FRAMESET, new Integer( XHtmlBuilder.FRAMESET ));
400: htmlTags.put(HTML.Tag.H1, new XHeadingTag(HTML.Tag.H1));
401: htmlTags.put(HTML.Tag.H2, new XHeadingTag(HTML.Tag.H2));
402: htmlTags.put(HTML.Tag.H3, new XHeadingTag(HTML.Tag.H3));
403: htmlTags.put(HTML.Tag.H4, new XHeadingTag(HTML.Tag.H4));
404: htmlTags.put(HTML.Tag.H5, new XHeadingTag(HTML.Tag.H5));
405: htmlTags.put(HTML.Tag.H6, new XHeadingTag(HTML.Tag.H6));
406: htmlTags.put(HTML.Tag.HEAD, new XStructuralTagHandler(
407: HTML.Tag.HEAD));
408: htmlTags.put(HTML.Tag.HR, new Hr());
409: htmlTags.put(HTML.Tag.HTML, new XStructuralTagHandler(
410: HTML.Tag.HTML));
411: htmlTags.put(HTML.Tag.I, new XFormatTagHandler(HTML.Tag.I));
412: htmlTags.put(HTML.Tag.IMG, new Img());
413: htmlTags.put(HTML.Tag.INPUT, new Input());
414: // htmlTags.put( HTML.Tag.ISINDEX, new Integer( XHtmlBuilder.ISINDEX )); // Deprecated
415: htmlTags.put(HTML.Tag.KBD, new XFormatTagHandler(
416: HTML.Tag.KBD));
417: htmlTags.put(HTML.Tag.LI, new XDataTagHandler(HTML.Tag.LI));
418: htmlTags.put(HTML.Tag.LINK, new XDataTagHandler(
419: HTML.Tag.LINK));
420: htmlTags.put(HTML.Tag.MAP, new Map());
421: // htmlTags.put( HTML.Tag.MENU, new Integer( XHtmlBuilder.MENU )); // Deprecated
422: htmlTags.put(HTML.Tag.META, new XDataTagHandler(
423: HTML.Tag.META));
424: // htmlTags.put( HTML.Tag.NOFRAMES, new Integer( XHtmlBuilder.NOFRAMES ));
425: htmlTags.put(HTML.Tag.OBJECT, new XFormatTagHandler(
426: HTML.Tag.OBJECT));
427: htmlTags.put(HTML.Tag.OL, new XDataTagHandler(HTML.Tag.OL));
428: htmlTags.put(HTML.Tag.OPTION, new XDataTagHandler(
429: HTML.Tag.OPTION));
430: htmlTags.put(HTML.Tag.P, new P());
431: htmlTags.put(HTML.Tag.PARAM, new XDataTagHandler(
432: HTML.Tag.PARAM));
433: htmlTags.put(HTML.Tag.PRE, new XFormatTagHandler(
434: HTML.Tag.PRE));
435: htmlTags.put(HTML.Tag.SAMP, new XFormatTagHandler(
436: HTML.Tag.SAMP));
437: htmlTags.put(HTML.Tag.SCRIPT, new Script());
438: htmlTags.put(HTML.Tag.SELECT, new Select());
439: htmlTags.put(HTML.Tag.SMALL, new XFormatTagHandler(
440: HTML.Tag.SMALL));
441: htmlTags.put(HTML.Tag.SPAN, new Span());
442: htmlTags.put(HTML.Tag.STRIKE, new XFormatTagHandler(
443: HTML.Tag.STRIKE));
444: htmlTags.put(HTML.Tag.S, new XFormatTagHandler(HTML.Tag.S));
445: htmlTags.put(HTML.Tag.STRONG, new XFormatTagHandler(
446: HTML.Tag.STRONG));
447: htmlTags.put(HTML.Tag.STYLE, new Style());
448: htmlTags.put(HTML.Tag.SUB, new XFormatTagHandler(
449: HTML.Tag.SUB));
450: htmlTags.put(HTML.Tag.SUP, new XFormatTagHandler(
451: HTML.Tag.SUP));
452: htmlTags.put(HTML.Tag.TABLE, new Table());
453: htmlTags.put(HTML.Tag.TD, new Td(HTML.Tag.TD));
454: htmlTags.put(HTML.Tag.TEXTAREA, new TextArea());
455: htmlTags.put(HTML.Tag.TH, new Td(HTML.Tag.TH));
456: htmlTags.put(HTML.Tag.TITLE, new XDataTagHandler(
457: HTML.Tag.TITLE));
458: htmlTags.put(HTML.Tag.TR, new Td(HTML.Tag.TR));
459: htmlTags.put(HTML.Tag.TT,
460: new XFormatTagHandler(HTML.Tag.TT));
461: htmlTags.put(HTML.Tag.U, new XFormatTagHandler(HTML.Tag.U));
462: htmlTags.put(HTML.Tag.UL, new XDataTagHandler(HTML.Tag.UL));
463: htmlTags.put(HTML.Tag.VAR,
464: new XDataTagHandler(HTML.Tag.VAR));
465: }
466: }
467:
468: public XHtmlTagHandler getTagHandler(HTML.Tag t) {
469: XHtmlTagHandler tagHandler = (XHtmlTagHandler) htmlTags.get(t);
470: if (tagHandler != null)
471: tagHandler = tagHandler.newInstance(this , lastHandler);
472: else {
473: tagHandler = new XDefaultTagHandler(t);
474: tagHandler.setParent(lastHandler);
475: }
476:
477: return tagHandler;
478: }
479:
480: // End tag handling ----------------------------------------------------------
481:
482: // Start style handling ------------------------------------------------------
483: /**
484: * Set the class of an element
485: * @param c the component affected by the style
486: * @param className the name of the class
487: */
488: public void setClass(Component c, String className) {
489: XHtmlStyle style = styles.get(className);
490: if (style == null) {
491: style = new XHtmlStyle(className);
492: styles.put(className, style);
493: }
494:
495: // Apply the style
496: c.setForeground(style.getForeground(false));
497: c.setBackground(style.getBackground(false));
498: c.setFont(style.getFont());
499: }
500:
501: /**
502: * Setup a new style or styles by parsing the stylesheet
503: * @param styleInfo the full style information
504: */
505: public void setupStyle(String styleInfo) {
506: StringTokenizer st = new StringTokenizer(styleInfo, "}");
507: while (st.hasMoreTokens()) {
508: String styleStr = st.nextToken().trim();
509: if (styleStr.length() == 0)
510: continue;
511:
512: int pos = styleStr.indexOf('{');
513: String className = styleStr.substring(0, pos - 1).trim();
514: if (className.charAt(0) == '.')
515: className = className.substring(1);
516: String styleAttribs = styleStr.substring(pos + 1);
517:
518: XHtmlStyle style = styles.get(className);
519: if (style == null) {
520: style = new XHtmlStyle(className);
521: styles.put(className, style);
522: }
523:
524: style.parse(styleAttribs);
525: }
526: }
527:
528: /**
529: * Get a color from a color specification
530: * @param the html color spec
531: * @return the Color object
532: */
533: public static Color getColor(String colorSpec) {
534: String spec = colorSpec.toLowerCase();
535: if (spec.equals("black"))
536: return Color.black;
537: else if (spec.equals("silver"))
538: return new Color(192, 192, 192);
539: else if (spec.equals("gray"))
540: return new Color(128, 128, 128);
541: else if (spec.equals("white"))
542: return Color.white;
543: else if (spec.equals("maroon"))
544: return new Color(128, 0, 0);
545: else if (spec.equals("red"))
546: return Color.red;
547: else if (spec.equals("purple"))
548: return new Color(128, 0, 128);
549: else if (spec.equals("fuchsia"))
550: return new Color(255, 0, 255);
551: else if (spec.equals("green"))
552: return new Color(0, 128, 0);
553: else if (spec.equals("lime"))
554: return new Color(0, 255, 0);
555: else if (spec.equals("olive"))
556: return new Color(128, 128, 0);
557: else if (spec.equals("yellow"))
558: return Color.yellow;
559: else if (spec.equals("navy"))
560: return new Color(0, 0, 128);
561: else if (spec.equals("blue"))
562: return Color.blue;
563: else if (spec.equals("teal"))
564: return new Color(0, 128, 128);
565: else if (spec.equals("aqua"))
566: return new Color(0, 255, 255);
567: else if (spec.startsWith("rgb")) {
568: spec = spec.substring(spec.indexOf('(') + 1);
569: int pos = spec.indexOf(",");
570: int r = Integer.parseInt(spec.substring(0, pos++).trim());
571: int pos2 = spec.indexOf(",", pos);
572: int g = Integer.parseInt(spec.substring(pos, pos2).trim());
573: int b = Integer.parseInt(spec.substring(++pos2,
574: spec.indexOf(")")).trim());
575: return new Color(r, g, b);
576: } else {
577: int r = Integer.parseInt(spec.substring(1, 3), 16);
578: int g = Integer.parseInt(spec.substring(3, 5), 16);
579: int b = Integer.parseInt(spec.substring(5, 7), 16);
580: return new Color(r, g, b);
581: }
582: }
583:
584: /**
585: * Get a SwingConstant for an attribute
586: * @param the attribute name as a string
587: * @return the constant
588: */
589: public static int getSwingConstant(String name) {
590: String uName = name.toUpperCase();
591:
592: if (swingConstants == null) {
593: swingConstants = new Hashtable();
594: swingConstants.put("CENTER", new Integer(
595: SwingConstants.CENTER));
596: swingConstants.put("TOP", new Integer(SwingConstants.TOP));
597: swingConstants
598: .put("LEFT", new Integer(SwingConstants.LEFT));
599: swingConstants.put("BOTTOM", new Integer(
600: SwingConstants.BOTTOM));
601: swingConstants.put("RIGHT", new Integer(
602: SwingConstants.RIGHT));
603: swingConstants.put("NORTH", new Integer(
604: SwingConstants.NORTH));
605: swingConstants.put("NORTH_EAST", new Integer(
606: SwingConstants.NORTH_EAST));
607: swingConstants
608: .put("EAST", new Integer(SwingConstants.EAST));
609: swingConstants.put("SOUTH_EAST", new Integer(
610: SwingConstants.SOUTH_EAST));
611: swingConstants.put("SOUTH", new Integer(
612: SwingConstants.SOUTH));
613: swingConstants.put("SOUTH_WEST", new Integer(
614: SwingConstants.SOUTH_WEST));
615: swingConstants
616: .put("WEST", new Integer(SwingConstants.WEST));
617: swingConstants.put("NORTH_WEST", new Integer(
618: SwingConstants.NORTH_WEST));
619: swingConstants.put("HORIZONTAL", new Integer(
620: SwingConstants.HORIZONTAL));
621: swingConstants.put("VERTICAL", new Integer(
622: SwingConstants.VERTICAL));
623: swingConstants.put("LEADING", new Integer(
624: SwingConstants.LEADING));
625: swingConstants.put("TRAILING", new Integer(
626: SwingConstants.TRAILING));
627: swingConstants
628: .put("NEXT", new Integer(SwingConstants.NEXT));
629: swingConstants.put("PREVIOUS", new Integer(
630: SwingConstants.PREVIOUS));
631: }
632:
633: return ((Integer) swingConstants.get(uName)).intValue();
634: }
635:
636: // End style handling --------------------------------------------------------
637:
638: private class XHtmlBuilderParserCallback extends
639: HTMLEditorKit.ParserCallback {
640: private HTML.Tag lastTag;
641: private XHtmlBuilder builder;
642:
643: public XHtmlBuilderParserCallback(XHtmlBuilder htmlBuilder) {
644: builder = htmlBuilder;
645: }
646:
647: public void handleText(char[] data, int pos) {
648: super .handleText(data, pos);
649: lastHandler.processText(componentFactory, new String(data));
650: }
651:
652: public void handleComment(char[] data, int pos) {
653: super .handleComment(data, pos);
654: lastHandler.processComment(new String(data));
655: }
656:
657: public void handleStartTag(HTML.Tag t, MutableAttributeSet a,
658: int pos) {
659: super .handleStartTag(t, a, pos);
660: lastTag = t;
661: XHtmlTagHandler tagHandler = getTagHandler(t);
662: tagHandler.startProcessing(builder, componentFactory, a);
663: lastHandler = tagHandler;
664: }
665:
666: public void handleEndTag(HTML.Tag t, int pos) {
667: super .handleEndTag(t, pos);
668: if (lastHandler != null) {
669: lastHandler.endProcessing(componentFactory);
670: XHtmlTagHandler parentHandler = lastHandler.getParent();
671: if (parentHandler != null)
672: parentHandler.addChild(lastHandler);
673: lastHandler = parentHandler;
674: }
675: }
676:
677: /**
678: * Handle a closed/simple tag by running the start and then the end proceesing
679: * @param t the HTML tag
680: * @param a the attribute set
681: * @param pos the position within the file
682: */
683: public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a,
684: int pos) {
685: super .handleSimpleTag(t, a, pos);
686:
687: // Start tag processing
688: lastTag = t;
689: XHtmlTagHandler tagHandler = getTagHandler(t);
690: tagHandler.startProcessing(builder, componentFactory, a);
691: lastHandler = tagHandler;
692:
693: // End tag processing
694: if (lastHandler != null) {
695: lastHandler.endProcessing(componentFactory);
696: XHtmlTagHandler parentHandler = lastHandler.getParent();
697: if (parentHandler != null)
698: parentHandler.addChild(lastHandler);
699: lastHandler = parentHandler;
700: }
701: }
702:
703: public void handleError(String errorMsg, int pos) {
704: super .handleError(errorMsg, pos);
705: }
706: }
707:
708: /**
709: * Get the page loader type - a unique name identifying the loader
710: * @return "html"
711: */
712: public String getType() {
713: return "html";
714: }
715: }
|