Example usage for javax.xml.transform.sax TransformerHandler endPrefixMapping

List of usage examples for javax.xml.transform.sax TransformerHandler endPrefixMapping

Introduction

In this page you can find the example usage for javax.xml.transform.sax TransformerHandler endPrefixMapping.

Prototype

public void endPrefixMapping(String prefix) throws SAXException;

Source Link

Document

End the scope of a prefix-URI mapping.

Usage

From source file:IOUtils.java

/**
 * Checks if the used Trax implementation correctly handles namespaces set using
 * <code>startPrefixMapping()</code>, but wants them also as 'xmlns:' attributes.
 * <p>//from  w  w w . j  ava2s.co m
 * The check consists in sending SAX events representing a minimal namespaced document
 * with namespaces defined only with calls to <code>startPrefixMapping</code> (no
 * xmlns:xxx attributes) and check if they are present in the resulting text.
 */
protected static boolean needsNamespacesAsAttributes(Properties format)
        throws TransformerException, SAXException {
    // Serialize a minimal document to check how namespaces are handled.
    final StringWriter writer = new StringWriter();

    final String uri = "namespaceuri";
    final String prefix = "nsp";
    final String check = "xmlns:" + prefix + "='" + uri + "'";

    final TransformerHandler handler = FACTORY.newTransformerHandler();

    handler.getTransformer().setOutputProperties(format);
    handler.setResult(new StreamResult(writer));

    // Output a single element
    handler.startDocument();
    handler.startPrefixMapping(prefix, uri);
    handler.startElement(uri, "element", "element", new AttributesImpl());
    handler.endElement(uri, "element", "element");
    handler.endPrefixMapping(prefix);
    handler.endDocument();

    final String text = writer.toString();

    // Check if the namespace is there (replace " by ' to be sure of what we search in)
    boolean needsIt = (text.replace('"', '\'').indexOf(check) == -1);

    return needsIt;
}

From source file:org.apache.cocoon.components.source.impl.WebDAVSource.java

private InputStream resourcesToXml(WebdavResource[] resources) throws Exception {
    TransformerFactory tf = TransformerFactory.newInstance();
    TransformerHandler th = ((SAXTransformerFactory) tf).newTransformerHandler();
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(bOut);
    th.setResult(result);/* www  .j  a v a 2s .c  om*/
    th.startDocument();
    th.startPrefixMapping(PREFIX, NAMESPACE);
    th.startElement(NAMESPACE, COLLECTION_NAME, PREFIX + ":" + COLLECTION_NAME, XMLUtils.EMPTY_ATTRIBUTES);
    resourcesToSax(resources, th);
    th.endElement(NAMESPACE, COLLECTION_NAME, PREFIX + ":" + COLLECTION_NAME);
    th.endPrefixMapping(PREFIX);
    th.endDocument();

    return new ByteArrayInputStream(bOut.toByteArray());
}

From source file:org.apache.cocoon.serialization.AbstractTextSerializer.java

/**
 * Checks if the used Trax implementation correctly handles namespaces set using
 * <code>startPrefixMapping()</code>, but wants them also as 'xmlns:' attributes.
 * <p>/*  w w  w .  jav  a2  s . c  o  m*/
 * The check consists in sending SAX events representing a minimal namespaced document
 * with namespaces defined only with calls to <code>startPrefixMapping</code> (no
 * xmlns:xxx attributes) and check if they are present in the resulting text.
 */
protected boolean needsNamespacesAsAttributes() throws Exception {

    SAXTransformerFactory factory = getTransformerFactory();

    Boolean cacheValue = (Boolean) needsNamespaceCache.get(factory.getClass().getName());
    if (cacheValue != null) {
        return cacheValue.booleanValue();
    } else {
        // Serialize a minimal document to check how namespaces are handled.
        StringWriter writer = new StringWriter();

        String uri = "namespaceuri";
        String prefix = "nsp";
        String check = "xmlns:" + prefix + "='" + uri + "'";

        TransformerHandler handler = this.getTransformerHandler();

        handler.getTransformer().setOutputProperties(format);
        handler.setResult(new StreamResult(writer));

        // Output a single element
        handler.startDocument();
        handler.startPrefixMapping(prefix, uri);
        handler.startElement(uri, "element", "element", XMLUtils.EMPTY_ATTRIBUTES);
        handler.endElement(uri, "element", "element");
        handler.endPrefixMapping(prefix);
        handler.endDocument();

        String text = writer.toString();

        // Check if the namespace is there (replace " by ' to be sure of what we search in)
        boolean needsIt = (text.replace('"', '\'').indexOf(check) == -1);

        String msg = needsIt ? " needs namespace attributes (will be slower)."
                : " handles correctly namespaces.";

        getLogger().debug("Trax handler " + handler.getClass().getName() + msg);

        needsNamespaceCache.put(factory.getClass().getName(), BooleanUtils.toBooleanObject(needsIt));

        return needsIt;
    }
}