Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//* Licensed Materials - Property of IBM                              *

import java.io.IOException;
import java.io.InputStream;

import java.util.StringTokenizer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.ls.DOMImplementationLS;

import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.SAXException;

public class Main {
    public static String toNormalizedXML(InputStream is)
            throws ParserConfigurationException, SAXException, IOException {

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setCoalescing(true);
        dbf.setIgnoringElementContentWhitespace(true);
        dbf.setIgnoringComments(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(is);
        document.normalizeDocument();
        document.getDocumentElement().setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", //$NON-NLS-1$
                "xsi:schemaLocation", //$NON-NLS-1$
                "http://abc4trust.eu/wp2/abcschemav1.0 ../../../../../../../../../abc4trust-xml/src/main/resources/xsd/schema.xsd"); //$NON-NLS-1$
        DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
        LSSerializer serializer = domImplLS.createLSSerializer();
        String xml = serializer.writeToString(document);

        return trim(xml);
    }

    /**
     * Remove leading a trailing whitespace characters from each line of input
     * @param input
     * @return
     */
    public static String trim(String input) {
        final String newlineDelimiters = "\n\r\f"; //$NON-NLS-1$

        StringBuilder ret = new StringBuilder();
        StringTokenizer st = new StringTokenizer(input, newlineDelimiters);
        while (st.hasMoreTokens()) {
            ret.append(st.nextToken().replaceAll("^\\s+", "").replaceAll("\\s+$", "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            ret.append("\n"); //$NON-NLS-1$
        }
        return ret.toString();
    }
}