Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * (J)ava (M)iscellaneous (U)tilities (L)ibrary
 *
 * JMUL is a central repository for utilities which are used in my
 * other public and private repositories.
 *
 * Copyright (C) 2013  Kristian Kutin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * e-mail: kristian.kutin@arcor.de
 */

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.ProcessingInstruction;

public class Main {
    /**
     * A string constant.
     */
    private static final String TARGET_STYLESHEET = "xml-stylesheet";
    /**
     * A string constant.
     */
    private static final String LOCATION_PLACEHOLDER = "{stylesheet.location}";
    /**
     * A string constant.
     */
    private static final String DATA_STYLESHEET = "type=\"text/xsl\" href=\"" + LOCATION_PLACEHOLDER + "\"";

    /**
     * Adds stylesheet informations to an xml document. See
     * <a href='http://stackoverflow.com/questions/2651647/add-xml-stylesheet-and-get-standalone-yes'>Stack Overflow</a>.
     *
     * @param aDocument
     * @param aFilename
     */
    public static void addStylesheet(Document aDocument, String aFilename) {

        aDocument.setXmlStandalone(true);

        Element root = aDocument.getDocumentElement();

        String data = DATA_STYLESHEET.replace(LOCATION_PLACEHOLDER, aFilename);
        ProcessingInstruction processingInstruction = aDocument.createProcessingInstruction(TARGET_STYLESHEET,
                data);

        aDocument.insertBefore(processingInstruction, root);
    }
}