Example usage for org.xml.sax.helpers LocatorImpl LocatorImpl

List of usage examples for org.xml.sax.helpers LocatorImpl LocatorImpl

Introduction

In this page you can find the example usage for org.xml.sax.helpers LocatorImpl LocatorImpl.

Prototype

public LocatorImpl() 

Source Link

Document

Zero-argument constructor.

Usage

From source file:com.puppycrawl.tools.checkstyle.XmlContentHandler.java

/**
 * Default constructor. //from   ww  w  .j a  va2  s . c  o  m
 */
public XmlContentHandler(File file) {
    super();

    locator = new LocatorImpl();
    root = null;
    this.file = file;
}

From source file:org.esco.grouper.parsing.SGSParsingUtil.java

/**
 * Builds an instance of SGSParsingUtil.
 */// w  ww  .  j a  va 2  s  .  c om
public SGSParsingUtil() {
    super();
    locator = new LocatorImpl();
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Creation of a ContentHandler: " + getClass().getSimpleName() + ".");
    }
}

From source file:org.ajax4jsf.renderkit.compiler.HtmlCompiler.java

/**
 * Constructor with initialisation.//from  ww  w. j  a  va2 s.  co  m
 */
public HtmlCompiler() {
    WithDefaultsRulesWrapper rules = new WithDefaultsRulesWrapper(new RulesBase());
    // Add default rules to process plain tags.
    rules.addDefault(new PlainElementCreateRule());
    rules.addDefault(new SetNextRule(CHILD_METHOD));
    digestr.setDocumentLocator(new LocatorImpl());
    digestr.setRules(rules);
    digestr.setValidating(false);
    digestr.setNamespaceAware(false);
    digestr.setUseContextClassLoader(true);
    // Concrete renderer method call rules.
    setCustomRules(digestr);
}

From source file:org.apache.cocoon.generation.TextGenerator.java

/**
 * Generate XML data.//from   www. j a v  a2  s.  c  o  m
 *
 * @throws IOException
 * @throws ProcessingException
 * @throws SAXException
 */
public void generate() throws IOException, SAXException, ProcessingException {
    InputStreamReader in = null;

    try {
        final InputStream sis = this.inputSource.getInputStream();
        if (sis == null) {
            throw new ProcessingException("Source '" + this.inputSource.getURI() + "' not found");
        }

        if (encoding != null) {
            in = new InputStreamReader(sis, encoding);
        } else {
            in = new InputStreamReader(sis);
        }
    } catch (SourceException se) {
        throw new ProcessingException("Error during resolving of '" + this.source + "'.", se);
    }

    LocatorImpl locator = new LocatorImpl();

    locator.setSystemId(this.inputSource.getURI());
    locator.setLineNumber(1);
    locator.setColumnNumber(1);

    contentHandler.setDocumentLocator(locator);
    contentHandler.startDocument();
    contentHandler.startPrefixMapping("", URI);

    AttributesImpl atts = new AttributesImpl();
    if (localizable) {
        atts.addAttribute("", "source", "source", "CDATA", locator.getSystemId());
        atts.addAttribute("", "line", "line", "CDATA", String.valueOf(locator.getLineNumber()));
        atts.addAttribute("", "column", "column", "CDATA", String.valueOf(locator.getColumnNumber()));
    }

    contentHandler.startElement(URI, "text", "text", atts);

    LineNumberReader reader = new LineNumberReader(in);
    String line;
    String newline = null;

    while (true) {
        if (newline == null) {
            line = convertNonXmlChars(reader.readLine());
        } else {
            line = newline;
        }
        if (line == null) {
            break;
        }
        newline = convertNonXmlChars(reader.readLine());
        if (newline != null) {
            line += SystemUtils.LINE_SEPARATOR;
        }
        locator.setLineNumber(reader.getLineNumber());
        locator.setColumnNumber(1);
        contentHandler.characters(line.toCharArray(), 0, line.length());
        if (newline == null) {
            break;
        }
    }
    reader.close();
    contentHandler.endElement(URI, "text", "text");
    contentHandler.endPrefixMapping("");
    contentHandler.endDocument();
}

From source file:org.apache.cocoon.generation.TextGenerator2.java

/**
 * Generate XML data.// w  w w . j a va2s .  c  o  m
 *
 * @throws IOException
 * @throws ProcessingException
 * @throws SAXException
 */
