View Javadoc

1   /*
2    * Copyright 2011-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.tagcloud.export;
17  
18  import java.io.IOException;
19  import java.io.OutputStream;
20  import java.util.List;
21  
22  import javax.xml.parsers.DocumentBuilder;
23  import javax.xml.parsers.DocumentBuilderFactory;
24  import javax.xml.parsers.ParserConfigurationException;
25  import javax.xml.transform.OutputKeys;
26  import javax.xml.transform.Transformer;
27  import javax.xml.transform.TransformerException;
28  import javax.xml.transform.TransformerFactory;
29  import javax.xml.transform.dom.DOMSource;
30  import javax.xml.transform.stream.StreamResult;
31  
32  import org.w3c.dom.DOMException;
33  import org.w3c.dom.Document;
34  import org.w3c.dom.Element;
35  
36  import de.smartics.tagcloud.Tag;
37  import de.smartics.tagcloud.TagCloud;
38  
39  /**
40   * Exports the tag cloud to an XML file.
41   *
42   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
43   * @version $Revision:591 $
44   */
45  public class XmlTagCloudExport
46  {
47    // ********************************* Fields *********************************
48  
49    // --- constants ------------------------------------------------------------
50  
51    /**
52     * The URI of the XML schema instance.
53     * <p>
54     * The value of this constant is {@value}.
55     * </p>
56     */
57    private static final String XML_SCHEMA_INSTANCE =
58        "http://www.w3.org/2001/XMLSchema-instance";
59  
60    /**
61     * The URI of the test documentation doctype.
62     * <p>
63     * The value of this constant is {@value}.
64     * </p>
65     */
66    private static final String CODE_URI =
67        "http://www.smartics.de/project/references/domain/tagcloud";
68  
69    // --- members --------------------------------------------------------------
70  
71    /**
72     * The builder for XML documents.
73     */
74    private final DocumentBuilder builder; // NOPMD
75  
76    /**
77     * The flag determines whether or not the XML file should be pretty-printed (
78     * <code>true</code>) or not (<code>false</code>).
79     */
80    private final boolean prettyPrint;
81  
82    // ****************************** Initializer *******************************
83  
84    // ****************************** Constructors ******************************
85  
86    /**
87     * Default constructor.
88     */
89    public XmlTagCloudExport(final boolean prettyPrint)
90      throws ParserConfigurationException
91    {
92      final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
93      this.builder = factory.newDocumentBuilder();
94      this.prettyPrint = prettyPrint;
95    }
96  
97    // ****************************** Inner Classes *****************************
98  
99    // ********************************* Methods ********************************
100 
101   // --- init -----------------------------------------------------------------
102 
103   // --- get&set --------------------------------------------------------------
104 
105   // --- business -------------------------------------------------------------
106 
107   public void export(final TagCloud tagCloud, final OutputStream output)
108     throws IOException
109   {
110     final Document document = createDocument(tagCloud);
111     serialize(document, output);
112   }
113 
114   protected Document createDocument(final TagCloud tagCloud)
115   {
116     final Document document = builder.newDocument();
117     final Element rootElement = createDocRoot(document);
118 
119     final List<Tag> tags = tagCloud.getTags();
120     for (final Tag tag : tags)
121     {
122       final Element tagElement = document.createElement("tag");
123       tagElement.setAttribute("name", tag.getName());
124       tagElement.setAttribute("score", String.valueOf(tag.getScore()));
125       tagElement.setAttribute("weight", String.valueOf(tag.getWeight()));
126       rootElement.appendChild(tagElement);
127     }
128     return document;
129   }
130 
131   private Element createDocRoot(final Document document) throws DOMException
132   {
133     final Element docRoot = document.createElement("tagcloud");
134     docRoot.setAttribute("xmlns:xsi", XML_SCHEMA_INSTANCE);
135     docRoot.setAttribute("xmlns", CODE_URI);
136     docRoot.setAttribute("xsi:schemaLocation", CODE_URI + ' ' + CODE_URI);
137     document.appendChild(docRoot);
138     return docRoot;
139   }
140 
141   /**
142    * Writes the DOM document to the given stream.
143    *
144    * @param document the document to write.
145    * @param out the stream to write to.
146    * @throws IOException on any problem writing to the stream.
147    */
148   private void serialize(final Document document, final OutputStream out)
149     throws IOException
150   {
151     try
152     {
153       final TransformerFactory factory = TransformerFactory.newInstance();
154       final Transformer serializer = factory.newTransformer();
155       if (prettyPrint)
156       {
157         serializer.setOutputProperty(OutputKeys.INDENT, "yes");
158         serializer.setOutputProperty(
159             "{http://xml.apache.org/xslt}indent-amount", "2");
160       }
161 
162       serializer.transform(new DOMSource(document), new StreamResult(out));
163     }
164     catch (final TransformerException e)
165     {
166       throw new IOException("Cannot write XML document.", e);
167     }
168   }
169   // --- object basics --------------------------------------------------------
170 
171 }