Package net.xoetrope.builder.w3c.html

Source Code of net.xoetrope.builder.w3c.html.XHtmlBuilder

package net.xoetrope.builder.w3c.html;

import net.xoetrope.builder.w3c.html.tags.A;
import net.xoetrope.builder.w3c.html.tags.Address;
import net.xoetrope.builder.w3c.html.tags.BaseFont;
import net.xoetrope.builder.w3c.html.tags.BlockQuote;
import net.xoetrope.builder.w3c.html.tags.Br;
import net.xoetrope.builder.w3c.html.tags.Col;
import net.xoetrope.builder.w3c.html.tags.Div;
import net.xoetrope.builder.w3c.html.tags.Font;
import net.xoetrope.builder.w3c.html.tags.Form;
import net.xoetrope.builder.w3c.html.tags.Hr;
import net.xoetrope.builder.w3c.html.tags.XHeadingTag;
import net.xoetrope.builder.w3c.html.tags.Img;
import net.xoetrope.builder.w3c.html.tags.Input;
import net.xoetrope.builder.w3c.html.tags.Map;
import net.xoetrope.builder.w3c.html.tags.P;
import net.xoetrope.builder.w3c.html.tags.Script;
import net.xoetrope.builder.w3c.html.tags.Select;
import net.xoetrope.builder.w3c.html.tags.Span;
import net.xoetrope.builder.w3c.html.tags.Style;
import net.xoetrope.builder.w3c.html.tags.Table;
import net.xoetrope.builder.w3c.html.tags.Td;
import net.xoetrope.builder.w3c.html.tags.TextArea;
import net.xoetrope.builder.w3c.html.tags.XDataTagHandler;
import net.xoetrope.builder.w3c.html.tags.XDefaultTagHandler;
import net.xoetrope.builder.w3c.html.tags.XFormatTagHandler;
import net.xoetrope.builder.w3c.html.tags.XObjectTagHandler;
import net.xoetrope.builder.w3c.html.tags.XHtmlTagHandler;
import net.xoetrope.builder.w3c.html.tags.XStructuralTagHandler;
import java.awt.Color;
import java.awt.Component;
import java.io.Reader;
import java.util.Enumeration;
import java.util.Hashtable;
import java.awt.Container;

import net.xoetrope.builder.XuiBuilder;
import net.xoetrope.debug.DebugLogger;
import net.xoetrope.xui.XPage;
import net.xoetrope.xui.PageSupport;
import net.xoetrope.xui.build.BuildProperties;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.StringTokenizer;
import javax.swing.SwingConstants;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import net.xoetrope.xui.XComponentConstructor;
import net.xoetrope.xui.XComponentFactory;
import net.xoetrope.xui.XProject;
import net.xoetrope.xui.XProjectManager;

/**
* A build for W3C HTML forms/pages. The builder can create XUI pages on-the-fly
* or it can be used to convert and save an html page to a xui format.
* <p> Copyright (c) Xoetrope Ltd., 2002-2006</p>
* <p> $Revision: 1.7 $</p>
* <p> License: see License.txt</p>
*/
public class XHtmlBuilder extends XuiBuilder
{
  protected static boolean debugLayout = false;
 
  private static Hashtable swingConstants;
  private static Hashtable< Object, XHtmlTagHandler > htmlTags = null;
  private Hashtable< String, XHtmlStyle > styles;

  protected String packageName;

  protected String selectStyle = XPage.RADIO;
  protected XHtmlComponentFactory htmlFactory;
  protected XHtmlTagHandler lastHandler;
 
  protected static URL documentUrl;

  /**
   * Create a new builder
   * @param project the current xui project
   * @param factory the component factory
   */
  public XHtmlBuilder( XProject project )
  {
    super( project, XPage.XUI_SWING_PACKAGE );
    setupHtmlTags();
    styles = new Hashtable< String, XHtmlStyle >();

    Hashtable componentFactories = XComponentFactory.getFactories();
    Enumeration enumeration = componentFactories.keys();
    while ( enumeration.hasMoreElements() ) {
      XComponentConstructor factory = ( XComponentConstructor )componentFactories.get( enumeration.nextElement() );
      if ( factory instanceof XHtmlComponentFactory ) {
        htmlFactory = (XHtmlComponentFactory)factory;
        break;
      }
    }
  }
  /**
   * Is a debug layout to be used?
   * @return true if the debug layout is to be used
   */
  public static boolean isDebugLayout()
  {
    return debugLayout;
  }

