Search in sources :

Example 71 with SAXException

use of org.xml.sax.SAXException in project OpenAM by OpenRock.

the class UnmarshallerImpl method unmarshal.

protected Object unmarshal(XMLReader reader, InputSource source) throws JAXBException {
    SAXLocator locator = new SAXLocator();
    SAXUnmarshallerHandler handler = createUnmarshallerHandler(locator);
    reader = InterningXMLReader.adapt(reader);
    reader.setContentHandler(handler);
    // saxErrorHandler will be set by the createUnmarshallerHandler method.
    // configure XMLReader so that the error will be sent to it.
    // This is essential for the UnmarshallerHandler to be able to abort
    // unmarshalling when an error is found.
    //
    // Note that when this XMLReader is provided by the client code,
    // it might be already configured to call a client error handler.
    // This will clobber such handler, if any.
    //
    // Ryan noted that we might want to report errors to such a client
    // error handler as well.
    reader.setErrorHandler(new ErrorHandlerAdaptor(handler, locator));
    try {
        reader.parse(source);
    } catch (IOException e) {
        throw new JAXBException(e);
    } catch (SAXException e) {
        throw createUnmarshalException(e);
    }
    Object result = handler.getResult();
    // avoid keeping unnecessary references too long to let the GC
    // reclaim more memory.
    // setting null upsets some parsers, so use a dummy instance instead.
    reader.setContentHandler(dummyHandler);
    reader.setErrorHandler(dummyHandler);
    return result;
}
Also used : SAXLocator(com.sun.xml.bind.validator.SAXLocator) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 72 with SAXException

use of org.xml.sax.SAXException in project OpenAM by OpenRock.

the class Util method handlePrintConversionException.

/**
     * Reports a print conversion error while marshalling.
     */
public static void handlePrintConversionException(Object caller, Exception e, XMLSerializer serializer) throws SAXException {
    if (e instanceof SAXException)
        //        will be thrown) 
        throw (SAXException) e;
    String message = e.getMessage();
    if (message == null) {
        message = e.toString();
    }
    ValidationEvent ve = new PrintConversionEventImpl(ValidationEvent.ERROR, message, new ValidationEventLocatorImpl(caller), e);
    serializer.reportError(ve);
}
Also used : ValidationEvent(javax.xml.bind.ValidationEvent) ValidationEventLocatorImpl(javax.xml.bind.helpers.ValidationEventLocatorImpl) PrintConversionEventImpl(javax.xml.bind.helpers.PrintConversionEventImpl) SAXException(org.xml.sax.SAXException)

Example 73 with SAXException

use of org.xml.sax.SAXException in project OpenAM by OpenRock.

the class MarshallerImpl method write.

private void write(XMLSerializable obj, ContentHandler writer) throws JAXBException {
    try {
        if (getSchemaLocation() != null || getNoNSSchemaLocation() != null) {
            // if we need to add xsi:schemaLocation or its brother,
            // throw in the component to do that.
            writer = new SchemaLocationFilter(getSchemaLocation(), getNoNSSchemaLocation(), writer);
        }
        SAXMarshaller serializer = new SAXMarshaller(writer, prefixMapper, this);
        // set a DocumentLocator that doesn't provide any information
        writer.setDocumentLocator(new LocatorImpl());
        writer.startDocument();
        serializer.childAsBody(obj, null);
        writer.endDocument();
        // extra check
        serializer.reconcileID();
    } catch (SAXException e) {
        throw new MarshalException(e);
    }
}
Also used : SchemaLocationFilter(com.sun.xml.bind.marshaller.SchemaLocationFilter) MarshalException(javax.xml.bind.MarshalException) LocatorImpl(org.xml.sax.helpers.LocatorImpl) SAXException(org.xml.sax.SAXException)

Example 74 with SAXException

use of org.xml.sax.SAXException in project OpenAM by OpenRock.

the class ValidatorImpl method validate.

private boolean validate(Object o, boolean validateId) throws ValidationException {
    try {
        //ValidatableObject vo = Util.toValidatableObject(o);
        ValidatableObject vo = jaxbContext.getGrammarInfo().castToValidatableObject(o);
        if (vo == null)
            throw new ValidationException(Messages.format(Messages.NOT_VALIDATABLE));
        EventInterceptor ei = new EventInterceptor(eventHandler);
        ValidationContext context = new ValidationContext(jaxbContext, ei, validateId);
        context.validate(vo);
        context.reconcileIDs();
        return !ei.hadError();
    } catch (SAXException e) {
        // we need a consistent mechanism to convert SAXException into JAXBException
        Exception nested = e.getException();
        if (nested != null) {
            throw new ValidationException(nested);
        } else {
            throw new ValidationException(e);
        }
    //return false;
    }
}
Also used : ValidationException(javax.xml.bind.ValidationException) SAXException(org.xml.sax.SAXException) PropertyException(javax.xml.bind.PropertyException) ValidationException(javax.xml.bind.ValidationException) SAXException(org.xml.sax.SAXException)

Example 75 with SAXException

use of org.xml.sax.SAXException in project OpenAM by OpenRock.

the class ErrorHandlerAdaptor method propagateEvent.

private void propagateEvent(int severity, SAXParseException saxException) throws SAXException {
    // get location info:
    //     sax locators simply use the location info embedded in the 
    //     sax exception, dom locators keep a reference to their DOMScanner
    //     and call back to figure out where the error occurred.
    ValidationEventLocator vel = locator.getLocation(saxException);
    ValidationEventImpl ve = new ValidationEventImpl(severity, saxException.getMessage(), vel);
    Exception e = saxException.getException();
    if (e != null) {
        ve.setLinkedException(e);
    } else {
        ve.setLinkedException(saxException);
    }
    // call the client's event handler.
    host.handleEvent(ve, severity != ValidationEvent.FATAL_ERROR);
}
Also used : ValidationEventImpl(javax.xml.bind.helpers.ValidationEventImpl) ValidationEventLocator(javax.xml.bind.ValidationEventLocator) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Aggregations

SAXException (org.xml.sax.SAXException)2465 IOException (java.io.IOException)1622 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1049 Document (org.w3c.dom.Document)682 DocumentBuilder (javax.xml.parsers.DocumentBuilder)537 InputSource (org.xml.sax.InputSource)518 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)415 InputStream (java.io.InputStream)317 Element (org.w3c.dom.Element)311 NodeList (org.w3c.dom.NodeList)292 File (java.io.File)274 Node (org.w3c.dom.Node)247 ByteArrayInputStream (java.io.ByteArrayInputStream)235 StringReader (java.io.StringReader)224 SAXParser (javax.xml.parsers.SAXParser)209 SAXParseException (org.xml.sax.SAXParseException)196 TransformerException (javax.xml.transform.TransformerException)180 ArrayList (java.util.ArrayList)169 SAXParserFactory (javax.xml.parsers.SAXParserFactory)159 XMLReader (org.xml.sax.XMLReader)151