source: java/main/src/main/java/com/framsticks/leftovers/FavouritesXMLFactory.java @ 84

Last change on this file since 84 was 84, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • simplification of entities management model
  • cleanup around params (improve hierarchy)
  • migrate from JUnit to TestNG
  • introduce FEST to automatically test GUI
  • improve slider control
  • loosen synchronization between gui tree and backend representation
  • and many other bug fixes

NOTICE:

  • a great many of lines is changed only because of substituting spaces with tabs

CHANGELOG (oldest changes at the bottom):

Some cleaning after fix found.

Fix bug with tree.

More changes with TreeNodes?.

Finally fix issue with tree.

Improve gui tree management.

Decouple update of values from fetch request in gui.

Minor changes.

Minor changes.

Minor change.

Change Path construction wording.

More fixes to SliderControl?.

Fix SliderControl?.

Fix SliderControl?.

Minor improvement.

Several changes.

Make NumberParam? a generic class.

Add robot to the gui test.

Setup common testing logging configuration.

Remove Parameters class.

Remove entityOwner from Parameters.

Move name out from Parameters class.

Move configuration to after the construction.

Simplify observers and endpoints.

Remove superfluous configureEntity overrides.

Add dependency on fest-swing-testng.

Use FEST for final print test.

Use FEST for more concise and readable assertions.

Divide test of F0Parser into multiple methods.

Migrate to TestNG

Minor change.

Change convention from LOGGER to log.

Fix reporting of errors during controls filling.

Bound maximal height of SliderControl?.

Minor improvements.

Improve tooltips for controls.

Also use Delimeted in more places.

Move static control utilities to Gui.

Rename package gui.components to controls.

Some cleaning in controls.

Improve Param classes placing.

Move ValueParam?, PrimitiveParam? and CompositeParam? one package up.

Improve ParamBuilder?.

Move getDef to ValueParam? and PrimitiveParam?.

Move getMax and getDef to ValueParam?.

Move getMin to ValueParam?.

Upgrade to laters apache commons versions.

Use filterInstanceof extensively.

Add instanceof filters.

Make ValueParam? in many places of Param.

Place assertions about ValueParam?.

Add ValueParam?

Rename ValueParam? to PrimitiveParam?

Minor changes.

Several improvements to params types.

Add NumberParam?.

Add TextControl? component.

Add .swp files to .gitignore

Greatly improved slider component.

Some improvements.

Make Param.reassign return also a state.

Add IterableIterator?.

Several changes.

  • Move util classes to better packages.
  • Remove warnings from eclim.

Several improvements.

Fix bug with BooleanParam?.

Some experiments with visualization.

Another fix to panel management.

Improve panel management.

Some refactorization around panels.

Add root class for panel.

File size: 7.1 KB
Line 
1package com.framsticks.leftovers;
2
3import java.io.BufferedWriter;
4import java.io.File;
5import java.io.FileWriter;
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.HashMap;
9import java.util.Iterator;
10import java.util.LinkedHashMap;
11import java.util.Map;
12
13import javax.xml.parsers.DocumentBuilder;
14import javax.xml.parsers.DocumentBuilderFactory;
15import javax.xml.stream.XMLOutputFactory;
16import javax.xml.stream.XMLStreamWriter;
17
18import org.w3c.dom.Document;
19import org.w3c.dom.Node;
20import org.w3c.dom.NodeList;
21
22public 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}
Note: See TracBrowser for help on using the repository browser.