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 11295 2013-12-13 10:56:29Z dschwarz $
 
 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$
     private static String SIGNATURE_DTD             = "xmldsig-core-schema.dtd"//$NON-NLS-1$
     private static String DTD_SIGNATURE_PUBLIC_ID   = "-//W3C/XML Signatures";     //$NON-NLS-1$
 
    
running on server or on client
 
     private static boolean onServer_           = false;

    
XML signature validator
 
     protected XMLValidator xmlValidator;

    
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 int countUnknownTag = 0;
 
     public XMAAppParserILogger log_boolean bOnServerXMLValidator validator) {
         initlog_bOnServervalidator);
     }
 
     public XMAAppParserILogger log_boolean bOnServer) {
         initlog_bOnServernull);
     }
    
    
constructor
 
     public XMAAppParserILogger log_ ) {
         init(log_falsenull );
     }
 
     private void initILogger log_boolean bOnServerXMLValidator validator) {
          = bOnServer;
         iflog_ == null ) throw new NullPointerException();
         this. = log_;
          = null;
         = null;
         = null;
        if (validator != null) {
            validator.setEntityResolver(this);
        }
        this. = validator;
        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();
        // validate only on server
        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$
        String strClass;
        if (..equals(publicId) ) {
          strClass = . + .;
        } else if (..equals(publicId) ) {
          strClass = . + .;
        } else {
            return null;
        }
        InputStream in  = getClass().getClassLoader().getResourceAsStreamstrClass );
        return new InputSourcein );
    }

    
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 {
            // XML Signature validator uses DOM parser, but SAX parser is used for unmarshalling. To use the input stream
            // for both use cases, the input is stored in a temporary buffer for reuse.
            
            // FIXME Use DOM parser instead of SAX parser for unmarshalling
            
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] tmp = new byte[4096];
            int cnt = 0;
            while((cnt = is.read(tmp)) > 0) {
                bos.write(tmp, 0, cnt);
            }
            byte[] myArray = bos.toByteArray();
            
            .parsenew InputSource(new ByteArrayInputStream(myArray)), this );
           
            if (! &&  != null) {
                boolean valid;
                try {
                    valid = .validateSignature(new ByteArrayInputStream(myArray));
                } catch (Exception e) {
                    .log(.e.getMessage(), e);
                    valid = false;
                }
                if (!valid) {
                    throw new IOException("Unable to verify signature of application descriptor.");
                }
            }
            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 {
        FileInputStream is = null;
        try {
            is = new FileInputStream(strURI);
            return parseis );
        } finally {
            Util.close(isstrURI);
        }
    }


    
    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 attrsthrows SAXException {
        if (..equals(qName))        handleApp(attrs);
        else if (..equals(qName))         handleComponent(attrs);
        else if (..equals(qName))       handlePluginImpl(attrs);
        else if (..equals(qName))       handlePluginSpec(attrs);
        else if (..equals(qName))          handleResource(attrs);
        else if (..equals(qName))     handleResLink(attrs);
        else if (..equals(qName))   handleSWTDescription(attrs);
        else if (..equals(qName))        handlePreinstall(attrs);
        else if (..equals(qName))   handleSSLRestriction(attrs);
        else if (..equals(qName))       handleCertificate(attrs);
        else if (..equals(qName))           handleSubject(attrs);
        else if (..equals(qName))            handleIssuer(attrs);
        else if (..equals(qName))         handleSignature(attrs);
        else                                                 handleUnknownTag(attrs);
    }
    protected void handleUnknownTag(Attributes attrs)
    throws SAXException {
        if () {
            // ignore details
            return;
        }
      if (attrs != null) {
    	  ++;
      } else { /* tag close */
    	  --;
      }
  }
    boolean isSignature = false;
    protected void handleSignature(Attributes attrs) {
         = attrs != null;
    }
    private XMASSLRestriction currentSSLRestriction = null;
    boolean isSubject = false;
    boolean isIssuer = false;
    
    @Override
    public void characters(char[] chint startint lengththrows SAXException {
        if () {
            .setSubject(new String(chstartlength));
        } else if () {
            .setIssuer(new String(chstartlength));
        }
    }

    

Parameters:
attrs Attributes to this element
Throws:
org.xml.sax.SAXException
    protected void handleSSLRestriction(Attributes attrs)
      throws SAXException {
        if(isUnknownTag()) return;
        if (attrs != null) {
             = new XMASSLRestriction();
        } else {
             = null;
        }
    }

    
    

Parameters:
attrs Attributes to this element
Throws:
org.xml.sax.SAXException
    protected void handleCertificate(Attributes attrs)
            throws SAXException {
        if(isUnknownTag()) return;
    }
    
    
    

Parameters:
attrs Attributes to this element
Throws:
org.xml.sax.SAXException
    protected void handleSubject(Attributes attrs)
            throws SAXException {
        if(isUnknownTag()) return;
         = attrs != null;
    }

    

Parameters:
attrs Attributes to this element
Throws:
org.xml.sax.SAXException
    protected void handleIssuer(Attributes attrs)
            throws SAXException {
        if(isUnknownTag()) return;
         = attrs != null;
    }
    
    
    

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