1 package ejava.projects.esales.xml;
2
3 import java.io.InputStream;
4 import java.util.HashMap;
5 import java.util.concurrent.Callable;
6
7
8 import javax.xml.XMLConstants;
9 import javax.xml.bind.JAXBContext;
10 import javax.xml.bind.JAXBElement;
11 import javax.xml.bind.JAXBException;
12 import javax.xml.bind.Unmarshaller;
13 import javax.xml.stream.XMLInputFactory;
14 import javax.xml.stream.XMLStreamException;
15 import javax.xml.stream.XMLStreamReader;
16 import javax.xml.transform.stream.StreamSource;
17 import javax.xml.validation.Schema;
18 import javax.xml.validation.SchemaFactory;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.xml.sax.SAXException;
23
24 import com.sun.xml.bind.IDResolver;
25
26
27
28
29
30
31
32
33 public class ESalesParser {
34 @SuppressWarnings("unused")
35 private Log log = LogFactory.getLog(ESalesParser.class);
36 protected XMLInputFactory xmlif = XMLInputFactory.newInstance();
37 protected Unmarshaller um;
38 protected XMLStreamReader xmlr;
39 public static final String SAMPLE_FILE = "xml/eSales-10.xml";
40
41
42
43
44
45
46
47
48
49
50 public ESalesParser(Class<?> rootType, InputStream is)
51
52 throws JAXBException, XMLStreamException {
53 JAXBContext jaxbContext = JAXBContext.newInstance(rootType);
54 um = jaxbContext.createUnmarshaller();
55 xmlif = XMLInputFactory.newInstance();
56 xmlr = xmlif.createXMLStreamReader(is);
57
58
59
60
61 IDResolver idResolver = new IDResolver() {
62 private HashMap<String,Object> idmap = null;
63
64 @SuppressWarnings("rawtypes")
65 @Override
66 public Callable<?> resolve(final String id, Class targetType) throws SAXException {
67 return new Callable() {
68 public Object call() throws Exception {
69 if(idmap==null) return null;
70 return idmap.get(id);
71 }
72 };
73 }
74
75 @Override
76 public void bind(String id, Object obj) throws SAXException {
77 if(idmap==null) idmap = new HashMap<String,Object>();
78 idmap.put(id,obj);
79 };
80 };
81 um.setProperty(IDResolver.class.getName(), idResolver);
82 }
83
84 public void setSchema(InputStream schema) throws SAXException {
85 SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
86 Schema schemaObject = sf.newSchema(new StreamSource(schema));
87 um.setSchema(schemaObject);
88 }
89
90 private boolean contains(String elements[], String localName) {
91 for(String element: elements) {
92 if (element.equalsIgnoreCase(localName)) {
93 return true;
94 }
95 }
96 return false;
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110 public Object getObject(String...elements)
111 throws XMLStreamException, JAXBException {
112 xmlr.next();
113 while (xmlr.hasNext()) {
114 if (xmlr.isStartElement() &&
115 contains(elements, xmlr.getName().getLocalPart())) {
116 Object object = um.unmarshal(xmlr);
117 return (object instanceof JAXBElement) ?
118 ((JAXBElement<?>)object).getValue() : object;
119 }
120 xmlr.next();
121 }
122 return null;
123 }
124
125 public static InputStream getSampleData() {
126 return Thread.currentThread()
127 .getContextClassLoader()
128 .getResourceAsStream(SAMPLE_FILE);
129 }
130 }