Java XML String Transform encode(final String value)

Here you can find the source of encode(final String value)

Description

XML-encode a String

License

Open Source License

Parameter

Parameter Description
value the String to be XML-encoded

Return

the XML-encoded String

Declaration

public static String encode(final String value) 

Method Source Code

//package com.java2s;
/**/*from w  w w  .  j  a  v a2  s .  c om*/
 * Copyright 2006 OCLC Online Computer Library Center Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or
 * agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.StringWriter;
import java.util.Map;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Node;

public class Main {
    private static TransformerFactory tFactory = TransformerFactory.newInstance();

    /**
     * XML-encode a String
     * 
     * @param value the String to be XML-encoded
     * @return the XML-encoded String
     */
    public static String encode(final String value) {
        final StringBuffer sb = new StringBuffer();
        for (int i = 0; i < value.length(); ++i) {
            final char c = value.charAt(i);
            switch (c) {
            case '&':
                sb.append("&amp;");
                break;
            case '<':
                sb.append("&lt;");
                break;
            case '>':
                sb.append("&gt;");
                break;
            case '\'':
                sb.append("&apos;");
                break;
            case '"':
                sb.append("&quot;");
                break;
            default:
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * Transform a DOM Node into an XML String.
     * 
     * @param node
     * @return an XML String representation of the specified Node
     * @throws TransformerException
     */
    public static String toString(final Node node) throws TransformerException {
        return toString(node, true);
    }

    /**
     * Transform a DOM Node into an XML String
     * 
     * @param node
     * @param omitXMLDeclaration
     * @return an XML String representation of the specified Node
     * @throws TransformerException
     */
    public static String toString(final Node node, final boolean omitXMLDeclaration) throws TransformerException {
        final StringWriter writer = new StringWriter();
        final Transformer transformer = getThreadedIdentityTransformer(omitXMLDeclaration);
        final Source source = new DOMSource(node);
        final Result result = new StreamResult(writer);
        transformer.transform(source, result);
        return writer.toString();
    }

    /**
     * Get a thread-safe Transformer without an assigned transform. This is useful for transforming a DOM Document into
     * XML text.
     * 
     * @param omitXmlDeclaration
     * @return an "identity" Transformer assigned to the current thread
     * @throws TransformerConfigurationException
     */
    public static Transformer getThreadedIdentityTransformer(final boolean omitXmlDeclaration)
            throws TransformerConfigurationException {
        return getThreadedTransformer(omitXmlDeclaration, null, (String) null);
    }

    /**
     * Get a thread-safe Transformer without an assigned transform. This is useful for transforming a DOM Document into
     * XML text.
     * 
     * @param omitXmlDeclaration
     * @param standalone
     * @return an Identity Transformer
     * @throws TransformerConfigurationException
     */
    public static Transformer getThreadedIdentityTransformer(final boolean omitXmlDeclaration,
            final boolean standalone) throws TransformerConfigurationException {
        return getThreadedTransformer(omitXmlDeclaration, standalone, null, (String) null);
    }

    private static Transformer getThreadedTransformer(final boolean omitXmlDeclaration, final boolean standalone,
            final Map<Thread, Transformer> threadMap, final String xslURL)
            throws TransformerConfigurationException {
        final Thread currentThread = Thread.currentThread();
        Transformer transformer = null;
        if (threadMap != null) {
            transformer = threadMap.get(currentThread);
        }
        if (transformer == null) {
            if (xslURL == null) {
                transformer = tFactory.newTransformer(); // "never null"
            } else {
                transformer = tFactory.newTransformer(new StreamSource(xslURL));
            }
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            if (threadMap != null) {
                threadMap.put(currentThread, transformer);
            }
        }
        transformer.setOutputProperty(OutputKeys.STANDALONE, standalone ? "yes" : "no");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
        return transformer;
    }

    /**
     * Get a thread-safe Transformer.
     * 
     * @param omitXmlDeclaration
     * @param threadMap
     * @param xslURL
     * @return a thread-safe Transformer
     * @throws TransformerConfigurationException
     */
    public static Transformer getThreadedTransformer(final boolean omitXmlDeclaration,
            final Map<Thread, Transformer> threadMap, final String xslURL)
            throws TransformerConfigurationException {
        return getThreadedTransformer(omitXmlDeclaration, true, threadMap, xslURL);
    }
}

Related

  1. createXml(ArrayList nameList, ArrayList stringList, ArrayList stringArrayNameList, HashMap> stringArrayContentMap)
  2. createXmlError(String message, String details)
  3. createXmlEventReaderOnXmlString(String xml, String docBaseUri)
  4. decodeHex(String hex)
  5. elementToXMLString(Object aElement)
  6. encodeStringIntoMemento(String str)
  7. escapeBackslashes(String value)
  8. extractAndNormalizeEmbedPictures(String xmlFile, String odtFile, String parentDir, String imgBaseDir)
  9. extractElement(String xml, String tagName)