Java Code Examples for org.xml.sax.ErrorHandler

The following are top voted examples for showing how to use org.xml.sax.ErrorHandler. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: openjdk-jdk10   File: DOMParser.java   View source code 6 votes vote down vote up
/**
 * Return the current error handler.
 *
 * @return The current error handler, or null if none
 *         has been registered.
 * @see #setErrorHandler
 */
public ErrorHandler getErrorHandler() {

    ErrorHandler errorHandler = null;
    try {
        XMLErrorHandler xmlErrorHandler =
            (XMLErrorHandler)fConfiguration.getProperty(ERROR_HANDLER);
        if (xmlErrorHandler != null &&
            xmlErrorHandler instanceof ErrorHandlerWrapper) {
            errorHandler = ((ErrorHandlerWrapper)xmlErrorHandler).getErrorHandler();
        }
    }
    catch (XMLConfigurationException e) {
        // do nothing
    }
    return errorHandler;

}
 
Example 2
Project: xmlrss   File: RedactableXMLSignature.java   View source code 6 votes vote down vote up
/**
 * Loads the document used for signing/verification/redaction from the given input stream.
 * <p>
 * The document is checked against the given schema and the given error handler is used.
 *
 * @param inputStream the input stream
 * @param schema      the schema of the loaded XML document
 * @param handler     the errorhandler
 * @throws RedactableXMLSignatureException if this RedactableXMLSignature object is not properly initialized
 */
public final void setDocument(InputStream inputStream, Schema schema, ErrorHandler handler)
        throws RedactableXMLSignatureException {

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setSchema(schema);
    documentBuilderFactory.setNamespaceAware(true);
    documentBuilderFactory.setIgnoringElementContentWhitespace(true);

    try {
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        documentBuilder.setErrorHandler(handler);
        setDocument(documentBuilder.parse(inputStream));
    } catch (ParserConfigurationException | SAXException | IOException e) {
        throw new RedactableXMLSignatureException(e);
    }
}
 
Example 3
Project: openjdk-jdk10   File: ValidatorTest.java   View source code 6 votes vote down vote up
private void validate(final String xsdFile, final Source src, final Result result) throws Exception {
    try {
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(new File(ValidatorTest.class.getResource(xsdFile).toURI()));

        // Get a Validator which can be used to validate instance document
        // against this grammar.
        Validator validator = schema.newValidator();
        ErrorHandler eh = new ErrorHandlerImpl();
        validator.setErrorHandler(eh);

        // Validate this instance document against the
        // Instance document supplied
        validator.validate(src, result);
    } catch (Exception ex) {
        throw ex;
    }
}
 
Example 4
Project: openjdk-jdk10   File: AbstractSAXParser.java   View source code 6 votes vote down vote up
/**
 * Return the current error handler.
 *
 * @return The current error handler, or null if none
 *         has been registered.
 * @see #setErrorHandler
 */
public ErrorHandler getErrorHandler() {

    ErrorHandler errorHandler = null;
    try {
        XMLErrorHandler xmlErrorHandler =
            (XMLErrorHandler)fConfiguration.getProperty(ERROR_HANDLER);
        if (xmlErrorHandler != null &&
            xmlErrorHandler instanceof ErrorHandlerWrapper) {
            errorHandler = ((ErrorHandlerWrapper)xmlErrorHandler).getErrorHandler();
        }
    }
    catch (XMLConfigurationException e) {
        // do nothing
    }
    return errorHandler;

}
 
Example 5
Project: OpenJSharp   File: ProblemActionHeader.java   View source code 6 votes vote down vote up
public void writeTo(ContentHandler h, ErrorHandler errorHandler) throws SAXException {
    String nsUri = getNamespaceURI();
    String ln = getLocalPart();

    h.startPrefixMapping("",nsUri);
    h.startElement(nsUri,ln,ln,EMPTY_ATTS);
    h.startElement(nsUri,actionLocalName,actionLocalName,EMPTY_ATTS);
    h.characters(action.toCharArray(),0,action.length());
    h.endElement(nsUri,actionLocalName,actionLocalName);
    if (soapAction != null) {
        h.startElement(nsUri,soapActionLocalName,soapActionLocalName,EMPTY_ATTS);
        h.characters(soapAction.toCharArray(),0,soapAction.length());
        h.endElement(nsUri,soapActionLocalName,soapActionLocalName);
    }
    h.endElement(nsUri,ln,ln);
}
 
