1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.issues.notes;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.StringWriter;
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.TransformerConfigurationException;
28 import javax.xml.transform.TransformerException;
29 import javax.xml.transform.TransformerFactory;
30 import javax.xml.transform.dom.DOMSource;
31 import javax.xml.transform.stream.StreamResult;
32
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Node;
35 import org.w3c.dom.NodeList;
36 import org.xml.sax.SAXException;
37
38
39
40
41
42
43
44 public class NotesReader
45 {
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public NotesReader()
60 {
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public String read(final InputStream inputStream) throws IOException
82 {
83 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
84 DocumentBuilder builder = null;
85 try
86 {
87 builder = factory.newDocumentBuilder();
88 final Document document = builder.parse(inputStream);
89 return extractBodyContent(document);
90 }
91 catch (final ParserConfigurationException e)
92 {
93 final IOException ex =
94 new IOException(
95 "Cannot create parser to read description file from stream.");
96 ex.initCause(e);
97 throw ex;
98 }
99 catch (final SAXException e)
100 {
101 final IOException ex =
102 new IOException("Cannot read description file from stream.");
103 ex.initCause(e);
104 throw ex;
105 }
106 }
107
108
109
110
111
112
113
114
115
116
117 private String extractBodyContent(final Document document) throws IOException
118 {
119 final NodeList bodyList = document.getElementsByTagName("body");
120 final int bodyCount = bodyList.getLength();
121 if (bodyCount != 1)
122 {
123 throw new IOException("Expected one body element, found " + bodyCount
124 + '.');
125 }
126
127 final Node body = bodyList.item(0);
128
129 return extractContent(body);
130 }
131
132
133
134
135
136
137
138
139 private String extractContent(final Node body) throws IOException
140 {
141 try
142 {
143 final DOMSource source = new DOMSource(body);
144 final StringWriter writer = new StringWriter(2048);
145 final StreamResult result = new StreamResult(writer);
146 final TransformerFactory transformerFactory =
147 TransformerFactory.newInstance();
148 final Transformer transformer = transformerFactory.newTransformer();
149
150 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
151 transformer.transform(source, result);
152
153 final String content = writer.toString();
154 final int start = content.indexOf("<body>") + "<body>".length();
155 final int end = content.lastIndexOf("</body>");
156
157 return content.substring(start, end).trim();
158 }
159 catch (final TransformerConfigurationException e)
160 {
161 final IOException ex =
162 new IOException("Cannot write description file to memory.");
163 ex.initCause(e);
164 throw ex;
165 }
166 catch (final TransformerException e)
167 {
168 final IOException ex =
169 new IOException("Cannot write description file to memory.");
170 ex.initCause(e);
171 throw ex;
172 }
173 }
174
175
176
177 }