public void generate() throws IOException, SAXException, ProcessingException {
    InputStreamReader in = null;
    try {
        final InputStream sis = this.inputSource.getInputStream();
        if (sis == null) {
            throw new ProcessingException("Source '" + this.inputSource.getURI() + "' not found");
        }
        if (encoding != null) {
            in = new InputStreamReader(sis, encoding);
        } else {
            in = new InputStreamReader(sis);
        }
    } catch (SourceException se) {
        throw new ProcessingException("Error during resolving of '" + this.source + "'.", se);
    }
    LocatorImpl locator = new LocatorImpl();
    locator.setSystemId(this.inputSource.getURI());
    locator.setLineNumber(1);
    locator.setColumnNumber(1);
    /* Do not pass the source URI to the contentHandler, assuming that that is the LexicalTransformer. It does not have to be.
      contentHandler.setDocumentLocator(locator);
    */
    contentHandler.startDocument();
    AttributesImpl atts = new AttributesImpl();
    if (localizable) {
        atts.addAttribute("", "source", "source", "CDATA", locator.getSystemId());
        atts.addAttribute("", "line", "line", "CDATA", String.valueOf(locator.getLineNumber()));
        atts.addAttribute("", "column", "column", "CDATA", String.valueOf(locator.getColumnNumber()));
    }
    String nsPrefix = this.element.contains(":") ? this.element.replaceFirst(":.+$", "") : "";
    String localName = this.element.replaceFirst("^.+:", "");
    if (this.namespace.length() > 1)
        contentHandler.startPrefixMapping(nsPrefix, this.namespace);
    contentHandler.startElement(this.namespace, localName, this.element, atts);
    LineNumberReader reader = new LineNumberReader(in);
    String line;
    String newline = null;
    while (true) {
        if (newline == null) {
            line = convertNonXmlChars(reader.readLine());
        } else {
            line = newline;
        }
        if (line == null) {
            break;
        }
        newline = convertNonXmlChars(reader.readLine());
        if (newline != null) {
            line += SystemUtils.LINE_SEPARATOR;
        }
        locator.setLineNumber(reader.getLineNumber());
        locator.setColumnNumber(1);
        contentHandler.characters(line.toCharArray(), 0, line.length());
        if (newline == null) {
            break;
        }
    }
    reader.close();
    contentHandler.endElement(this.namespace, localName, this.element);
    if (this.namespace.length() > 1)
        contentHandler.endPrefixMapping(nsPrefix);
    contentHandler.endDocument();
}

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

@Override
public PipelineEventReader<XMLEventReader, XMLEvent> getEventReader(HttpServletRequest request,
        HttpServletResponse response) {/*from   w w w  .  j a  v  a 2s.co  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:org.n52.ifgicopter.spf.xml.SchematronValidator.java

/**
 * Here the real validation process is started.
 * //from  w w  w.  j  a  v  a 2 s.  co  m
 * @return true if no schematron rule was violated.
 */
public boolean validate() {
    Transform trans = new Transform();

    /*
     * transform the schematron
     */
    String[] arguments = new String[] { "-x", "org.apache.xerces.parsers.SAXParser", "-w1", "-o",
            DOCS_FOLDER + "/tmp/tmp.xsl", this.schematronFile, DOCS_FOLDER + "/iso_svrl_for_xslt2.xsl",
            "generate-paths=yes" };
    trans.doTransform(arguments, "java net.sf.saxon.Transform");

    /*
     * transform the instance
     */
    String report = DOCS_FOLDER + "/tmp/"
            + this.xmlInstanceFile.substring(this.xmlInstanceFile.lastIndexOf("/") + 1) + ".report.xml";
    arguments = new String[] { "-x", "org.apache.xerces.parsers.SAXParser", "-w1", "-o", report,
            this.xmlInstanceFile, DOCS_FOLDER + "/tmp/tmp.xsl" };
    trans.doTransform(arguments, "java net.sf.saxon.Transform");

    LocatorImpl locator = new LocatorImpl();
    /*
     * an extension of DefaultHandler
     */
    DefaultHandler handler = new DefaultHandler() {

        private String failTmp;
        private Locator locator2;
        private boolean insideFail = false;

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes)
                throws SAXException {
            if (qName.endsWith("failed-assert")) {
                this.failTmp = "Assertion error at \"" + attributes.getValue("test") + "\" (line "
                        + this.locator2.getLineNumber() + "): ";
                this.insideFail = true;
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if (qName.endsWith("failed-assert")) {
                SchematronValidator.this.assertFails.add(this.failTmp);
                this.failTmp = null;
                this.insideFail = false;
            }
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            if (this.insideFail) {
                this.failTmp += new String(ch, start, length).trim();
            }
        }

        @Override
        public void setDocumentLocator(Locator l) {
            this.locator2 = l;
        }

    };
    handler.setDocumentLocator(locator);

    try {
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        parser.parse(new File(report), handler);
    } catch (SAXException e) {
        log.warn(e.getMessage(), e);
    } catch (IOException e) {
        log.warn(e.getMessage(), e);
    } catch (ParserConfigurationException e) {
        log.warn(e.getMessage(), e);
    }

    return (this.assertFails.size() == 0) ? true : false;
}

From source file:org.xchain.framework.lifecycle.Execution.java

/**
 * Updates the current location of the top entry of the execution trace stack.
 *//*from w w  w. j a v  a2s . c  o  m*/
private static void updateExecutionTraceLocation() {
    if (!executionTraceStack.isEmpty()) {
        Command command = ((CommandExecutionContext) executionContextStack.peek()).command;
        Locator locator = null;

        if (command instanceof Locatable) {
            locator = ((Locatable) command).getLocator();
        } else {
            LocatorImpl locatorImpl = new LocatorImpl();
            locatorImpl.setLineNumber(0);
            locatorImpl.setColumnNumber(0);
            locatorImpl.setSystemId("UNKNOWN_LOCATION");
            locator = locatorImpl;
        }

        executionTraceStack.peek().setLocator(locator);
    }
}