Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH . All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: s IT Solutions AT Spardat GmbH - initial API and implementation /
 
 
 /*
  * Created on : 04.2003
  * Created by : s3595
  */
 package at.spardat.xma.boot.comp.data;
 
 
 
 
class: XMAAppParser

Author(s):
s3595 Chris Sch�fer (CGS)

Version:
$Id: XMAAppParser.java 6993 2010-12-14 14:23:42Z webok $
 
 public class XMAAppParser extends DefaultHandler implements DTDHandler {
 
     private static String DEFAULT_DTD               = "xma-app.dtd"//$NON-NLS-1$
     private static String DEFAULT_DTD_PACKAGE       = "at/spardat/xma/boot/comp/"// "at.spardat.xma.boot.comp."; //$NON-NLS-1$
     private static String DTD_PUBLIC_ID             = "-//Spardat//IMC-XMA//EN";     //$NON-NLS-1$
 
    
validate xml
 
     private static boolean validation_           = false;

    
parser
 
     private javax.xml.parsers.SAXParser parser_;

    
logger
 
     private ILogger log_;

    
parsed application
 
     private XMAApp xmaApp_;

    
current component
 
     private XMAComponent currentComponent;

    
current plugin
 
     private XMAPluginImpl currentPluginImpl_;

    
current SWT description
 
     private XMASWTDescription currentSWTDescription;

    
supported tags (must be sorted)
 
     private static final String[] tags = {
         .,
         .,
         .,
         .,
         .,
         .,
         .,
         . };
 
     private static final int TAG_COMPONENT    = 0;
     private static final int TAG_PLUGIN_IMPL  = 1;
     private static final int TAG_PLUGIN_SPEC  = 2;
     private static final int TAG_PREINSTALL   = 3;
     private static final int TAG_RESOURCE     = 4;
     private static final int TAG_RESOURCELINK = 5;
     private static final int TAG_SWT_DESCRIPTION = 6;
     private static final int TAG_APP          = 7;
 
     private int countUnknownTag = 0;
 
     public XMAAppParserILogger log_boolean bValidation) {
         initlog_bValidation);
     }

    
constructor
 
    public XMAAppParserILogger log_ ) {
        init(log_false );
    }
    private void initILogger log_boolean bValidation) {
         = bValidation;
        iflog_ == null ) throw new NullPointerException();
        this. = log_;
         = null;
         = null;
         = null;
        try {
            initParser();
        } catch (Exception e) {
            log_.log(.,"XML parser configuration error: ",e);
            throw new RuntimeException("XML parser configuration error: ",e);
        }
    }

    
    protected void initParser() throws ParserConfigurationExceptionSAXException {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setValidating();
         = spf.newSAXParser();
        XMLReader xmlreader = .getXMLReader();
        xmlreader.setDTDHandler(this);
        xmlreader.setEntityResolverthis );
    }

    
get the dtd out of the classloader. on development, if class-directory is seperated, build should copy the dtd into the classes dir.
    public InputSource resolveEntity(String publicIdString systemId) {
        .log(."resolvedEntity: \"{0}\" with sysid: \"{1}\""new Object[] {publicId,systemId} ); //$NON-NLS-1$
        if (..equals(publicId) ) {
          String strClass = . + .;
          InputStream in  = getClass().getClassLoader().getResourceAsStreamstrClass );
          return new InputSourcein );
        }
        return null;
    }

    
parse is the main method for parsing configuration files

Parameters:
is the input stream to the data to parse
Throws:
java.io.IOException
org.xml.sax.SAXException
     public XMAApp parseInputStream is ) throws IOExceptionSAXException {
        try {
           .parsenew InputSource(is), this );
           XMAApp ret   = this.;
           this. = null;
           return ret;
        } catchIOException e ) {
            .log(."parser error: "e); //$NON-NLS-1$
            throw e;
        } catch (SAXException e) {
            .log(."parser error: "e); //$NON-NLS-1$
            throw e;
        }
      }

    
