My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright 2012 stanislawbartkowski@gmail.com
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gwtmodel.mapxml;

import com.gwtmodel.table.common.CUtil;
import com.gwtmodel.table.mapxml.DataMapList;
import com.gwtmodel.table.mapxml.IDataContainer;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.Stack;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
* @author hotel Utility class - changes XML file into DataMapList map
*/
public class ChangeXMLToMap {

private ChangeXMLToMap() {

}

/**
* Implementation of SAX handler
*
* @author hotel
*
*/
private static class MyHandler extends DefaultHandler {

private StringBuffer buf;
/** stack of nested XML tags. */
private final Stack<String> st = new Stack<String>();
private final DataMapList<?> d;
private final IXMLTypeFactory fa;
private final Document pattDoc;
private IDataContainer elem = null;
/** if true then 'lines' section is scanned now. */
private boolean lines = false;

MyHandler(IXMLTypeFactory fa, DataMapList<?> d, Document pattDoc) {
this.d = d;
this.fa = fa;
this.pattDoc = pattDoc;
}

/**
* Create XPath pointer basing on current tag nesting as //root
* tag/tag/....
*
* @return XPath pointer
*/
private String toPath() {
String xp = "/";
boolean first = true;
for (String s : st) {
if (!first) {
xp = xp + "/" + s;
}
first = false;
}
return xp;
}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// push tag to stack
st.push(qName);
buf = new StringBuffer();
String tag = toPath();
if (fa.getLinesTag().equals(tag)) {
lines = true;
}
// if 'lines' section and the first tag add next line
if (lines & fa.getLineTag().equals(qName)) {
elem = d.addToLines();
}
}

/**
* Finds type attribute in pattern XML
*
* @return Type string or null meaning no type is defined (default
* string)
*/
private String getType() {
XPathFactory xPathF = XPathFactory.newInstance();
XPath xPath = xPathF.newXPath();
XPathExpression xPathE;
Object res = null;
try {
xPathE = xPath.compile(toPath() + "[@" + IXMLTypeFactory.TYPE
+ "]");
res = xPathE.evaluate(pattDoc, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
e.printStackTrace();
return null;
}
NodeList nlist = (NodeList) res;
for (int i = 0; i < nlist.getLength(); i++) {
Node n = nlist.item(i);
NamedNodeMap map = n.getAttributes();
Node atr = map.getNamedItem(IXMLTypeFactory.TYPE);
return atr.getTextContent();
}
return null;

}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
String val = buf.toString().trim();
String tag = toPath();
if (lines && fa.getLineTag().equals(qName)) {
elem = null;
} else if (lines && fa.getLinesTag().equals(tag)) {
// end of 'lines' section
lines = false;
} else if (!CUtil.EmptyS(val)) {
String attr = getType();
Object o = fa.contruct(attr, val);
if (lines && elem != null) {
elem.put(qName, o);
} else {
d.getdFields().put(toPath(), o);
}
}
buf = new StringBuffer();
// delete element from tag stack
st.pop();
}

@Override
public void characters(char ch[], int start, int length)
throws SAXException {
buf.append(ch, start, length);
}
}

/**
* Only one public method, construct DataMapList map from XML file. In case
* of error throw exception
*
* @param fa
* IXMLTypeFactory
* @param d
* DataMapList to be filled
* @param sXML
* Source XML (as string)
* @param fPattern
* InputStream with pattern XML
*
* @throws ParserConfigurationException
* Exception in case of error
* @throws SAXException
* @throws IOException
*/
public static void constructMapFromXML(IXMLTypeFactory fa,
DataMapList<?> d, String sXML, InputStream fPattern)
throws ParserConfigurationException, SAXException, IOException {

DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.parse(fPattern);
StringReader s = new StringReader(sXML);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new InputSource(s), new MyHandler(fa, d, doc));
}

}

Change log

r552 by stanislawbartkowski on Mar 10, 2012   Diff
Read mail, parameter addEmpty to Box
Go to: 
Project members, sign in to write a code review

Older revisions

r526 by stanislawbartkowski on Jan 1, 2012   Diff
[No log message]
r513 by stanislawbartkowski on Dec 26, 2011   Diff
[No log message]
r508 by stanislawbartkowski on Dec 26, 2011   Diff
Changes related to invoice making
All revisions of this file

File info

Size: 6431 bytes, 192 lines

File properties

svn:mime-type
text/plain
Powered by Google Project Hosting