Java tutorial
//package com.java2s; /* Copyright 2011-2014 Red Hat, Inc This file is part of PressGang CCMS. PressGang CCMS is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. PressGang CCMS 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with PressGang CCMS. If not, see <http://www.gnu.org/licenses/>. */ import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import org.w3c.dom.Document; import org.w3c.dom.Element; public class Main { /** * Creates an XIInclude element with a link to a file * * @param doc The DOM Document to create the xi:include for. * @param file The file name/path to link to. * @return An xi:include element that can be used to include content from another file. */ public static Element createXIInclude(final Document doc, final String file) { try { // Encode the file reference final StringBuilder fixedFileRef = new StringBuilder(); final String[] split = file.split("/"); for (int i = 0; i < split.length; i++) { if (i != 0) { fixedFileRef.append("/"); } final String uriPart = split[i]; if (uriPart.isEmpty() || uriPart.equals("..") || uriPart.equals(".")) { fixedFileRef.append(uriPart); } else { fixedFileRef.append(URLEncoder.encode(uriPart, "UTF-8")); } } // If the file ref ended with "/" then it wouldn't have been added as their was no content after it if (file.endsWith("/")) { fixedFileRef.append("/"); } final Element xiInclude = doc.createElementNS("http://www.w3.org/2001/XInclude", "xi:include"); xiInclude.setAttribute("href", fixedFileRef.toString()); xiInclude.setAttribute("xmlns:xi", "http://www.w3.org/2001/XInclude"); return xiInclude; } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } }