package com.framsticks.leftovers; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.nio.file.Files; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import com.framsticks.util.FramsticksException; import com.framsticks.util.io.Encoding; public class FavouritesXMLFactory { private static final Logger log = Logger.getLogger(FavouritesXMLFactory.class); private static final String PATH_MARK = "path"; private static final String NAME_MARK = "name"; private static final String FIELD_MARK = "field"; private static final String TYPE_MARK = "type"; public static Node readDocument(InputStream stream) { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(stream); doc.getDocumentElement().normalize(); return doc.getFirstChild(); } catch (ParserConfigurationException | SAXException | IOException e) { throw new FramsticksException().cause(e); } } public static Node readDocument(String filename) { File file = new File(filename); if (!file.exists()) { throw new FramsticksException().msg("file does not exist"); } try { return readDocument(new FileInputStream(file)); } catch (FileNotFoundException e) { throw new FramsticksException().cause(e); } } public static class XmlWriter implements AutoCloseable { BufferedWriter writer = null; XMLStreamWriter xml = null; public XmlWriter(String filename) throws XMLStreamException, IOException { XMLOutputFactory factory = XMLOutputFactory.newInstance(); File file = new File(filename); Files.deleteIfExists(file.toPath()); writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file), Encoding.getDefaultCharset())); xml = factory.createXMLStreamWriter(writer); xml.writeStartDocument("1.0"); } public void close() { try { xml.writeEndDocument(); xml.flush(); xml.close(); writer.close(); } catch (XMLStreamException | IOException e) { log.error(e); } } public void start(String name) throws XMLStreamException { xml.writeStartElement(name); } public void element(String name, Object value) throws XMLStreamException { start(name); xml.writeCharacters(value.toString()); end(); } public void end() throws XMLStreamException { xml.writeEndElement(); } public void attribute(String name, Object value) throws XMLStreamException { xml.writeAttribute(name, value.toString()); } } /** * Writes favourites nodes and fields list to XML file. * * @param path * Path of XML file. * @param nodes * Collection of user favourites nodes and fields. */ public void writeFavouritesXML(String path, Collection nodes) { if (nodes.isEmpty()) { return; } try (XmlWriter xml = new XmlWriter(path)) { xml.start("framsticks"); for (UserFavourite node : nodes) { xml.start("item"); xml.element(PATH_MARK, node.getPath()); xml.element(NAME_MARK, node.getName()); /*if(node.isFavouriteNode()){ xmlWriter.writeStartElement(typeMark); xmlWriter.writeCharacters(Integer.toString(node.getNodeType().getId())); xmlWriter.writeEndElement(); }else{ //isFavouriteField() */ xml.element(FIELD_MARK, node.getField()); xml.end(); } xml.end(); } catch (XMLStreamException | IOException e) { log.error(e); } } /** * Reads user favourites nodes and fields from XML file. * * @param filePath * PAth to XML file. * @return LinkedHashMap contains user favourites nodes and fields. Key is * concatenation of path and field getName. */ public LinkedHashMap readFavouritesXML( String filePath) { Node root = readDocument(filePath); if (root == null) { return null; } LinkedHashMap result = new LinkedHashMap(); NodeList items = root.getChildNodes(); UserFavourite userNode; String path, name, field; for (int i = 0; i < items.getLength(); i++) { Node item = items.item(i); NodeList itemDetails = item.getChildNodes(); if (itemDetails.getLength() == 3) { if (itemDetails.item(0).getNodeName().equals(PATH_MARK) && itemDetails.item(1).getNodeName().equals(NAME_MARK) && (itemDetails.item(2).getNodeName().equals(FIELD_MARK) || itemDetails.item(2).getNodeName().equals(TYPE_MARK))) { path = itemDetails.item(0).getTextContent(); name = itemDetails.item(1).getTextContent(); if (itemDetails.item(2).getNodeName().equals( FIELD_MARK)) { field = itemDetails.item(2).getTextContent(); userNode = new UserFavourite(path, name, field); result.put(path+field, userNode); }else{ try { // int nodeTypeId = Integer.parseInt(itemDetails.item(2).getTextContent()); userNode = new UserFavourite(path, name, null); result.put(path, userNode); } catch (Exception e) { } } } } } return result; } /** * Writes column configuration to XML file. * * @param path * Path to XML file. * @param groupColumns * Map with column configuration. Key - path to group node. Value - * List of columns getId. */ public void writeColumnConfigurationXML(String path, Map> groupColumns) { if (groupColumns.isEmpty()) { return; } try (XmlWriter xml = new XmlWriter(path)) { xml.start("framsticks"); for (Map.Entry> i : groupColumns.entrySet()) { xml.start("item"); xml.attribute(PATH_MARK, i.getKey()); for (String column : i.getValue()) { xml.element("column", column); } xml.end(); } xml.end(); } catch (XMLStreamException | IOException e) { log.error(e); } } /** * Reads column configuration from XML file. * * @param path * Path to XML file. * @return HashMap with configuration. Key - path to group node. Value - * List of columns getId. */ public HashMap> readColumnConfigurationXML( String path) { Node root = readDocument(path); if (root == null) { return null; } HashMap> result = new LinkedHashMap>(); NodeList items = root.getChildNodes(); String itemPath; ArrayList columns; for (int i = 0; i < items.getLength(); i++) { Node item = items.item(i); itemPath = item.getAttributes().getNamedItem(PATH_MARK) .getNodeValue(); columns = new ArrayList(); NodeList itemDetails = item.getChildNodes(); for (int j = 0; j < itemDetails.getLength(); j++) { columns.add(itemDetails.item(j).getTextContent()); } result.put(itemPath, columns); } return result; } }