  /**
   * Set the debug layout flag
   * @param state true if the debug layout is to be used
   */
  public static void setDebugLayout( boolean state )
  {
    debugLayout = state;
  }
 
  /**
   * Attempt to get an input stream from the specified path
   * @param urlStr the url string
   * @return the inputstream or null if the strean cannot be opened.
   */
  protected InputStream getUrlInputStream( String urlStr )
  {
    try {
      documentUrl = new URL( urlStr );

      // Open the connection and set the properties so that the input is loaded
      // from the server and even if a cached version is available.
      URLConnection conn = documentUrl.openConnection();
      conn.setDefaultUseCaches( false );
      conn.setIfModifiedSince( 0 );
      conn.setDoInput( true );
      conn.setDoOutput( false );
      conn.setUseCaches( false );
      conn.connect();
     
      InputStream is = conn.getInputStream();
      return is;
    }
    catch( MalformedURLException ex ) {     
    }
    catch( Exception ex ) {
      if ( BuildProperties.DEBUG )
        DebugLogger.logError( "Unable to load the page from the server: " + urlStr );
      //ex.printStackTrace();
    }
    return null;
  }

  /**
   * Loads an XPage via a reader obtained from the XProject (searches
   * the classpath). The pageName is assumed to be the name of an XML file. For
   * example if the pageName is 'welcome' then the 'welcome.xml' file is read as
   * a UTF8 encoded XML file (by default).
   * @param defPackageName the package or path to the page
   * @param pageName the page name or the name of the class implementing the page
   * @param include true if the page to be loaded is being included in another
   * page in which case any class attribute of the included page is ignored
   * @return the page
   */
  public PageSupport loadPage( String defPackageName, String pageName, boolean include )
  {
    packageName = defPackageName;

    Reader r = null;
    try {
      InputStream is = getUrlInputStream( pageName );
      if ( is != null )
        r = new BufferedReader( new InputStreamReader( is ));
      else {
        String fileName = pageName;
        if ( pageName.indexOf( ".htm" ) < 0 )
          fileName += ".html";
        r = currentProject.getBufferedReader( fileName, null );
        documentUrl = currentProject.findResource( fileName );
      }
    }
    catch ( Exception ex ) {
      if ( BuildProperties.DEBUG )
        DebugLogger.logError( "BUILDER", "File NOT found: " + pageName );
    }

    try {
      if (( r == null ) || !r.ready())
        return null;

      return readPage( r, pageName, ".html", include );
    }
    catch ( Exception e ) {
      if ( BuildProperties.DEBUG )
        DebugLogger.logError( "BUILDER", "File NOT found: " + pageName );
    }
    finally {
      if ( !include )
        rootPage = null;
    }
    return null;
  }
 
  /**
   * Read an XML description of the page and construct a new XPage. An instance
   * of the class specified by the class attribute is constructed or else an
   * instance of XPage if no class attribute is specified. The new page is
   * populated but is not yet added to its parent.
   * <br>
   * The startup file parameter 'DefaultClass' is used to obtain a default for
   * each page's class if a class parameter is not specified in the page's XML
   * <br>
   * The startup file parameter 'Validations' is used to obtain a default for
   * each page's set of validation rules
   *
   * @param reader a input stream from which to read the page
   * @param pageName the name of the page
   * @param ext the file extension
   * @param include the page to be loaded is being included in another page
   * @return the page
   */
  public PageSupport readPage( Reader reader, String pageName, String ext, boolean include )
  {
    try {
      setupPage( pageName, ext, include );

      HTMLEditorKit editorKit = new HTMLEditorKit()
      HTMLDocument doc = (HTMLDocument)editorKit.createDefaultDocument();
      HTMLEditorKit.ParserCallback callback = new XHtmlBuilderParserCallback( this );
      ParserDelegator pd = new ParserDelegator();
      doc.setPreservesUnknownTags( true );
      doc.setParser( pd );
      doc.setPreservesUnknownTags( true );
      pd.parse( reader, callback, true );
    }
    catch ( Exception e )
    {
      if ( BuildProperties.DEBUG )
        DebugLogger.logError( "Exception while reading the page: " + pageName );
      e.printStackTrace();
    }
    return page;
  }


