Java XML String Transform readFile(String systemId)

Here you can find the source of readFile(String systemId)

Description

Reads a file.

License

Apache License

Parameter

Parameter Description
systemId the system id

Exception

Parameter Description
Exception if an exception occurs

Return

the content

Declaration

public static String readFile(String systemId) throws Exception 

Method Source Code

//package com.java2s;
/* See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * Esri Inc. licenses this file to You 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./*from  w  w w .j av a  2  s  .c  o m*/
 */

import java.io.StringWriter;

import javax.xml.XMLConstants;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Main {
    /** DEFAULT_ENCODING = "UTF-8" */
    public static final String DEFAULT_ENCODING = "UTF-8";
    /** DEFAULT_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" */
    public static final String DEFAULT_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

    /**
     * Reads a file.
     * @param systemId the system id
     * @return the content
     * @throws Exception if an exception occurs
     */
    public static String readFile(String systemId) throws Exception {
        /*
        String s = new String(Files.readAllBytes(Paths.get(path)),"UTF-8");
        if (s != null) s = s.trim();
        return s;
         */
        StringWriter result = new StringWriter();
        transform(new StreamSource(systemId), new StreamResult(result), true);
        return checkResult(result, true);
    }

    /**
     * Executes a transformation.
     * <br>The output encoding is set to UTF-8
     * @param source the transformation source
     * @param result the transformation result
     * @param indent if true, the output indent key is set to "yes"
     * @throws TransformerException if an exception occurs
     */
    public static void transform(javax.xml.transform.Source source, javax.xml.transform.Result result,
            boolean indent) throws TransformerException {
        TransformerFactory factory = TransformerFactory.newInstance();
        factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
        factory.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true);
        //factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true); 
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, DEFAULT_ENCODING);
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        if (indent) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        }
        transformer.transform(source, result);
    }

    /**
     * Check a result.
     * @param result the result
     * @return the string (trimmed, null if empty)
     */
    public static String checkResult(StringWriter result, boolean checkIndent) {
        String s = result.toString();
        if (s != null) {
            s = s.trim();
            if (checkIndent) {
                if (s.startsWith(DEFAULT_HEADER + "<")) {
                    s = s.replace(DEFAULT_HEADER, DEFAULT_HEADER + "\r\n");
                }
            }
            if (s.length() == 0)
                s = null;
        }
        ;
        return s;
    }
}

Related

  1. normXML(String s)
  2. parseEventsToXML(String module, Hashtable keyvalues)
  3. parseToString(final Node node)
  4. parseXmlFile(String filename, boolean validating, boolean namespaceAware)
  5. readFile(String fileName)
  6. readURL(String urlStr)
  7. readXMLFile(String xmlFileName)
  8. render(String name, byte[] xmldata)
  9. replaceLineSeparatorInternal(String string, String lineSeparator)