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