Example 6
Project: openjdk-jdk10   File: DOMForestParser.java   View source code 6 votes vote down vote up
public void parse(
    InputSource source,
    ContentHandler contentHandler,
    ErrorHandler errorHandler,
    EntityResolver entityResolver )
    throws SAXException, IOException {

    String systemId = source.getSystemId();
    Document dom = forest.get(systemId);

    if(dom==null) {
        // if no DOM tree is built for it,
        // let the fall back parser parse the original document.
        //
        // for example, XSOM parses datatypes.xsd (XML Schema part 2)
        // but this will never be built into the forest.
        fallbackParser.parse( source, contentHandler, errorHandler, entityResolver );
        return;
    }

    scanner.scan( dom, contentHandler );
}
 
Example 7
Project: OpenJSharp   File: DOMForestParser.java   View source code 6 votes vote down vote up
public void parse(InputSource source, ContentHandler handler, ErrorHandler errorHandler, EntityResolver entityResolver)

            throws SAXException, IOException {
        String systemId = source.getSystemId();
        Document dom = forest.get(systemId);

        if (dom == null) {
            // if no DOM tree is built for it,
            // let the fall back parser parse the original document.
            //
            // for example, XSOM parses datatypes.xsd (XML Schema part 2)
            // but this will never be built into the forest.
            fallbackParser.parse(source, handler, errorHandler, entityResolver);
            return;
        }

        scanner.scan(dom, handler);

    }
 
Example 8
Project: OpenJSharp   File: SourceLocationAddOn.java   View source code 6 votes vote down vote up
public boolean run(
    Outline outline,
    Options opt,
    ErrorHandler errorHandler ) {

    for( ClassOutline ci : outline.getClasses() ) {
        JDefinedClass impl = ci.implClass;
        if (ci.getSuperClass() == null) {
            JVar $loc = impl.field(JMod.PROTECTED, Locator.class, fieldName);
            $loc.annotate(XmlLocation.class);
            $loc.annotate(XmlTransient.class);

            impl._implements(Locatable.class);

            impl.method(JMod.PUBLIC, Locator.class, "sourceLocation").body()._return($loc);

            JMethod setter = impl.method(JMod.PUBLIC, Void.TYPE, "setSourceLocation");
            JVar $newLoc = setter.param(Locator.class, "newLocator");
            setter.body().assign($loc, $newLoc);
        }
    }

    return true;
}
 
Example 9
Project: incubator-netbeans   File: XMLUtilTest.java   View source code 5 votes vote down vote up
public void testHandlerLeak() throws Exception {
    ErrorHandler handler = new DefaultHandler();
    EntityResolver resolver = new DefaultHandler();
    Reference<?> handlerRef = new WeakReference<Object>(handler);
    Reference<?> resolverRef = new WeakReference<Object>(resolver);
    XMLUtil.parse(new InputSource(new StringReader("<hello/>")), false, false, handler, resolver);
    handler = null;
    resolver = null;
    assertGC("can collect handler", handlerRef);
    assertGC("can collect resolver", resolverRef);
}
 
Example 10
Project: openjdk-jdk10   File: CompactSyntax.java   View source code 5 votes vote down vote up
public CompactSyntax(CompactParseable parseable, Reader r, String sourceUri, SchemaBuilder sb, ErrorHandler eh, String inheritedNs) {
  this(r);
  this.sourceUri = sourceUri;
  this.parseable = parseable;
  this.sb = sb;
  this.ncb = sb.getNameClassBuilder();
  this.eh = eh;
  // this causes the root pattern to have non-null annotations
  // which is useful because it gives a context to trang
  this.topLevelComments = sb.makeCommentList();
  this.inheritedNs = defaultNamespace = new String(inheritedNs);
}
 
Example 11
Project: incubator-netbeans   File: XMLUtil.java   View source code 5 votes vote down vote up
public static Document parse (
        InputSource input, 
        boolean validate, 
        boolean namespaceAware,
        ErrorHandler errorHandler,             
        EntityResolver entityResolver
    ) throws IOException, SAXException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();        
    factory.setValidating(validate);
    factory.setNamespaceAware(namespaceAware);            
    DocumentBuilder builder = null;
    try {
         builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new SAXException(ex);
    }
        
    if (errorHandler != null) {
        builder.setErrorHandler(errorHandler);
    }
    
    if (entityResolver != null) {
        builder.setEntityResolver(entityResolver);
    }

    assert input != null : "InputSource cannot be null";
    
    try {
        return builder.parse(input);
    } catch (SAXException ex) {
        StringBuilder msg = new StringBuilder();
        msg.append("Cannot parse");
        msg.append("Thread.cCL: ").append(Thread.currentThread().getContextClassLoader());
        throw new SAXException(msg.toString(), ex);
    }
}
 
