/** * Is specified schema supported by this <code>SchemaFactory</code>? * * @param schemaLanguage Specifies the schema language which the returned <code>SchemaFactory * </code> will understand. <code>schemaLanguage</code> must specify a <a * href="#schemaLanguage">valid</a> schema language. * @return <code>true</code> if <code>SchemaFactory</code> supports <code>schemaLanguage</code>, * else <code>false</code>. * @throws NullPointerException If <code>schemaLanguage</code> is <code>null</code>. * @throws IllegalArgumentException If <code>schemaLanguage.length() == 0</code> or <code> * schemaLanguage</code> does not specify a <a href="#schemaLanguage">valid</a> schema * language. */ public boolean isSchemaLanguageSupported(String schemaLanguage) { if (schemaLanguage == null) { throw new NullPointerException( JAXPValidationMessageFormatter.formatMessage( Locale.getDefault(), "SchemaLanguageNull", null)); } if (schemaLanguage.length() == 0) { throw new IllegalArgumentException( JAXPValidationMessageFormatter.formatMessage( Locale.getDefault(), "SchemaLanguageLengthZero", null)); } // only W3C XML Schema 1.0 is supported return schemaLanguage.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI); }
public void setProperty(String name, Object object) throws SAXNotRecognizedException, SAXNotSupportedException { if (name == null) { throw new NullPointerException( JAXPValidationMessageFormatter.formatMessage( Locale.getDefault(), "ProperyNameNull", null)); } if (name.equals(SECURITY_MANAGER)) { fSecurityManager = (SecurityManager) object; fXMLSchemaLoader.setProperty(SECURITY_MANAGER, fSecurityManager); return; } else if (name.equals(XMLGRAMMAR_POOL)) { throw new SAXNotSupportedException( SAXMessageFormatter.formatMessage( Locale.getDefault(), "property-not-supported", new Object[] {name})); } try { fXMLSchemaLoader.setProperty(name, object); } catch (XMLConfigurationException e) { String identifier = e.getIdentifier(); if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { throw new SAXNotRecognizedException( SAXMessageFormatter.formatMessage( Locale.getDefault(), "property-not-recognized", new Object[] {identifier})); } else { throw new SAXNotSupportedException( SAXMessageFormatter.formatMessage( Locale.getDefault(), "property-not-supported", new Object[] {identifier})); } } }
public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { if (name == null) { throw new NullPointerException( JAXPValidationMessageFormatter.formatMessage( Locale.getDefault(), "FeatureNameNull", null)); } if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { fSecurityManager = value ? new SecurityManager() : null; fXMLSchemaLoader.setProperty(SECURITY_MANAGER, fSecurityManager); return; } try { fXMLSchemaLoader.setFeature(name, value); } catch (XMLConfigurationException e) { String identifier = e.getIdentifier(); if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { throw new SAXNotRecognizedException( SAXMessageFormatter.formatMessage( Locale.getDefault(), "feature-not-recognized", new Object[] {identifier})); } else { throw new SAXNotSupportedException( SAXMessageFormatter.formatMessage( Locale.getDefault(), "feature-not-supported", new Object[] {identifier})); } } }
/** * Throws a {@link IllegalStateException} if we are not in the startElement callback. the JAXP * API requires this for most of the public methods. */ private void checkState(boolean forElementInfo) { if (!(fInStartElement || (fInEndElement && forElementInfo))) { throw new IllegalStateException( JAXPValidationMessageFormatter.formatMessage( Locale.getDefault(), "TypeInfoProviderIllegalState", null)); } }
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException { if (name == null) { throw new NullPointerException( JAXPValidationMessageFormatter.formatMessage( fXMLSchemaLoader.getLocale(), "FeatureNameNull", null)); } if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { return (fSecurityManager != null); } try { return fXMLSchemaLoader.getFeature(name); } catch (XMLConfigurationException e) { String identifier = e.getIdentifier(); if (e.getType() == Status.NOT_RECOGNIZED) { throw new SAXNotRecognizedException( SAXMessageFormatter.formatMessage( fXMLSchemaLoader.getLocale(), "feature-not-recognized", new Object[] {identifier})); } else { throw new SAXNotSupportedException( SAXMessageFormatter.formatMessage( fXMLSchemaLoader.getLocale(), "feature-not-supported", new Object[] {identifier})); } } }
public void validate(Source source, Result result) throws SAXException, IOException { if (result instanceof StAXResult || result == null) { StAXSource staxSource = (StAXSource) source; StAXResult staxResult = (StAXResult) result; try { XMLStreamReader streamReader = staxSource.getXMLStreamReader(); if (streamReader != null) { // Hand off to XMLStreamReader helper. if (fStreamHelper == null) { fStreamHelper = new StreamHelper(); } fStreamHelper.validate(streamReader, staxResult); } else { // Hand off to XMLEventReader helper. if (fEventHelper == null) { fEventHelper = new EventHelper(); } fEventHelper.validate(staxSource.getXMLEventReader(), staxResult); } } catch (XMLStreamException e) { throw new SAXException(e); } catch (XMLParseException e) { throw Util.toSAXParseException(e); } catch (XNIException e) { throw Util.toSAXException(e); } finally { // Release references to application objects fCurrentEvent = null; fStAXLocationWrapper.setLocation(null); fXMLStreamReaderLocation.setXMLStreamReader(null); if (fStAXValidatorHandler != null) { fStAXValidatorHandler.setStAXResult(null); } } return; } throw new IllegalArgumentException( JAXPValidationMessageFormatter.formatMessage( fComponentManager.getLocale(), "SourceResultMismatch", new Object[] {source.getClass().getName(), result.getClass().getName()})); }
public void validate(Source source, Result result) throws SAXException, IOException { if (result == null || result instanceof StAXResult) { if (identityTransformer1 == null) { try { SAXTransformerFactory tf = fComponentManager.getFeature(Constants.ORACLE_FEATURE_SERVICE_MECHANISM) ? (SAXTransformerFactory) SAXTransformerFactory.newInstance() : (SAXTransformerFactory) TransformerFactory.newInstance( DEFAULT_TRANSFORMER_IMPL, StAXValidatorHelper.class.getClassLoader()); identityTransformer1 = tf.newTransformer(); identityTransformer2 = tf.newTransformerHandler(); } catch (TransformerConfigurationException e) { // this is impossible, but again better safe than sorry throw new TransformerFactoryConfigurationError(e); } } handler = new ValidatorHandlerImpl(fComponentManager); if (result != null) { handler.setContentHandler(identityTransformer2); identityTransformer2.setResult(result); } try { identityTransformer1.transform(source, new SAXResult(handler)); } catch (TransformerException e) { if (e.getException() instanceof SAXException) throw (SAXException) e.getException(); throw new SAXException(e); } finally { handler.setContentHandler(null); } return; } throw new IllegalArgumentException( JAXPValidationMessageFormatter.formatMessage( Locale.getDefault(), "SourceResultMismatch", new Object[] {source.getClass().getName(), result.getClass().getName()})); }
public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { if (name == null) { throw new NullPointerException( JAXPValidationMessageFormatter.formatMessage( fXMLSchemaLoader.getLocale(), "FeatureNameNull", null)); } if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { if (System.getSecurityManager() != null && (!value)) { throw new SAXNotSupportedException( SAXMessageFormatter.formatMessage(null, "jaxp-secureprocessing-feature", null)); } fSecurityManager = value ? new SecurityManager() : null; fXMLSchemaLoader.setProperty(SECURITY_MANAGER, fSecurityManager); fXMLSchemaLoader.setProperty(ACCESS_EXTERNAL_DTD, Constants.EXTERNAL_ACCESS_DEFAULT_FSP); fXMLSchemaLoader.setProperty(ACCESS_EXTERNAL_SCHEMA, Constants.EXTERNAL_ACCESS_DEFAULT_FSP); return; } else if (name.equals(Constants.ORACLE_FEATURE_SERVICE_MECHANISM)) { // in secure mode, let _useServicesMechanism be determined by the constructor if (System.getSecurityManager() != null) return; } try { fXMLSchemaLoader.setFeature(name, value); } catch (XMLConfigurationException e) { String identifier = e.getIdentifier(); if (e.getType() == Status.NOT_RECOGNIZED) { throw new SAXNotRecognizedException( SAXMessageFormatter.formatMessage( fXMLSchemaLoader.getLocale(), "feature-not-recognized", new Object[] {identifier})); } else { throw new SAXNotSupportedException( SAXMessageFormatter.formatMessage( fXMLSchemaLoader.getLocale(), "feature-not-supported", new Object[] {identifier})); } } }
final void validate(XMLEventReader reader, StAXResult result) throws SAXException, XMLStreamException { fCurrentEvent = reader.peek(); if (fCurrentEvent != null) { int eventType = fCurrentEvent.getEventType(); if (eventType != XMLStreamConstants.START_DOCUMENT && eventType != XMLStreamConstants.START_ELEMENT) { throw new SAXException( JAXPValidationMessageFormatter.formatMessage( fComponentManager.getLocale(), "StAXIllegalInitialState", null)); } setup(null, result, false); fSchemaValidator.startDocument(fStAXLocationWrapper, null, fNamespaceContext, null); loop: while (reader.hasNext()) { fCurrentEvent = reader.nextEvent(); eventType = fCurrentEvent.getEventType(); switch (eventType) { case XMLStreamConstants.START_ELEMENT: ++fDepth; StartElement start = fCurrentEvent.asStartElement(); fillQName(fElementQName, start.getName()); fillXMLAttributes(start); fillDeclaredPrefixes(start); fNamespaceContext.setNamespaceContext(start.getNamespaceContext()); fStAXLocationWrapper.setLocation(start.getLocation()); fSchemaValidator.startElement(fElementQName, fAttributes, null); break; case XMLStreamConstants.END_ELEMENT: EndElement end = fCurrentEvent.asEndElement(); fillQName(fElementQName, end.getName()); fillDeclaredPrefixes(end); fStAXLocationWrapper.setLocation(end.getLocation()); fSchemaValidator.endElement(fElementQName, null); if (--fDepth <= 0) { break loop; } break; case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.SPACE: if (fStAXValidatorHandler != null) { Characters chars = fCurrentEvent.asCharacters(); fStAXValidatorHandler.setIgnoringCharacters(true); sendCharactersToValidator(chars.getData()); fStAXValidatorHandler.setIgnoringCharacters(false); fStAXValidatorHandler.characters(chars); } else { sendCharactersToValidator(fCurrentEvent.asCharacters().getData()); } break; case XMLStreamConstants.CDATA: if (fStAXValidatorHandler != null) { Characters chars = fCurrentEvent.asCharacters(); fStAXValidatorHandler.setIgnoringCharacters(true); fSchemaValidator.startCDATA(null); sendCharactersToValidator(fCurrentEvent.asCharacters().getData()); fSchemaValidator.endCDATA(null); fStAXValidatorHandler.setIgnoringCharacters(false); fStAXValidatorHandler.cdata(chars); } else { fSchemaValidator.startCDATA(null); sendCharactersToValidator(fCurrentEvent.asCharacters().getData()); fSchemaValidator.endCDATA(null); } break; case XMLStreamConstants.START_DOCUMENT: ++fDepth; if (fStAXValidatorHandler != null) { fStAXValidatorHandler.startDocument((StartDocument) fCurrentEvent); } break; case XMLStreamConstants.END_DOCUMENT: if (fStAXValidatorHandler != null) { fStAXValidatorHandler.endDocument((EndDocument) fCurrentEvent); } break; case XMLStreamConstants.PROCESSING_INSTRUCTION: if (fStAXValidatorHandler != null) { fStAXValidatorHandler.processingInstruction((ProcessingInstruction) fCurrentEvent); } break; case XMLStreamConstants.COMMENT: if (fStAXValidatorHandler != null) { fStAXValidatorHandler.comment((Comment) fCurrentEvent); } break; case XMLStreamConstants.ENTITY_REFERENCE: if (fStAXValidatorHandler != null) { fStAXValidatorHandler.entityReference((EntityReference) fCurrentEvent); } break; case XMLStreamConstants.DTD: DTD dtd = (DTD) fCurrentEvent; processEntityDeclarations(dtd.getEntities()); if (fStAXValidatorHandler != null) { fStAXValidatorHandler.doctypeDecl(dtd); } break; } } fSchemaValidator.endDocument(null); } }
final void validate(XMLStreamReader reader, StAXResult result) throws SAXException, XMLStreamException { if (reader.hasNext()) { int eventType = reader.getEventType(); if (eventType != XMLStreamConstants.START_DOCUMENT && eventType != XMLStreamConstants.START_ELEMENT) { throw new SAXException( JAXPValidationMessageFormatter.formatMessage( fComponentManager.getLocale(), "StAXIllegalInitialState", null)); } fXMLStreamReaderLocation.setXMLStreamReader(reader); setup( fXMLStreamReaderLocation, result, Boolean.TRUE.equals(reader.getProperty(STRING_INTERNING))); fSchemaValidator.startDocument(fStAXLocationWrapper, null, fNamespaceContext, null); do { switch (eventType) { case XMLStreamConstants.START_ELEMENT: ++fDepth; fillQName( fElementQName, reader.getNamespaceURI(), reader.getLocalName(), reader.getPrefix()); fillXMLAttributes(reader); fillDeclaredPrefixes(reader); fNamespaceContext.setNamespaceContext(reader.getNamespaceContext()); fSchemaValidator.startElement(fElementQName, fAttributes, null); break; case XMLStreamConstants.END_ELEMENT: fillQName( fElementQName, reader.getNamespaceURI(), reader.getLocalName(), reader.getPrefix()); fillDeclaredPrefixes(reader); fNamespaceContext.setNamespaceContext(reader.getNamespaceContext()); fSchemaValidator.endElement(fElementQName, null); --fDepth; break; case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.SPACE: fTempString.setValues( reader.getTextCharacters(), reader.getTextStart(), reader.getTextLength()); fSchemaValidator.characters(fTempString, null); break; case XMLStreamConstants.CDATA: fSchemaValidator.startCDATA(null); fTempString.setValues( reader.getTextCharacters(), reader.getTextStart(), reader.getTextLength()); fSchemaValidator.characters(fTempString, null); fSchemaValidator.endCDATA(null); break; case XMLStreamConstants.START_DOCUMENT: ++fDepth; if (fStAXValidatorHandler != null) { fStAXValidatorHandler.startDocument(reader); } break; case XMLStreamConstants.PROCESSING_INSTRUCTION: if (fStAXValidatorHandler != null) { fStAXValidatorHandler.processingInstruction(reader); } break; case XMLStreamConstants.COMMENT: if (fStAXValidatorHandler != null) { fStAXValidatorHandler.comment(reader); } break; case XMLStreamConstants.ENTITY_REFERENCE: if (fStAXValidatorHandler != null) { fStAXValidatorHandler.entityReference(reader); } break; case XMLStreamConstants.DTD: processEntityDeclarations((List) reader.getProperty("javax.xml.stream.entities")); break; } eventType = reader.next(); } while (reader.hasNext() && fDepth > 0); fSchemaValidator.endDocument(null); if (eventType == XMLStreamConstants.END_DOCUMENT && fStAXValidatorHandler != null) { fStAXValidatorHandler.endDocument(reader); } } }
public Schema newSchema(Source[] schemas) throws SAXException { // this will let the loader store parsed Grammars into the pool. XMLGrammarPoolImplExtension pool = new XMLGrammarPoolImplExtension(); fXMLGrammarPoolWrapper.setGrammarPool(pool); XMLInputSource[] xmlInputSources = new XMLInputSource[schemas.length]; InputStream inputStream; Reader reader; for (int i = 0; i < schemas.length; i++) { Source source = schemas[i]; if (source instanceof StreamSource) { StreamSource streamSource = (StreamSource) source; String publicId = streamSource.getPublicId(); String systemId = streamSource.getSystemId(); inputStream = streamSource.getInputStream(); reader = streamSource.getReader(); xmlInputSources[i] = new XMLInputSource(publicId, systemId, null); xmlInputSources[i].setByteStream(inputStream); xmlInputSources[i].setCharacterStream(reader); } else if (source instanceof SAXSource) { SAXSource saxSource = (SAXSource) source; InputSource inputSource = saxSource.getInputSource(); if (inputSource == null) { throw new SAXException( JAXPValidationMessageFormatter.formatMessage( Locale.getDefault(), "SAXSourceNullInputSource", null)); } xmlInputSources[i] = new SAXInputSource(saxSource.getXMLReader(), inputSource); } else if (source instanceof DOMSource) { DOMSource domSource = (DOMSource) source; Node node = domSource.getNode(); String systemID = domSource.getSystemId(); xmlInputSources[i] = new DOMInputSource(node, systemID); } else if (source == null) { throw new NullPointerException( JAXPValidationMessageFormatter.formatMessage( Locale.getDefault(), "SchemaSourceArrayMemberNull", null)); } else { throw new IllegalArgumentException( JAXPValidationMessageFormatter.formatMessage( Locale.getDefault(), "SchemaFactorySourceUnrecognized", new Object[] {source.getClass().getName()})); } } try { fXMLSchemaLoader.loadGrammar(xmlInputSources); } catch (XNIException e) { // this should have been reported to users already. throw Util.toSAXException(e); } catch (IOException e) { // this hasn't been reported, so do so now. SAXParseException se = new SAXParseException(e.getMessage(), null, e); fErrorHandler.error(se); throw se; // and we must throw it. } // Clear reference to grammar pool. fXMLGrammarPoolWrapper.setGrammarPool(null); // Select Schema implementation based on grammar count. final int grammarCount = pool.getGrammarCount(); if (grammarCount > 1) { return new XMLSchema(new ReadOnlyGrammarPool(pool)); } else if (grammarCount == 1) { Grammar[] grammars = pool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA); return new SimpleXMLSchema(grammars[0]); } else { return EmptyXMLSchema.getInstance(); } }
public void validate(Source source, Result result) throws SAXException, IOException { if (result instanceof StreamResult || result == null) { final StreamSource streamSource = (StreamSource) source; final StreamResult streamResult = (StreamResult) result; XMLInputSource input = new XMLInputSource(streamSource.getPublicId(), streamSource.getSystemId(), null); input.setByteStream(streamSource.getInputStream()); input.setCharacterStream(streamSource.getReader()); // Gets the parser configuration. We'll create and initialize a new one, if we // haven't created one before or if the previous one was garbage collected. boolean newConfig = false; XMLParserConfiguration config = (XMLParserConfiguration) fConfiguration.get(); if (config == null) { config = initialize(); newConfig = true; } // If settings have changed on the component manager, refresh the error handler and entity // resolver. else if (fComponentManager.getFeature(PARSER_SETTINGS)) { config.setProperty(ENTITY_RESOLVER, fComponentManager.getProperty(ENTITY_RESOLVER)); config.setProperty(ERROR_HANDLER, fComponentManager.getProperty(ERROR_HANDLER)); config.setProperty(SECURITY_MANAGER, fComponentManager.getProperty(SECURITY_MANAGER)); } // prepare for parse fComponentManager.reset(); if (streamResult != null) { if (fSerializerFactory == null) { fSerializerFactory = SerializerFactory.getSerializerFactory(Method.XML); } // there doesn't seem to be a way to reset a serializer, so we need to make // a new one each time. Serializer ser; if (streamResult.getWriter() != null) { ser = fSerializerFactory.makeSerializer(streamResult.getWriter(), new OutputFormat()); } else if (streamResult.getOutputStream() != null) { ser = fSerializerFactory.makeSerializer(streamResult.getOutputStream(), new OutputFormat()); } else if (streamResult.getSystemId() != null) { String uri = streamResult.getSystemId(); OutputStream out = XMLEntityManager.createOutputStream(uri); ser = fSerializerFactory.makeSerializer(out, new OutputFormat()); } else { throw new IllegalArgumentException( JAXPValidationMessageFormatter.formatMessage( fComponentManager.getLocale(), "StreamResultNotInitialized", null)); } // we're using the parser only as an XNI-to-SAX converter, // so that we can use the SAX-based serializer SAXParser parser = (SAXParser) fParser.get(); if (newConfig || parser == null) { parser = new SAXParser(config); fParser = new SoftReference(parser); } else { parser.reset(); } config.setDocumentHandler(fSchemaValidator); fSchemaValidator.setDocumentHandler(parser); parser.setContentHandler(ser.asContentHandler()); } else { fSchemaValidator.setDocumentHandler(null); } try { config.parse(input); } catch (XMLParseException e) { throw Util.toSAXParseException(e); } catch (XNIException e) { throw Util.toSAXException(e); } finally { // release the references to the SAXParser and Serializer fSchemaValidator.setDocumentHandler(null); } return; } throw new IllegalArgumentException( JAXPValidationMessageFormatter.formatMessage( fComponentManager.getLocale(), "SourceResultMismatch", new Object[] {source.getClass().getName(), result.getClass().getName()})); }
public void validate(Source source, Result result) throws SAXException, IOException { if (result instanceof SAXResult || result == null) { final SAXSource saxSource = (SAXSource) source; final SAXResult saxResult = (SAXResult) result; if (result != null) { setContentHandler(saxResult.getHandler()); } try { XMLReader reader = saxSource.getXMLReader(); if (reader == null) { // create one now SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); try { reader = spf.newSAXParser().getXMLReader(); // If this is a Xerces SAX parser, set the security manager if there is one if (reader instanceof com.sun.org.apache.xerces.internal.parsers.SAXParser) { SecurityManager securityManager = (SecurityManager) fComponentManager.getProperty(SECURITY_MANAGER); if (securityManager != null) { try { reader.setProperty(SECURITY_MANAGER, securityManager); } // Ignore the exception if the security manager cannot be set. catch (SAXException exc) { } } } } catch (Exception e) { // this is impossible, but better safe than sorry throw new FactoryConfigurationError(e); } } // If XML names and Namespace URIs are already internalized we // can avoid running them through the SymbolTable. try { fStringsInternalized = reader.getFeature(STRING_INTERNING); } catch (SAXException exc) { // The feature isn't recognized or getting it is not supported. // In either case, assume that strings are not internalized. fStringsInternalized = false; } ErrorHandler errorHandler = fComponentManager.getErrorHandler(); reader.setErrorHandler( errorHandler != null ? errorHandler : DraconianErrorHandler.getInstance()); reader.setEntityResolver(fResolutionForwarder); fResolutionForwarder.setEntityResolver(fComponentManager.getResourceResolver()); reader.setContentHandler(this); reader.setDTDHandler(this); InputSource is = saxSource.getInputSource(); reader.parse(is); } finally { // release the reference to user's handler ASAP setContentHandler(null); } return; } throw new IllegalArgumentException( JAXPValidationMessageFormatter.formatMessage( Locale.getDefault(), "SourceResultMismatch", new Object[] {source.getClass().getName(), result.getClass().getName()})); }