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