Example 12
Project: lams   File: DefaultDocumentLoader.java   View source code 5 votes vote down vote up
/**
 * Load the {@link Document} at the supplied {@link InputSource} using the standard JAXP-configured
 * XML parser.
 */
@Override
public Document loadDocument(InputSource inputSource, EntityResolver entityResolver,
		ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {

	DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);
	if (logger.isDebugEnabled()) {
		logger.debug("Using JAXP provider [" + factory.getClass().getName() + "]");
	}
	DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);
	return builder.parse(inputSource);
}
 
Example 13
Project: lams   File: DefaultDocumentLoader.java   View source code 5 votes vote down vote up
/**
 * Create a JAXP DocumentBuilder that this bean definition reader
 * will use for parsing XML documents. Can be overridden in subclasses,
 * adding further initialization of the builder.
 * @param factory the JAXP DocumentBuilderFactory that the DocumentBuilder
 * should be created with
 * @param entityResolver the SAX EntityResolver to use
 * @param errorHandler the SAX ErrorHandler to use
 * @return the JAXP DocumentBuilder
 * @throws ParserConfigurationException if thrown by JAXP methods
 */
protected DocumentBuilder createDocumentBuilder(
		DocumentBuilderFactory factory, EntityResolver entityResolver, ErrorHandler errorHandler)
		throws ParserConfigurationException {

	DocumentBuilder docBuilder = factory.newDocumentBuilder();
	if (entityResolver != null) {
		docBuilder.setEntityResolver(entityResolver);
	}
	if (errorHandler != null) {
		docBuilder.setErrorHandler(errorHandler);
	}
	return docBuilder;
}
 
Example 14
Project: lams   File: PersistenceUnitReader.java   View source code 5 votes vote down vote up
/**
 * Validate the given stream and return a valid DOM document for parsing.
 */
protected Document buildDocument(ErrorHandler handler, InputStream stream)
		throws ParserConfigurationException, SAXException, IOException {

	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	dbf.setNamespaceAware(true);
	DocumentBuilder parser = dbf.newDocumentBuilder();
	parser.setErrorHandler(handler);
	return parser.parse(stream);
}
 
Example 15
Project: openjdk-jdk10   File: JAXBHeader.java   View source code 5 votes vote down vote up
public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException {
    try {
        bridge.marshal(jaxbObject,contentHandler,null);
    } catch (JAXBException e) {
        SAXParseException x = new SAXParseException(e.getMessage(),null,null,-1,-1,e);
        errorHandler.fatalError(x);
        throw x;
    }
}
 
Example 16
Project: OpenJSharp   File: StringHeader.java   View source code 5 votes vote down vote up
public void writeTo(ContentHandler h, ErrorHandler errorHandler) throws SAXException {
    String nsUri = name.getNamespaceURI();
    String ln = name.getLocalPart();

    h.startPrefixMapping("",nsUri);
    if(mustUnderstand) {
        AttributesImpl attributes = new AttributesImpl();
        attributes.addAttribute(soapVersion.nsUri,MUST_UNDERSTAND,"S:"+MUST_UNDERSTAND,"CDATA", getMustUnderstandLiteral(soapVersion));
        h.startElement(nsUri,ln,ln,attributes);
    } else {
        h.startElement(nsUri,ln,ln,EMPTY_ATTS);
    }
    h.characters(value.toCharArray(),0,value.length());
    h.endElement(nsUri,ln,ln);
}
 
Example 17
Project: OpenJSharp   File: JAXBHeader.java   View source code 5 votes vote down vote up
public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException {
    try {
        bridge.marshal(jaxbObject,contentHandler,null);
    } catch (JAXBException e) {
        SAXParseException x = new SAXParseException(e.getMessage(),null,null,-1,-1,e);
        errorHandler.fatalError(x);
        throw x;
    }
}
 
Example 18
Project: OpenJSharp   File: SchemaBuilderImpl.java   View source code 5 votes vote down vote up
/**
 *
 * @param eh Error handler to receive errors while building the schema.
 * @param datatypeLibraryFactory This is consulted to locate datatype
 * libraries.
 * @param pb Used to build patterns.
 */
public SchemaBuilderImpl(ErrorHandler eh,
        DatatypeLibraryFactory datatypeLibraryFactory,
        SchemaPatternBuilder pb) {
    this.parent = null;
    this.eh = eh;
    this.datatypeLibraryFactory = datatypeLibraryFactory;
    this.pb = pb;
    this.inheritNs = "";
    this.openIncludes = null;
}
 