  /**
   * Loads the page based on the contents of the page tag or by using default
   * values.
   *
   * @param pageName the name of the page
   * @param ext the file extension
   * @param include the page to be loaded is being included in another page
   */
  protected void setupPage( String pageName, String ext, boolean include )
  {
    XProjectManager.setCurrentProject( currentProject );
    String className = "net.xoetrope.xui.XPage";

    if ( !include ) {
      if ( ( className.indexOf( '.' ) <= 0 ) && ( packageName.length() > 1 ) )
        className = packageName + className;
      try {
        page = loadClass( className );
      }
      catch ( Exception e ) {
        if ( BuildProperties.DEBUG )
          DebugLogger.trace( "Unable to load the named class: " + className );
        page = new XPage();
      }
      setPageName( pageName );
      setPageExtension( ext );
      page.setLayout( new XHtmlFormLayout( this ));   

      componentFactory.setParentComponent( (Container)page );
    }

    rootPage = (XPage)page;
  }
 
  /**
   * Find a resource URl from a resource string, resolving relative resource
   * names in the process.
   * @param fileName the resource string
   * @return the resource url
   */
  public static URL findResource( String fileName )
  {
    try
    {
      return new URL( documentUrl, fileName );     
    }
    catch (Exception e)
    {
      return null;
   
  }
 
  /**
   * <p>Set a named attributes. The attributes are stored in a hashtable owned by
   * the page. Derived classes may access the hashtable directly but the
   * preferred method of access is the getAttribute method. Attributes are used
   * by the XuiBuilder class for component attributes other than those it handles
   * directly. The attributes can be thought of as component properties or extra
   * data and need not be used directly by the component.</p>
   * <p>
   * Attributes are stored using a key in the form attribName_compName or just
   * the attribName if compName is null.
   * </p>
   * @param attribName the attribute name
   * @param compName the component name or null if it is a page attribute
   * @param attribValue the attribute value
   */
  public void setComponentAttribute( String compName, String attribName, Object attribValue )
  {
    page.setAttribute( compName, attribName, attribValue );
  }
 
  /**
   * <p>Get a named attributes. The attributes are stored in a hashtable owned by
   * the page. Derived classes may access the hashtable directly but the
   * preferred method of access is the getAttribute method. Attributes are used
   * by the XuiBuilder class for component attributes other than those it handles
   * directly. The attributes can be thought of as component properties or extra
   * data and need not be used directly by the component.</p>
   * <p>
   * Attributes are stored using a key in the form attribName_compName or just
   * the attribName if compName is null.
   * </p>
   * @param attribName the attribute name
   * @param compName the component name or null if it is a page attribute
   */
  public Object getComponentAttribute( String compName, String attribName )
  {
    return page.getAttribute( compName, attribName );
  }
 
  // Start script handling -----------------------------------------------------
  /**
   * Add a new script function to the current class
   * @param componentName the name of the element being processed
   * @param eventName the name of the html event or null if a page event is being processed
   * @param methodName the name of the new method
   * @param script the contents of the method - the original javascript (assuming it is javascript)
   */
  public void addScript( String componentName, String eventName, String methodName, String script )
  {   
  }
 
  /**
   * Process the event specification, specified as an attribute of an HTML element
   * @param handler the tag handler
   * @param attribName the attribute name
   * @param attribValue the attribute value, containing the script
   */
  public void processEvent( XHtmlTagHandler handler, String attribName, String attribValue )
  {   
  }
  // End script handling -------------------------------------------------------
 
  // Start tag handling --------------------------------------------------------
  /**
   * Create a hashtable of tags and ids to speed processing of HTML
   */
  protected void setupHtmlTags()
  {
    if ( htmlTags == null ) {
      htmlTags = new Hashtable< Object, XHtmlTagHandler >();
      htmlTags.put( HTML.Tag.A, new A());
      htmlTags.put( HTML.Tag.ADDRESS, new Address());
      htmlTags.put( HTML.Tag.APPLET, new XObjectTagHandler( HTML.Tag.APPLET ));
      htmlTags.put( HTML.Tag.AREA, new XDataTagHandler( HTML.Tag.AREA ));
      htmlTags.put( HTML.Tag.B, new XFormatTagHandler( HTML.Tag.B ));
//      htmlTags.put( HTML.Tag.BASE, new Integer( XHtmlBuilder.BASE ));  // Deprecated
      htmlTags.put( HTML.Tag.BASEFONT, new BaseFont());
      htmlTags.put( HTML.Tag.BIG, new XFormatTagHandler( HTML.Tag.BIG ));
      htmlTags.put( HTML.Tag.BLOCKQUOTE, new BlockQuote());
      htmlTags.put( HTML.Tag.BODY, new XStructuralTagHandler( HTML.Tag.BODY ));
      htmlTags.put( HTML.Tag.BR, new Br());
      htmlTags.put( HTML.Tag.CAPTION, new XDataTagHandler( HTML.Tag.CAPTION ));
      htmlTags.put( HTML.Tag.CENTER, new Div( HTML.Tag.CENTER ));
      htmlTags.put( HTML.Tag.CITE, new XFormatTagHandler( HTML.Tag.CITE ));
      htmlTags.put( HTML.Tag.CODE, new XFormatTagHandler( HTML.Tag.CODE ));
      htmlTags.put( "col", new Col( "col" ));
      htmlTags.put( "colgroup", new Col( "colgroup" ));
      htmlTags.put( HTML.Tag.DD, new XDataTagHandler( HTML.Tag.DD ));
      htmlTags.put( HTML.Tag.DFN, new XFormatTagHandler( HTML.Tag.DFN ));
//      htmlTags.put( HTML.Tag.DIR, new Integer( XHtmlBuilder.DIR )); // Deprecated
      htmlTags.put( HTML.Tag.DIV, new Div( HTML.Tag.DIV ));
      htmlTags.put( HTML.Tag.DL, new XDataTagHandler( HTML.Tag.DL ));
      htmlTags.put( HTML.Tag.DT, new XDataTagHandler( HTML.Tag.DT ));
      htmlTags.put( HTML.Tag.EM, new XFormatTagHandler( HTML.Tag.EM ));
      htmlTags.put( HTML.Tag.FONT, new Font());
      htmlTags.put( HTML.Tag.FORM, new Form());
//      htmlTags.put( HTML.Tag.FRAME, new Integer( XHtmlBuilder.FRAME ));
//      htmlTags.put( HTML.Tag.FRAMESET, new Integer( XHtmlBuilder.FRAMESET ));
      htmlTags.put( HTML.Tag.H1, new XHeadingTag( HTML.Tag.H1 ));
      htmlTags.put( HTML.Tag.H2, new XHeadingTag( HTML.Tag.H2 ));
      htmlTags.put( HTML.Tag.H3, new XHeadingTag( HTML.Tag.H3 ));
      htmlTags.put( HTML.Tag.H4, new XHeadingTag( HTML.Tag.H4 ));
      htmlTags.put( HTML.Tag.H5, new XHeadingTag( HTML.Tag.H5 ));
      htmlTags.put( HTML.Tag.H6, new XHeadingTag( HTML.Tag.H6 ));
      htmlTags.put( HTML.Tag.HEAD, new XStructuralTagHandler( HTML.Tag.HEAD ));
      htmlTags.put( HTML.Tag.HR, new Hr());
      htmlTags.put( HTML.Tag.HTML, new XStructuralTagHandler( HTML.Tag.HTML ));
      htmlTags.put( HTML.Tag.I, new XFormatTagHandler( HTML.Tag.I ));
      htmlTags.put( HTML.Tag.IMG, new Img());
      htmlTags.put( HTML.Tag.INPUT, new Input());
//      htmlTags.put( HTML.Tag.ISINDEX, new Integer( XHtmlBuilder.ISINDEX )); // Deprecated
      htmlTags.put( HTML.Tag.KBD, new XFormatTagHandler( HTML.Tag.KBD ));
      htmlTags.put( HTML.Tag.LI, new XDataTagHandler( HTML.Tag.LI ));
      htmlTags.put( HTML.Tag.LINK, new XDataTagHandler( HTML.Tag.LINK ));
      htmlTags.put( HTML.Tag.MAP, new Map());
//      htmlTags.put( HTML.Tag.MENU, new Integer( XHtmlBuilder.MENU )); // Deprecated
      htmlTags.put( HTML.Tag.META, new XDataTagHandler( HTML.Tag.META ));
//      htmlTags.put( HTML.Tag.NOFRAMES, new Integer( XHtmlBuilder.NOFRAMES ));
      htmlTags.put( HTML.Tag.OBJECT, new XFormatTagHandler( HTML.Tag.OBJECT ));
      htmlTags.put( HTML.Tag.OL, new XDataTagHandler( HTML.Tag.OL ));
      htmlTags.put( HTML.Tag.OPTION, new XDataTagHandler( HTML.Tag.OPTION ));
      htmlTags.put( HTML.Tag.P, new P());
      htmlTags.put( HTML.Tag.PARAM, new XDataTagHandler( HTML.Tag.PARAM ));
      htmlTags.put( HTML.Tag.PRE, new XFormatTagHandler( HTML.Tag.PRE ));
      htmlTags.put( HTML.Tag.SAMP, new XFormatTagHandler( HTML.Tag.SAMP ));
      htmlTags.put( HTML.Tag.SCRIPT, new Script());
      htmlTags.put( HTML.Tag.SELECT, new Select());
      htmlTags.put( HTML.Tag.SMALL, new XFormatTagHandler( HTML.Tag.SMALL ));
      htmlTags.put( HTML.Tag.SPAN, new Span());
      htmlTags.put( HTML.Tag.STRIKE, new XFormatTagHandler( HTML.Tag.STRIKE ));
      htmlTags.put( HTML.Tag.S, new XFormatTagHandler( HTML.Tag.S ));
      htmlTags.put( HTML.Tag.STRONG, new XFormatTagHandler( HTML.Tag.STRONG ));
      htmlTags.put( HTML.Tag.STYLE, new Style());
      htmlTags.put( HTML.Tag.SUB, new XFormatTagHandler( HTML.Tag.SUB ));
      htmlTags.put( HTML.Tag.SUP, new XFormatTagHandler( HTML.Tag.SUP ));
      htmlTags.put( HTML.Tag.TABLE, new Table());
      htmlTags.put( HTML.Tag.TD, new Td( HTML.Tag.TD ));
      htmlTags.put( HTML.Tag.TEXTAREA, new TextArea());
      htmlTags.put( HTML.Tag.TH, new Td( HTML.Tag.TH ));
      htmlTags.put( HTML.Tag.TITLE, new XDataTagHandler( HTML.Tag.TITLE ));
      htmlTags.put( HTML.Tag.TR, new Td( HTML.Tag.TR ));
      htmlTags.put( HTML.Tag.TT, new XFormatTagHandler( HTML.Tag.TT ));
      htmlTags.put( HTML.Tag.U, new XFormatTagHandler( HTML.Tag.U ));
      htmlTags.put( HTML.Tag.UL, new XDataTagHandler( HTML.Tag.UL ));
      htmlTags.put( HTML.Tag.VAR, new XDataTagHandler( HTML.Tag.VAR ));
    }
  }
 
  public XHtmlTagHandler getTagHandler( HTML.Tag t )
  {
    XHtmlTagHandler tagHandler = (XHtmlTagHandler)htmlTags.get( t );
    if ( tagHandler != null )
      tagHandler = tagHandler.newInstance( this, lastHandler );
    else {
      tagHandler = new XDefaultTagHandler( t );
      tagHandler.setParent( lastHandler );
    }
   
    return tagHandler;
  }
  // End tag handling ----------------------------------------------------------
   
  // Start style handling ------------------------------------------------------
  /**
   * Set the class of an element
   * @param c the component affected by the style
   * @param className the name of the class
   */
  public void setClass( Component c, String className )
  {
    XHtmlStyle style = styles.get( className );
    if ( style == null ) {
      style = new XHtmlStyle( className );
      styles.put( className, style );
    }
   
    // Apply the style
    c.setForeground( style.getForeground( false ));
    c.setBackground( style.getBackground( false ));
    c.setFont( style.getFont());
  }          
 
  /**
   * Setup a new style or styles by parsing the stylesheet
   * @param styleInfo the full style information
   */
  public void setupStyle( String styleInfo )
  {   
    StringTokenizer st = new StringTokenizer( styleInfo, "}" );
    while( st.hasMoreTokens()) {
      String styleStr = st.nextToken().trim();
      if ( styleStr.length() == 0 )
        continue;
     
      int pos = styleStr.indexOf( '{' );
      String className = styleStr.substring( 0, pos-1 ).trim();     
      if ( className.charAt( 0 ) == '.' )
        className = className.substring( 1 );
      String styleAttribs = styleStr.substring( pos + 1 );
     
      XHtmlStyle style = styles.get( className );
      if ( style == null ) {
        style = new XHtmlStyle( className );
        styles.put( className, style );
      }
     
      style.parse( styleAttribs );
    }
  }
 
  /**
   * Get a color from a color specification
   * @param the html color spec
   * @return the Color object
   */
  public static Color getColor( String colorSpec )
  {
    String spec = colorSpec.toLowerCase();
    if ( spec.equals( "black" ))
      return Color.black;
    else if ( spec.equals( "silver" ))
      return new Color( 192, 192, 192 );
    else if ( spec.equals( "gray" ))
      return new Color( 128, 128, 128 );
    else if ( spec.equals( "white" ))
      return Color.white;
    else if ( spec.equals( "maroon" ))
      return new Color( 128, 0, 0 );
    else if ( spec.equals( "red" ))
      return Color.red;
    else if ( spec.equals( "purple" ))
      return new Color( 128, 0, 128 );
    else if ( spec.equals( "fuchsia" ))
      return new Color( 255, 0, 255 );
    else if ( spec.equals( "green" ))
      return new Color( 0, 128, 0 );
    else if ( spec.equals( "lime" ))
      return new Color( 0, 255, 0 );
    else if ( spec.equals( "olive" ))
      return new Color( 128, 128, 0 );
    else if ( spec.equals( "yellow" ))
      return Color.yellow;
    else if ( spec.equals( "navy" ))
      return new Color( 0, 0, 128 );
    else if ( spec.equals( "blue" ))
      return Color.blue;
    else if ( spec.equals( "teal" ))
      return new Color( 0, 128, 128 );
    else if ( spec.equals( "aqua" ))
      return new Color( 0, 255, 255 );
    else if ( spec.startsWith( "rgb" )) {
      spec = spec.substring( spec.indexOf( '(' ) + 1 );
      int pos = spec.indexOf( "," );
      int r = Integer.parseInt( spec.substring( 0, pos++ ).trim());
      int pos2 = spec.indexOf( ",", pos );
      int g = Integer.parseInt( spec.substring( pos, pos2 ).trim());
      int b = Integer.parseInt( spec.substring( ++pos2, spec.indexOf( ")" )).trim());
      return new Color( r, g, b );
    }
    else {
      int r = Integer.parseInt( spec.substring( 1, 3 ), 16 );
      int g = Integer.parseInt( spec.substring( 3, 5 ), 16 );
      int b = Integer.parseInt( spec.substring( 5, 7 ), 16 );
      return new Color( r, g, b );
    }
  }
 
  /**
   * Get a SwingConstant for an attribute
   * @param the attribute name as a string
   * @return the constant
   */
  public static int getSwingConstant( String name )
  {
    String uName = name.toUpperCase();
  
    if ( swingConstants == null ) {
      swingConstants = new Hashtable();
      swingConstants.put( "CENTER", new Integer( SwingConstants.CENTER ));
      swingConstants.put( "TOP", new Integer( SwingConstants.TOP ));
      swingConstants.put( "LEFT", new Integer( SwingConstants.LEFT ));
      swingConstants.put( "BOTTOM", new Integer( SwingConstants.BOTTOM ));
      swingConstants.put( "RIGHT", new Integer( SwingConstants.RIGHT ));
      swingConstants.put( "NORTH", new Integer( SwingConstants.NORTH ));
      swingConstants.put( "NORTH_EAST", new Integer( SwingConstants.NORTH_EAST ));
      swingConstants.put( "EAST", new Integer( SwingConstants.EAST ));
      swingConstants.put( "SOUTH_EAST", new Integer( SwingConstants.SOUTH_EAST ));
      swingConstants.put( "SOUTH", new Integer( SwingConstants.SOUTH ));
      swingConstants.put( "SOUTH_WEST", new Integer( SwingConstants.SOUTH_WEST ));
      swingConstants.put( "WEST", new Integer( SwingConstants.WEST ));
      swingConstants.put( "NORTH_WEST", new Integer( SwingConstants.NORTH_WEST ));
      swingConstants.put( "HORIZONTAL", new Integer( SwingConstants.HORIZONTAL ));
      swingConstants.put( "VERTICAL", new Integer( SwingConstants.VERTICAL ));
      swingConstants.put( "LEADING", new Integer( SwingConstants.LEADING ));
      swingConstants.put( "TRAILING", new Integer( SwingConstants.TRAILING ));
      swingConstants.put( "NEXT", new Integer( SwingConstants.NEXT ));
      swingConstants.put( "PREVIOUS", new Integer( SwingConstants.PREVIOUS ));
    }

    return ((Integer)swingConstants.get( uName )).intValue();
  }
  // End style handling --------------------------------------------------------
 
  private class XHtmlBuilderParserCallback extends HTMLEditorKit.ParserCallback
  {
    private HTML.Tag lastTag;
    private XHtmlBuilder builder;
   
    public XHtmlBuilderParserCallback( XHtmlBuilder htmlBuilder )
    {
      builder = htmlBuilder;
    }
   
    public void handleText( char[] data, int pos )
    {
      super.handleText( data, pos );
      lastHandler.processText( componentFactory, new String( data ));
    }

    public void handleComment( char[] data, int pos )
    {
      super.handleComment( data, pos );
      lastHandler.processComment( new String( data ));
    }

    public void handleStartTag( HTML.Tag t, MutableAttributeSet a, int pos )
    {
      super.handleStartTag( t, a, pos );
      lastTag = t;
      XHtmlTagHandler tagHandler = getTagHandler( t );
      tagHandler.startProcessing( builder, componentFactory, a );
      lastHandler = tagHandler;
    }

    public void handleEndTag( HTML.Tag t, int pos )
    {
      super.handleEndTag( t, pos );
      if ( lastHandler != null ) {
        lastHandler.endProcessing( componentFactory );
        XHtmlTagHandler parentHandler = lastHandler.getParent();
        if ( parentHandler != null )
          parentHandler.addChild( lastHandler );
        lastHandler = parentHandler;
      }
    }

    /**
     * Handle a closed/simple tag by running the start and then the end proceesing
     * @param t the HTML tag
     * @param a the attribute set
     * @param pos the position within the file
     */
    public void handleSimpleTag( HTML.Tag t, MutableAttributeSet a, int pos )
    {
      super.handleSimpleTag( t, a, pos );
     
      // Start tag processing
      lastTag = t;
      XHtmlTagHandler tagHandler = getTagHandler( t );
      tagHandler.startProcessing( builder, componentFactory, a );
      lastHandler = tagHandler;

      // End tag processing
      if ( lastHandler != null ) {
        lastHandler.endProcessing( componentFactory );
        XHtmlTagHandler parentHandler = lastHandler.getParent();
        if ( parentHandler != null )
          parentHandler.addChild( lastHandler );
        lastHandler = parentHandler;
      }
    }

    public void handleError( String errorMsg, int pos )
    {
      super.handleError( errorMsg, pos );
    }
  }
}
TOP

Related Classes of net.xoetrope.builder.w3c.html.XHtmlBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.