Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.StringReader;

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

import org.w3c.dom.Document;

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    /**
     * Gets a maven pom document
     * 
     * @return POM document
     */
    public static Document getPomDocument() {
        String template = "<?xml version=\"1.0\"?>\n"
                + "<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\"\n"
                + " xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"
                + "  <modelVersion>4.0.0</modelVersion><groupId/><artifactId/><version/><packaging/><properties/>\n"
                + "</project>";
        return getTemplateDocument(template);
    }

    /**
     * Parses a document from the given string
     * 
     * @param template The string to parse
     * @return The parsed {@link Document}
     */
    public static Document getTemplateDocument(String template) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = null;
        dbf.setValidating(false);
        try {
            db = dbf.newDocumentBuilder();
            db.setEntityResolver(new EntityResolver() {
                public InputSource resolveEntity(String publicID, String systemID) throws SAXException {
                    return new InputSource(new StringReader(""));
                }
            });
            Document doc = db.parse(new ByteArrayInputStream(template.getBytes("utf8")));
            return doc;
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}