Example 19
Project: OpenJSharp   File: CompactSyntax.java   View source code 5 votes vote down vote up
public CompactSyntax(CompactParseable parseable, Reader r, String sourceUri, SchemaBuilder sb, ErrorHandler eh, String inheritedNs) {
  this(r);
  this.sourceUri = sourceUri;
  this.parseable = parseable;
  this.sb = sb;
  this.ncb = sb.getNameClassBuilder();
  this.eh = eh;
  // this causes the root pattern to have non-null annotations
  // which is useful because it gives a context to trang
  this.topLevelComments = sb.makeCommentList();
  this.inheritedNs = defaultNamespace = new String(inheritedNs);
}
 
Example 20
Project: openjdk-jdk10   File: SchemaBuilderImpl.java   View source code 5 votes vote down vote up
/**
 *
 * @param eh Error handler to receive errors while building the schema.
 */
public SchemaBuilderImpl(ErrorHandler eh) {
    this(eh,
            new CascadingDatatypeLibraryFactory(new DatatypeLibraryLoader(),
            new BuiltinDatatypeLibraryFactory(new DatatypeLibraryLoader())),
            new SchemaPatternBuilder());
}
 
Example 21
Project: OpenJSharp   File: ToSAXHandler.java   View source code 5 votes vote down vote up
/**
 * @see org.xml.sax.ErrorHandler#fatalError(SAXParseException)
 */
public void fatalError(SAXParseException exc) throws SAXException {
    super.fatalError(exc);

    m_needToCallStartDocument = false;

    if (m_saxHandler instanceof ErrorHandler) {
        ((ErrorHandler)m_saxHandler).fatalError(exc);
    }
}
 
Example 22
Project: OpenJSharp   File: ConfigReader.java   View source code 5 votes vote down vote up
/**
 * Parses an xml config file and returns a Config object.
 *
 * @param xmlFile
 *        The xml config file which is passed by the user to annotation processing
 * @return
 *        A non null Config object
 */
private Config parseAndGetConfig (File xmlFile, ErrorHandler errorHandler, boolean disableSecureProcessing) throws SAXException, IOException {
    XMLReader reader;
    try {
        SAXParserFactory factory = XmlFactory.createParserFactory(disableSecureProcessing);
        reader = factory.newSAXParser().getXMLReader();
    } catch (ParserConfigurationException e) {
        // in practice this will never happen
        throw new Error(e);
    }
    NGCCRuntimeEx runtime = new NGCCRuntimeEx(errorHandler);

    // set up validator
    ValidatorHandler validator = configSchema.newValidator();
    validator.setErrorHandler(errorHandler);

    // the validator will receive events first, then the parser.
    reader.setContentHandler(new ForkContentHandler(validator,runtime));

    reader.setErrorHandler(errorHandler);
    Config config = new Config(runtime);
    runtime.setRootHandler(config);
    reader.parse(new InputSource(xmlFile.toURL().toExternalForm()));
    runtime.reset();

    return config;
}
 
Example 23
Project: openjdk-jdk10   File: XMLReaderImpl.java   View source code 4 votes vote down vote up
@Override
public void setErrorHandler(ErrorHandler handler) {
}
 
Example 24
Project: openjdk-jdk10   File: MessageWrapper.java   View source code 4 votes vote down vote up
@Override
public void writePayloadTo(ContentHandler contentHandler, ErrorHandler errorHandler, boolean fragment) throws SAXException {
    streamDelegate.writePayloadTo(contentHandler, errorHandler, fragment);
}
 
Example 25
Project: openjdk-jdk10   File: JAXBSource.java   View source code 4 votes vote down vote up
public void setErrorHandler(ErrorHandler handler) {
    this.errorHandler = handler;
}
 
Example 26
Project: openjdk-jdk10   File: ValidatorHandlerImpl.java   View source code 4 votes vote down vote up
public void setErrorHandler(ErrorHandler errorHandler) {
    fComponentManager.setErrorHandler(errorHandler);
}
 
Example 27
Project: lams   File: StaticBasicParserPool.java   View source code 4 votes vote down vote up
/** {@inheritDoc} */
public void setErrorHandler(ErrorHandler eh) {
    checkValidState();
    return;
}
 
Example 28
Project: monarch   File: CacheXmlGenerator.java   View source code 4 votes vote down vote up
public ErrorHandler getErrorHandler() {
  return this;
}
 
Example 29
Project: ramus   File: ReportSaveXMLReader.java   View source code 4 votes vote down vote up
@Override
public void setErrorHandler(ErrorHandler handler) {
}
 
