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

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

Introduction

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

Prototype

public void setSystemId(String systemId) 

Source Link

Document

Set the system identifier for this locator.

Usage

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

/**
 * Generate XML data. This method loads a server pages generator associated
 * with its (file) input source and delegates SAX event generator to it
 * taking care of "closing" any event left open by the loaded generator as a
 * result of its possible "premature" return (a common situation in server
 * pages)/*from ww  w.j a v  a2 s.  c o  m*/
 *
 * @exception IOException IO Error
 * @exception SAXException SAX event generation error
 * @exception ProcessingException Error during load/execution
 */
public void generate() throws IOException, SAXException, ProcessingException {

    if (this.completionPipe != null) {
        generator.setConsumer(this.completionPipe);
        if (this.xmlConsumer != null) {
            this.completionPipe.setConsumer(this.xmlConsumer);
        } else {
            this.completionPipe.setContentHandler(this.contentHandler);
            this.completionPipe.setLexicalHandler(this.lexicalHandler);
        }
    } else {
        if (this.xmlConsumer != null) {
            generator.setConsumer(this.xmlConsumer);
        } else {
            generator.setContentHandler(this.contentHandler);
            generator.setLexicalHandler(this.lexicalHandler);
        }
    }

    // Fixes BUG#4062: Set document locator which is used by XIncludeTransformer
    org.xml.sax.helpers.LocatorImpl locator = new org.xml.sax.helpers.LocatorImpl();
    locator.setSystemId(this.inputSource.getURI());
    this.contentHandler.setDocumentLocator(locator);

    // Log exception and ensure that generator is released.
    try {
        generator.generate();
    } catch (IOException e) {
        getLogger().debug("IOException in generate()", e);
        throw e;
    } catch (SAXException e) {
        getLogger().debug("SAXException in generate()", e);
        throw e;
    } catch (ProcessingException e) {
        getLogger().debug("ProcessingException in generate()", e);
        throw e;
    } catch (Exception e) {
        getLogger().debug("Exception in generate()", e);
        throw new ProcessingException("Exception in ServerPagesGenerator.generate()", e);
    } finally {
        if (generator != null) {
            programGenerator.release(generator);
        }
        generator = null;
    }

    if (this.completionPipe != null) {
        this.completionPipe.flushEvents();
    }
}

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

/**
 * Generate XML data./*  w  w w . java 2 s .  c om*/
 *
 * @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./*from   www.ja  v a  2  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);
    /* 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.xchain.framework.lifecycle.Execution.java

/**
 * Updates the current location of the top entry of the execution trace stack.
 *///from w w  w. j a va2  s  .  co 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);
    }
}