parse is the main method for parsing configuration files

Parameters:
strURI Is a String parameter. It is used as a URI to the XML-file location.
     public XMAApp parseString strURI ) throws IOExceptionSAXException {
        try {
           .parsestrURIthis );
           XMAApp ret = this.;
           this. = null;
           return ret;
        } catchIOException e ) {
            .log(."parser error: "e); //$NON-NLS-1$
            throw e;
        } catch (SAXException e) {
            .log(."parser error: "e); //$NON-NLS-1$
            throw e;
        }
      }

    
    public void startDocument() throws SAXException { }

    
    public void endDocument() throws SAXException { }

    
    public void startElement(String nsURIString localNameString qNameAttributes attrs)
        throws SAXException {
      this.switchElementHandlernsURI,localName,qName,attrs);
    }

    
    public void endElement(String nsURIString localNameString qName)
        throws SAXException {
      this.switchElementHandlernsURI,localNameqNamenull);
    }

    
see element dispatcher
    protected void switchElementHandler(String nsURIString localNameString qNameAttributes attrs)
        throws SAXException {
        switch (Arrays.binarySearch(qName )) {
            case :               handleApp(attrs); break;
            case :         handleComponent(attrs); break;
            case :       handlePluginImpl(attrs); break;
            case :       handlePluginSpec(attrs); break;
            case :          handleResource(attrs); break;
            case :      handleResLink(attrs); break;
            case :   handleSWTDescription(attrs); break;
            case :        handlePreinstall(attrs); break;
            default:   	            	handleUnknownTag(attrs);
        }
    }
    protected void handleUnknownTag(Attributes attrs)
    throws SAXException {
      if (attrs != null) {
    	  ++;
      } else { /* tag close */
    	  --;
      }
  }

    

Parameters:
attrs Attributes to this element
Throws:
org.xml.sax.SAXException
    protected void handleApp(Attributes attrs)
      throws SAXException {
        if(isUnknownTag()) return;
        if (attrs != null) {
          this. = new XMAApp( );
        } else { /* tag close */
        }
    }

    
handle component parsing
    protected void handleComponent(Attributes attrsthrows SAXException {
        if(isUnknownTag()) return;
        if (attrs != null) {
            String name = attrs.getValue. );
            String impl = attrs.getValue. );
            if == null )
              throw new SAXException"parser error"); //$NON-NLS-1$
             = new XMAComponent(nameimpl);
            .setApplication();
            .addComponent();
        } else { /* tag close */
           this. = null ;
        }
    }

    
e.g.: <jar href="test.jar" main="true" download="eager" />

Parameters:
attrs Attributes to this element
Throws:
org.xml.sax.SAXException
    protected void handleResource(Attributes attrsthrows SAXException {
       if(isUnknownTag()) return;
       if (attrs != null) {
         Locale locale   = null;
         Version version = null;
         String strHref     = attrs.getValue. );
         String strVersion  = attrs.getValue. );
         String strType     = attrs.getValue. );
         String strLocale   = attrs.getValue. );
         String strShared     = attrs.getValue. );
         String strFdmLoad  = attrs.getValue(. );
         String os     = attrs.getValue.);
         String arch  = attrs.getValue(.);
         XMAResource.TypeID tid = XMAResource.TypeID.getID(strType);
         ifstrLocale != null ) {
            locale = Util.getInstance().getLocale(strLocale);
         }
         if(strVersion == null ) {
            throw new SAXException"missing version"); //$NON-NLS-1$
         } else {
            version = new Version(strVersion);
         }
         boolean shared=false;
         if("true".equalsIgnoreCase(strShared)) {
             shared=true;
         }
         XMAResource res = new XMAResourcestrHrefversiontidlocaleshared,osarch);
         if("true".equalsIgnoreCase(strFdmLoad)) {
             res.setFdmload(true);
         }
         if != null ) {
            .addResource(res);
         } else if != null ) {
            .addResource(res);
         } else if( != null) {
            .addResource(res);
         } else
            .addResourceres );
       }// end if
    }

    
