Java Source Code: com.sun.xml.xsom.impl.parser.ParserContext


   1: package com.sun.xml.xsom.impl.parser;
   2: 
   3: import com.sun.xml.xsom.XSSchemaSet;
   4: import com.sun.xml.xsom.impl.ElementDecl;
   5: import com.sun.xml.xsom.impl.SchemaImpl;
   6: import com.sun.xml.xsom.impl.SchemaSetImpl;
   7: import com.sun.xml.xsom.parser.AnnotationParserFactory;
   8: import com.sun.xml.xsom.parser.XMLParser;
   9: import com.sun.xml.xsom.parser.XSOMParser;
  10: import org.xml.sax.EntityResolver;
  11: import org.xml.sax.ErrorHandler;
  12: import org.xml.sax.InputSource;
  13: import org.xml.sax.Locator;
  14: import org.xml.sax.SAXException;
  15: import org.xml.sax.SAXParseException;
  16: 
  17: import java.util.HashMap;
  18: import java.util.Iterator;
  19: import java.util.Map;
  20: import java.util.Vector;
  21: 
  22: /**
  23:  * Provides context information to be used by {@link NGCCRuntimeEx}s.
  24:  * 
  25:  * <p>
  26:  * This class does the actual processing for {@link XSOMParser},
  27:  * but to hide the details from the public API, this class in
  28:  * a different package.
  29:  * 
  30:  * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
  31:  */
  32:	  public class ParserContext {
  33:
  34:    /** SchemaSet to which a newly parsed schema is put in. */
  35:    public final SchemaSetImpl schemaSet = new SchemaSetImpl();
  36:
  37:    private final XSOMParser owner;
  38:
  39:    final XMLParser parser;
  40:
  41:
  42:    private final Vector<Patch> patchers = new Vector<Patch>();
  43:
  44:    /**
  45:     * Documents that are parsed already. Used to avoid cyclic inclusion/double
  46:     * inclusion of schemas. Set of {@link SchemaDocumentImpl}s.
  47:     *
  48:     * The actual data structure is map from {@link SchemaDocumentImpl} to itself,
  49:     * so that we can access the {@link SchemaDocumentImpl} itself.
  50:     */
  51:    public final Map<SchemaDocumentImpl, SchemaDocumentImpl> parsedDocuments = new HashMap<SchemaDocumentImpl, SchemaDocumentImpl>();
  52:
  53:
  54:	      public ParserContext( XSOMParser owner, XMLParser parser ) {
  55:        this.owner = owner;
  56:        this.parser = parser;
  57:
  58:	          try {
  59:            parse(new InputSource(ParserContext.class.getResource("datatypes.xsd").toExternalForm()));
  60:
  61:            SchemaImpl xs = (SchemaImpl)
  62:                schemaSet.getSchema("http://www.w3.org/2001/XMLSchema");
  63:            xs.addSimpleType(schemaSet.anySimpleType,true);
  64:            xs.addComplexType(schemaSet.anyType,true);
  65:        } catch( SAXException e ) {
  66:            // this must be a bug of XSOM
  67:            if(e.getException()!=null)
  68:                e.getException().printStackTrace();
  69:            else
  70:                e.printStackTrace();
  71:            throw new InternalError();
  72:        }
  73:    }
  74:
  75:	      public EntityResolver getEntityResolver() {
  76:        return owner.getEntityResolver();
  77:    }
  78:
  79:	      public AnnotationParserFactory getAnnotationParserFactory() {
  80:        return owner.getAnnotationParserFactory();
  81:    }
  82:
  83:    /**
  84:     * Parses a new XML Schema document.
  85:     */
  86:	      public void parse( InputSource source ) throws SAXException {
  87:        newNGCCRuntime().parseEntity(source,false,null,null);
  88:    }
  89:
  90:
  91:	      public XSSchemaSet getResult() throws SAXException {
  92:        // run all the patchers
  93:        for (Patch patcher : patchers)
  94:            patcher.run();
  95:        patchers.clear();
  96:
  97:        // build the element substitutability map
  98:        Iterator itr = schemaSet.iterateElementDecls();
  99:        while(itr.hasNext())
 100:            ((ElementDecl)itr.next()).updateSubstitutabilityMap();
 101:
 102:        if(hadError)    return null;
 103:        else            return schemaSet;
 104:    }
 105:
 106:	      public NGCCRuntimeEx newNGCCRuntime() {
 107:        return new NGCCRuntimeEx(this);
 108:    }
 109:
 110:
 111:
 112:    /** Once an error is detected, this flag is set to true. */
 113:    private boolean hadError = false;
 114:
 115:    /** Turns on the error flag. */
 116:    void setErrorFlag() { hadError=true; }
 117:
 118:    /**
 119:     * PatchManager implementation, which is accessible only from
 120:     * NGCCRuntimEx.
 121:     */
 122:	      final PatcherManager patcherManager = new PatcherManager() {
 123:	          public void addPatcher( Patch patch ) {
 124:            patchers.add(patch);
 125:        }
 126:	          public void reportError( String msg, Locator src ) throws SAXException {
 127:            // set a flag to true to avoid returning a corrupted object.
 128:            setErrorFlag();
 129:
 130:            SAXParseException e = new SAXParseException(msg,src);
 131:            if(errorHandler==null)
 132:                throw e;
 133:            else
 134:                errorHandler.error(e);
 135:        }
 136:    };
 137:
 138:    /**
 139:     * ErrorHandler proxy to turn on the hadError flag when an error
 140:     * is found.
 141:     */
 142:	      final ErrorHandler errorHandler = new ErrorHandler() {
 143:	          private ErrorHandler getErrorHandler() {
 144:            if( owner.getErrorHandler()==null )
 145:                return noopHandler;
 146:            else
 147:                return owner.getErrorHandler();
 148:        }
 149:
 150:	          public void warning(SAXParseException e) throws SAXException {
 151:            getErrorHandler().warning(e);
 152:        }
 153:
 154:	          public void error(SAXParseException e) throws SAXException {
 155:            setErrorFlag();
 156:            getErrorHandler().error(e);
 157:        }
 158:
 159:	          public void fatalError(SAXParseException e) throws SAXException {
 160:            setErrorFlag();
 161:            getErrorHandler().fatalError(e);
 162:        }
 163:    };
 164:
 165:    /**
 166:     * {@link ErrorHandler} that does nothing.
 167:     */
 168:	      final ErrorHandler noopHandler = new ErrorHandler() {
 169:	          public void warning(SAXParseException e) {
 170:        }
 171:	          public void error(SAXParseException e) {
 172:        }
 173:	          public void fatalError(SAXParseException e) {
 174:            setErrorFlag();
 175:        }
 176:    };
 177:}