001/*
002 *  Copyright 2017 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.contentio;
017
018import java.io.StringWriter;
019import java.util.Properties;
020
021import javax.xml.transform.OutputKeys;
022import javax.xml.transform.Transformer;
023import javax.xml.transform.TransformerException;
024import javax.xml.transform.TransformerFactory;
025import javax.xml.transform.dom.DOMSource;
026import javax.xml.transform.stream.StreamResult;
027
028import org.w3c.dom.Node;
029
030/**
031 * Helper to import contents.
032 */
033public final class ContentImporterHelper
034{
035    private ContentImporterHelper()
036    {
037        // empty
038    }
039    
040    /**
041     * Transform text to docbook syntaxe
042     * @param lines The paragraphs to transform
043     * @return the docbook text
044     */
045    public static String textToDocbook (String[] lines)
046    {
047        StringBuilder sb = new StringBuilder();
048        
049        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
050        sb.append("<article version=\"5.0\" xmlns=\"http://docbook.org/ns/docbook\">");
051        
052        for (String line : lines)
053        {
054            sb.append("<para>");
055            sb.append(line.replaceAll("<", "&lt;").replaceAll("&", "&amp;").replaceAll("\r?\n", "<phrase role=\"linebreak\"/>"));
056            sb.append("</para>");
057        }
058        
059        sb.append("</article>");
060        
061        return sb.toString();
062    }
063    
064    /**
065     * Serialize a XML node as a String, with XML declaration and without indentation.
066     * @param node the node.
067     * @return the XML string.
068     * @throws TransformerException if an error occurs.
069     */
070    public static String serializeNode(Node node) throws TransformerException
071    {
072        return serializeNode(node, false, false);
073    }
074    
075    /**
076     * Serialize a XML node as a String, with XML declaration.
077     * @param node the node.
078     * @param indent true to indent the result, false otherwise.
079     * @return the XML string.
080     * @throws TransformerException if an error occurs.
081     */
082    public static String serializeNode(Node node, boolean indent) throws TransformerException
083    {
084        return serializeNode(node, indent, false);
085    }
086    
087    /**
088     * Serialize a XML node as a String.
089     * @param node the node.
090     * @param indent true to indent the result, false otherwise.
091     * @param omitXmlDeclaration true to omit XML declaration, false otherwise.
092     * @return the XML string.
093     * @throws TransformerException if an error occurs.
094     */
095    public static String serializeNode(Node node, boolean indent, boolean omitXmlDeclaration) throws TransformerException
096    {
097        Transformer transformer = TransformerFactory.newInstance().newTransformer();
098        
099        Properties format = new Properties();
100        format.put(OutputKeys.METHOD, "xml");
101        format.put(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
102        format.put(OutputKeys.INDENT, indent ? "yes" : "no");
103        format.put(OutputKeys.ENCODING, "UTF-8");
104        
105        transformer.setOutputProperties(format);
106        
107        StringWriter writer = new StringWriter();
108        DOMSource domSource = new DOMSource(node);
109        StreamResult result = new StreamResult(writer);
110        
111        transformer.transform(domSource, result);
112        
113        return writer.toString();
114    }
115}