Example usage for org.xml.sax ContentHandler setDocumentLocator

List of usage examples for org.xml.sax ContentHandler setDocumentLocator

Introduction

In this page you can find the example usage for org.xml.sax ContentHandler setDocumentLocator.

Prototype

public void setDocumentLocator(Locator locator);

Source Link

Document

Receive an object for locating the origin of SAX document events.

Usage

From source file:net.javacrumbs.json2xml.JsonSaxAdapter.java

/**
 * Creates JsonSaxAdapter that coverts JSON to SAX events.
 * @param jsonParser parsed JSON//from   w  w  w . j av a2 s.co m
 * @param contentHandler target of SAX events
 * @param namespaceUri namespace of the generated XML
 * @param addTypeAttributes adds type information as attributes
 * @param artificialRootName if set, an artificial root is generated so JSON documents with more roots can be handeled.
 */
public JsonSaxAdapter(final JsonParser jsonParser, final ContentHandler contentHandler,
        final String namespaceUri, final boolean addTypeAttributes, final String artificialRootName,
        final ElementNameConverter nameConverter) {
    this.jsonParser = jsonParser;
    this.contentHandler = contentHandler;
    this.namespaceUri = namespaceUri;
    this.addTypeAttributes = addTypeAttributes;
    this.artificialRootName = artificialRootName;
    this.nameConverter = nameConverter;
    contentHandler.setDocumentLocator(new DocumentLocator());
}

From source file:org.apereo.portal.rendering.xslt.XSLTComponent.java

@Override
public PipelineEventReader<XMLEventReader, XMLEvent> getEventReader(HttpServletRequest request,
        HttpServletResponse response) {//from   www  .ja v a  2  s  .c o m
    final PipelineEventReader<XMLEventReader, XMLEvent> pipelineEventReader = this.wrappedComponent
            .getEventReader(request, response);

    final Transformer transformer = this.transformerSource.getTransformer(request, response);

    //Setup a URIResolver based on the current resource loader
    transformer.setURIResolver(this.uriResolver);

    //Configure the Transformer via injected class
    if (this.xsltParameterSource != null) {
        final Map<String, Object> transformerParameters = this.xsltParameterSource.getParameters(request,
                response);
        if (transformerParameters != null) {
            this.logger.debug("{} - Setting Transformer Parameters: ", this.beanName, transformerParameters);
            for (final Map.Entry<String, Object> transformerParametersEntry : transformerParameters
                    .entrySet()) {
                final String name = transformerParametersEntry.getKey();
                final Object value = transformerParametersEntry.getValue();
                if (value != null) {
                    transformer.setParameter(name, value);
                }
            }
        }

        final Properties outputProperties = this.xsltParameterSource.getOutputProperties(request, response);
        if (outputProperties != null) {
            this.logger.debug("{} - Setting Transformer Output Properties: ", this.beanName, outputProperties);
            transformer.setOutputProperties(outputProperties);
        }
    }

    //The event reader from the previous component in the pipeline
    final XMLEventReader eventReader = pipelineEventReader.getEventReader();

    //Wrap the event reader in a stream reader to avoid a JDK bug
    final XMLStreamReader streamReader;
    try {
        streamReader = new FixedXMLEventStreamReader(eventReader);
    } catch (XMLStreamException e) {
        throw new RuntimeException("Failed to create XMLStreamReader from XMLEventReader", e);
    }
    final Source xmlReaderSource = new StAXSource(streamReader);

    //Setup logging for the transform
    transformer.setErrorListener(this.errorListener);

    //Transform to a SAX ContentHandler to avoid JDK bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6775588
    final XMLEventBufferWriter eventWriterBuffer = new XMLEventBufferWriter();
    final ContentHandler contentHandler = StaxUtils.createLexicalContentHandler(eventWriterBuffer);
    contentHandler.setDocumentLocator(new LocatorImpl());

    final SAXResult outputTarget = new SAXResult(contentHandler);
    try {
        this.logger.debug("{} - Begining XML Transformation", this.beanName);
        transformer.transform(xmlReaderSource, outputTarget);
        this.logger.debug("{} - XML Transformation complete", this.beanName);
    } catch (TransformerException e) {
        throw new RuntimeException("Failed to transform document", e);
    }

    final String mediaType = transformer.getOutputProperty(OutputKeys.MEDIA_TYPE);

    final List<XMLEvent> eventBuffer = eventWriterBuffer.getEventBuffer();
    final XMLEventReader outputEventReader = new XMLEventBufferReader(eventBuffer.listIterator());

    final Map<String, String> outputProperties = pipelineEventReader.getOutputProperties();
    final PipelineEventReaderImpl<XMLEventReader, XMLEvent> pipelineEventReaderImpl = new PipelineEventReaderImpl<XMLEventReader, XMLEvent>(
            outputEventReader, outputProperties);
    pipelineEventReaderImpl.setOutputProperty(OutputKeys.MEDIA_TYPE, mediaType);
    return pipelineEventReaderImpl;
}

From source file:uk.ac.cam.caret.sakai.rwiki.component.service.impl.XSLTEntityHandler.java

public void parseToSAX(final String toRender, final ContentHandler ch) throws IOException, SAXException {
    /**// w w w  .  ja  v a2 s.c o m
     * create a proxy for the stream, filtering out the start element and
     * end element events
     */
    ContentHandler proxy = new ContentHandler() {
        public void setDocumentLocator(Locator arg0) {
            ch.setDocumentLocator(arg0);
        }

        public void startDocument() throws SAXException {
            // ignore
        }

        public void endDocument() throws SAXException {
            // ignore
        }

        public void startPrefixMapping(String arg0, String arg1) throws SAXException {
            ch.startPrefixMapping(arg0, arg1);
        }

        public void endPrefixMapping(String arg0) throws SAXException {
            ch.endPrefixMapping(arg0);
        }

        public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
            ch.startElement(arg0, arg1, arg2, arg3);
        }

        public void endElement(String arg0, String arg1, String arg2) throws SAXException {
            ch.endElement(arg0, arg1, arg2);
        }

        public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
            ch.characters(arg0, arg1, arg2);
        }

        public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {
            ch.ignorableWhitespace(arg0, arg1, arg2);
        }

        public void processingInstruction(String arg0, String arg1) throws SAXException {
            ch.processingInstruction(arg0, arg1);
        }

        public void skippedEntity(String arg0) throws SAXException {
            ch.skippedEntity(arg0);
        }

    };
    InputSource ins = new InputSource(new StringReader(toRender));
    XMLReader xmlReader;
    try {
        SAXParser saxParser = saxParserFactory.newSAXParser();

        xmlReader = saxParser.getXMLReader();
    }

    catch (Exception e) {
        log.error("SAXException when creating XMLReader", e); //$NON-NLS-1$
        // rethrow!!
        throw new SAXException(e);
    }
    xmlReader.setContentHandler(proxy);
    xmlReader.parse(ins);
}