Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (c) 2012 European Synchrotron Radiation Facility,
 *                    Diamond Light Source Ltd.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.io.StringWriter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.NodeList;

public class Main {
    private static final Pattern HEADER_PATTERN = Pattern.compile("^\\<\\?.+\\?\\>(.*)", Pattern.DOTALL);

    private static String getNodeValue(NodeList nodeList)
            throws TransformerFactoryConfigurationError, TransformerException {

        final Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setURIResolver(null);

        final StringBuilder buf = new StringBuilder();
        for (int i = 0; i < nodeList.getLength(); i++) {
            final StringWriter sw = new StringWriter();
            serializer.transform(new DOMSource(nodeList.item(i)), new StreamResult(sw));
            String xml = sw.toString();
            final Matcher matcher = HEADER_PATTERN.matcher(xml);
            if (matcher.matches()) {
                xml = matcher.group(1);
            }
            buf.append(xml);
            buf.append("\n");
        }

        return buf.toString();
    }
}