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