1 | package com.framsticks.leftovers; |
---|
2 | |
---|
3 | import java.io.BufferedWriter; |
---|
4 | import java.io.File; |
---|
5 | import java.io.FileOutputStream; |
---|
6 | import java.io.IOException; |
---|
7 | import java.io.OutputStreamWriter; |
---|
8 | import java.nio.file.Files; |
---|
9 | import java.util.ArrayList; |
---|
10 | import java.util.Collection; |
---|
11 | import java.util.HashMap; |
---|
12 | import java.util.LinkedHashMap; |
---|
13 | import java.util.Map; |
---|
14 | |
---|
15 | import javax.xml.parsers.DocumentBuilder; |
---|
16 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
17 | import javax.xml.parsers.ParserConfigurationException; |
---|
18 | import javax.xml.stream.XMLOutputFactory; |
---|
19 | import javax.xml.stream.XMLStreamException; |
---|
20 | import javax.xml.stream.XMLStreamWriter; |
---|
21 | |
---|
22 | import org.apache.log4j.Logger; |
---|
23 | import org.w3c.dom.Document; |
---|
24 | import org.w3c.dom.Node; |
---|
25 | import org.w3c.dom.NodeList; |
---|
26 | import org.xml.sax.SAXException; |
---|
27 | |
---|
28 | import com.framsticks.util.io.Encoding; |
---|
29 | |
---|
30 | public class FavouritesXMLFactory { |
---|
31 | private static final Logger log = Logger.getLogger(FavouritesXMLFactory.class); |
---|
32 | |
---|
33 | private static final String PATH_MARK = "path"; |
---|
34 | private static final String NAME_MARK = "name"; |
---|
35 | private static final String FIELD_MARK = "field"; |
---|
36 | private static final String TYPE_MARK = "type"; |
---|
37 | |
---|
38 | protected Node readDocument(String filename) { |
---|
39 | File file = new File(filename); |
---|
40 | if (!file.exists()) { |
---|
41 | return null; |
---|
42 | } |
---|
43 | DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory |
---|
44 | .newInstance(); |
---|
45 | try { |
---|
46 | DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); |
---|
47 | Document doc = docBuilder.parse(file); |
---|
48 | doc.getDocumentElement().normalize(); |
---|
49 | return doc.getFirstChild(); |
---|
50 | |
---|
51 | } catch (ParserConfigurationException | SAXException | IOException e) { |
---|
52 | log.error(e); |
---|
53 | return null; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | public static class XmlWriter implements AutoCloseable { |
---|
58 | |
---|
59 | BufferedWriter writer = null; |
---|
60 | XMLStreamWriter xml = null; |
---|
61 | |
---|
62 | public XmlWriter(String filename) throws XMLStreamException, |
---|
63 | IOException { |
---|
64 | XMLOutputFactory factory = XMLOutputFactory.newInstance(); |
---|
65 | |
---|
66 | File file = new File(filename); |
---|
67 | Files.deleteIfExists(file.toPath()); |
---|
68 | |
---|
69 | writer = new BufferedWriter(new OutputStreamWriter( |
---|
70 | new FileOutputStream(file), |
---|
71 | Encoding.getDefaultCharset())); |
---|
72 | xml = factory.createXMLStreamWriter(writer); |
---|
73 | |
---|
74 | xml.writeStartDocument("1.0"); |
---|
75 | } |
---|
76 | |
---|
77 | public void close() { |
---|
78 | try { |
---|
79 | xml.writeEndDocument(); |
---|
80 | xml.flush(); |
---|
81 | xml.close(); |
---|
82 | writer.close(); |
---|
83 | } catch (XMLStreamException | IOException e) { |
---|
84 | log.error(e); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | public void start(String name) throws XMLStreamException { |
---|
89 | xml.writeStartElement(name); |
---|
90 | } |
---|
91 | |
---|
92 | public void element(String name, Object value) throws XMLStreamException { |
---|
93 | start(name); |
---|
94 | xml.writeCharacters(value.toString()); |
---|
95 | end(); |
---|
96 | } |
---|
97 | |
---|
98 | public void end() throws XMLStreamException { |
---|
99 | xml.writeEndElement(); |
---|
100 | } |
---|
101 | |
---|
102 | public void attribute(String name, Object value) throws XMLStreamException { |
---|
103 | xml.writeAttribute(name, value.toString()); |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Writes favourites nodes and fields list to XML file. |
---|
109 | * |
---|
110 | * @param path |
---|
111 | * Path of XML file. |
---|
112 | * @param nodes |
---|
113 | * Collection of user favourites nodes and fields. |
---|
114 | */ |
---|
115 | public void writeFavouritesXML(String path, Collection<UserFavourite> nodes) { |
---|
116 | if (nodes.isEmpty()) { |
---|
117 | return; |
---|
118 | } |
---|
119 | |
---|
120 | try (XmlWriter xml = new XmlWriter(path)) { |
---|
121 | xml.start("framsticks"); |
---|
122 | for (UserFavourite node : nodes) { |
---|
123 | xml.start("item"); |
---|
124 | |
---|
125 | xml.element(PATH_MARK, node.getPath()); |
---|
126 | xml.element(NAME_MARK, node.getName()); |
---|
127 | |
---|
128 | /*if(node.isFavouriteNode()){ |
---|
129 | xmlWriter.writeStartElement(typeMark); |
---|
130 | xmlWriter.writeCharacters(Integer.toString(node.getNodeType().getId())); |
---|
131 | xmlWriter.writeEndElement(); |
---|
132 | }else{ //isFavouriteField() */ |
---|
133 | xml.element(FIELD_MARK, node.getField()); |
---|
134 | xml.end(); |
---|
135 | } |
---|
136 | xml.end(); |
---|
137 | } catch (XMLStreamException | IOException e) { |
---|
138 | log.error(e); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * Reads user favourites nodes and fields from XML file. |
---|
144 | * |
---|
145 | * @param filePath |
---|
146 | * PAth to XML file. |
---|
147 | * @return LinkedHashMap contains user favourites nodes and fields. Key is |
---|
148 | * concatenation of path and field getName. |
---|
149 | */ |
---|
150 | public LinkedHashMap<String, UserFavourite> readFavouritesXML( |
---|
151 | String filePath) { |
---|
152 | |
---|
153 | Node root = readDocument(filePath); |
---|
154 | if (root == null) { |
---|
155 | return null; |
---|
156 | } |
---|
157 | LinkedHashMap<String, UserFavourite> result = new LinkedHashMap<String, UserFavourite>(); |
---|
158 | |
---|
159 | NodeList items = root.getChildNodes(); |
---|
160 | UserFavourite userNode; |
---|
161 | String path, name, field; |
---|
162 | for (int i = 0; i < items.getLength(); i++) { |
---|
163 | Node item = items.item(i); |
---|
164 | NodeList itemDetails = item.getChildNodes(); |
---|
165 | if (itemDetails.getLength() == 3) { |
---|
166 | if (itemDetails.item(0).getNodeName().equals(PATH_MARK) |
---|
167 | && itemDetails.item(1).getNodeName().equals(NAME_MARK) |
---|
168 | && (itemDetails.item(2).getNodeName().equals(FIELD_MARK) |
---|
169 | || itemDetails.item(2).getNodeName().equals(TYPE_MARK))) { |
---|
170 | path = itemDetails.item(0).getTextContent(); |
---|
171 | name = itemDetails.item(1).getTextContent(); |
---|
172 | if (itemDetails.item(2).getNodeName().equals( |
---|
173 | FIELD_MARK)) { |
---|
174 | field = itemDetails.item(2).getTextContent(); |
---|
175 | userNode = new UserFavourite(path, name, field); |
---|
176 | result.put(path+field, userNode); |
---|
177 | }else{ |
---|
178 | try { |
---|
179 | // int nodeTypeId = Integer.parseInt(itemDetails.item(2).getTextContent()); |
---|
180 | userNode = new UserFavourite(path, name, null); |
---|
181 | result.put(path, userNode); |
---|
182 | } catch (Exception e) { |
---|
183 | } |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | return result; |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * Writes column configuration to XML file. |
---|
194 | * |
---|
195 | * @param path |
---|
196 | * Path to XML file. |
---|
197 | * @param groupColumns |
---|
198 | * Map with column configuration. Key - path to group node. Value - |
---|
199 | * List of columns getId. |
---|
200 | */ |
---|
201 | public void writeColumnConfigurationXML(String path, |
---|
202 | Map<String, ArrayList<String>> groupColumns) { |
---|
203 | if (groupColumns.isEmpty()) { |
---|
204 | return; |
---|
205 | } |
---|
206 | try (XmlWriter xml = new XmlWriter(path)) { |
---|
207 | xml.start("framsticks"); |
---|
208 | for (Map.Entry<String, ArrayList<String>> i : groupColumns.entrySet()) { |
---|
209 | xml.start("item"); |
---|
210 | xml.attribute(PATH_MARK, i.getKey()); |
---|
211 | for (String column : i.getValue()) { |
---|
212 | xml.element("column", column); |
---|
213 | } |
---|
214 | xml.end(); |
---|
215 | } |
---|
216 | xml.end(); |
---|
217 | } catch (XMLStreamException | IOException e) { |
---|
218 | log.error(e); |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | /** |
---|
223 | * Reads column configuration from XML file. |
---|
224 | * |
---|
225 | * @param path |
---|
226 | * Path to XML file. |
---|
227 | * @return HashMap with configuration. Key - path to group node. Value - |
---|
228 | * List of columns getId. |
---|
229 | */ |
---|
230 | public HashMap<String, ArrayList<String>> readColumnConfigurationXML( |
---|
231 | String path) { |
---|
232 | Node root = readDocument(path); |
---|
233 | if (root == null) { |
---|
234 | return null; |
---|
235 | } |
---|
236 | |
---|
237 | HashMap<String, ArrayList<String>> result = new LinkedHashMap<String, ArrayList<String>>(); |
---|
238 | |
---|
239 | NodeList items = root.getChildNodes(); |
---|
240 | String itemPath; |
---|
241 | ArrayList<String> columns; |
---|
242 | for (int i = 0; i < items.getLength(); i++) { |
---|
243 | Node item = items.item(i); |
---|
244 | itemPath = item.getAttributes().getNamedItem(PATH_MARK) |
---|
245 | .getNodeValue(); |
---|
246 | columns = new ArrayList<String>(); |
---|
247 | NodeList itemDetails = item.getChildNodes(); |
---|
248 | for (int j = 0; j < itemDetails.getLength(); j++) { |
---|
249 | columns.add(itemDetails.item(j).getTextContent()); |
---|
250 | } |
---|
251 | result.put(itemPath, columns); |
---|
252 | } |
---|
253 | return result; |
---|
254 | } |
---|
255 | } |
---|