handle parsing of resource links
    protected void handleResLink(Attributes attrs)
       throws SAXException {
         if(isUnknownTag()) return;
         if (attrs != null) {
             String strHref     = attrs.getValue. );
             XMAResourceLink rl = new XMAResourceLinkstrHref );
             if != null ) {
                .addResourceLink(rl);
             } else if != null ) {
                .addResourceLinkrl );
             } else
                throw new SAXException"parser error. resource link not in component or plugin"); //$NON-NLS-1$
         }
     }

    
handles parsing of plug in impl info
    protected void handlePluginImpl(Attributes attrs)
       throws SAXException {
       if(isUnknownTag()) return;
       if (attrs != null) {
           String implClient      = attrs.getValue. );
           String implServer      = attrs.getValue. );
           String strImplements   = attrs.getValue. );
           XMAPluginImpl pi = new XMAPluginImplimplClientimplServerstrImplements);
           .addPluginImpl(pi);
            = pi;
       } else {
            = null;
       }
     }

    
handles parsing of tag plugin spec
    protected void handlePluginSpec(Attributes attrs)
       throws SAXException {
        if(isUnknownTag()) return;
	    if (attrs != null) {
	         String strRequires = attrs.getValue. );
	         XMAPluginSpec pis  = new XMAPluginSpecstrRequires );
	         .addPluginSpecpis );
	    }
     }

    
handles parsing of tag swt-description
    protected void handleSWTDescription(Attributes attrsthrows SAXException {
        if(isUnknownTag()) return;
        if(attrs!=null) {
            XMASWTDescription swtDescription = new XMASWTDescription();
            if(.getSwtDescription()!=null) {
                throw new SAXException("parser error. duplicate <"+.+"> tag detected. Only one <"+.+"> tag is allowed.");
            }
            .setSwtDescription(swtDescription);
            =swtDescription;
        } else {
            =null;
        }
    }

    
handles parsing of tag preinstall
    protected void handlePreinstall(Attributes attrsthrows SAXException {
        if(isUnknownTag()) return;
        if(attrs!=null) {
            String version = attrs.getValue(.);
            SWTPreinstall preinstall = new SWTPreinstall(version);
            if(!=null) {
                .addPreinstall(preinstall);
            } else {
                throw new SAXException("parser error. preinstall not in <"+.+"> tag");
            }
        }
    }

    
This is a helper method to make sure a codebase string will work as a codebase URL by forcing a "/" on the end of the string.
    protected static final String fixCodebaseString(String s) {
        if (s == null || s.endsWith("/")) { //$NON-NLS-1$
            return s; }
        return (s + "/"); //$NON-NLS-1$
    }

    
    public void fatalError(SAXParseException e)
      throws SAXParseException {
          .log(."XML parse error at line: {0} with public-id: \"{1}\""new Object[] { new Integer(e.getLineNumber()),e.getPublicId() } ); //$NON-NLS-1$
        throw (SAXParseExceptione;
     }

    
    public void warning(SAXParseException e) {
        .log(."XML parse-warning at line: {0} with public-id: \"{1}\""new Object[] { new Integer(e.getLineNumber()),e.getPublicId() } ); //$NON-NLS-1$
     }

    
    public void error(SAXParseException e)
      throws SAXParseException {
          .log(."XML parse error at line: {0} with public-id: \"{1}\""new Object[] { new Integer(e.getLineNumber()),e.getPublicId() } ); //$NON-NLS-1$
      throw (SAXParseExceptione;
     }

    
Returns true if the parser stands in an unknown tag. Needed as an unknown tag and anything in it is ignored.

Returns:
true if the parser stands in an unknown tag, otherwise false
    private boolean isUnknownTag(){
    	return  > 0;
    }
New to GrepCode? Check out our FAQ X