Example 30
Project: ramus   File: ChartSaveXMLReader.java   View source code 4 votes vote down vote up
@Override
public ErrorHandler getErrorHandler() {
    return null;
}
 
Example 31
Project: OpenJSharp   File: SAXBufferProcessor.java   View source code 4 votes vote down vote up
public void setErrorHandler(ErrorHandler handler) {
    _errorHandler = handler;
}
 
Example 32
Project: OpenJSharp   File: SAXBufferProcessor.java   View source code 4 votes vote down vote up
public ErrorHandler getErrorHandler() {
    return _errorHandler;
}
 
Example 33
Project: OpenJSharp   File: XMLStreamBuffer.java   View source code 4 votes vote down vote up
public final void writeTo(ContentHandler handler, ErrorHandler errorHandler) throws SAXException {
    writeTo(handler, errorHandler, isFragment());
}
 
Example 34
Project: openjdk-jdk10   File: CheckingSchemaBuilder.java   View source code 4 votes vote down vote up
public CheckingSchemaBuilder( SchemaBuilder sb, ErrorHandler eh, DatatypeLibraryFactory dlf ) {
    super(new SchemaBuilderImpl(eh,dlf,new SchemaPatternBuilder()),sb);
}
 
Example 35
Project: OpenJSharp   File: SAXDocumentParser.java   View source code 4 votes vote down vote up
public ErrorHandler getErrorHandler() {
    return _errorHandler;
}
 
Example 36
Project: OpenJSharp   File: PayloadStreamReaderMessage.java   View source code 4 votes vote down vote up
public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException {
    message.writeTo(contentHandler, errorHandler);
}
 
Example 37
Project: OpenJSharp   File: PayloadStreamReaderMessage.java   View source code 4 votes vote down vote up
protected void writePayloadTo(ContentHandler contentHandler, ErrorHandler errorHandler, boolean fragment) throws SAXException {
    message.writePayloadTo(contentHandler, errorHandler, fragment);
}
 
Example 38
Project: OpenJSharp   File: ValidatorHandlerImpl.java   View source code 4 votes vote down vote up
public ErrorHandler getErrorHandler() {
    return fComponentManager.getErrorHandler();
}
 
Example 39
Project: OpenJSharp   File: StreamMessage.java   View source code 4 votes vote down vote up
public void writePayloadTo(ContentHandler contentHandler, ErrorHandler errorHandler, boolean fragment) throws SAXException {
    if ( envelopeReader != null ) readEnvelope(this);
    assert unconsumed();

    try {
        if(payloadLocalName==null)
            return; // no body

        if (bodyPrologue != null) {
            char[] chars = bodyPrologue.toCharArray();
            contentHandler.characters(chars, 0, chars.length);
        }

        XMLStreamReaderToContentHandler conv = new XMLStreamReaderToContentHandler(reader,contentHandler,true,fragment,getInscopeNamespaces());

        while(reader.getEventType() != XMLStreamConstants.END_DOCUMENT){
            String name = reader.getLocalName();
            String nsUri = reader.getNamespaceURI();

            // After previous conv.bridge() call the cursor will be at END_ELEMENT.
            // Check if its not soapenv:Body then move to next ELEMENT
            if(reader.getEventType() == XMLStreamConstants.END_ELEMENT){

                if (!isBodyElement(name, nsUri)){
                    // closing payload element: store epilogue for further signing, if applicable
                    // however if there more than one payloads exist - the last one is stored
                    String whiteSpaces = XMLStreamReaderUtil.nextWhiteSpaceContent(reader);
                    if (whiteSpaces != null) {
                        this.bodyEpilogue = whiteSpaces;
                        // write it to the message too
                        char[] chars = whiteSpaces.toCharArray();
                        contentHandler.characters(chars, 0, chars.length);
                    }
                } else {
                    // body closed > exit
                    break;
                }

            } else {
                // payload opening element: copy payload to writer
                conv.bridge();
            }
        }
        XMLStreamReaderUtil.readRest(reader);
        XMLStreamReaderUtil.close(reader);
        XMLStreamReaderFactory.recycle(reader);
    } catch (XMLStreamException e) {
        Location loc = e.getLocation();
        if(loc==null)   loc = DummyLocation.INSTANCE;

        SAXParseException x = new SAXParseException(
            e.getMessage(),loc.getPublicId(),loc.getSystemId(),loc.getLineNumber(),loc.getColumnNumber(),e);
        errorHandler.error(x);
    }
}
 
Example 40
Project: openjdk-jdk10   File: XMLReaderImpl.java   View source code 4 votes vote down vote up
@Override
public ErrorHandler getErrorHandler() {
    return null;
}