]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/java/org/open_ils/idl/IDLParser.java
Typo patch from Dan Scott, correcting the Namespace URI for the persistEnce
[Evergreen.git] / Open-ILS / src / java / org / open_ils / idl / IDLParser.java
1 package org.open_ils.idl;
2
3 import org.opensrf.util.*;
4
5 import java.util.HashMap;
6 import java.util.Set;
7 import java.util.Iterator;
8
9 import java.io.InputStream;
10 import java.io.FileInputStream;
11 import java.io.IOException;
12
13 import javax.xml.stream.*;
14 import javax.xml.stream.events.* ;
15 import javax.xml.namespace.QName;
16
17
18 public class IDLParser {
19
20     public static final String OILS_NS_BASE="http://opensrf.org/spec/IDL/base/v1";
21     public static final String OILS_NS_OBJ="http://open-ils.org/spec/opensrf/IDL/objects/v1";
22     public static final String OILS_NS_OBJ_PREFIX="oils_obj";
23     public static final String OILS_NS_PERSIST="http://open-ils.org/spec/opensrf/IDL/persistence/v1";
24     public static final String OILS_NS_PERSIST_PREFIX="oils_persist";
25     public static final String OILS_NS_REPORTER="http://open-ils.org/spec/opensrf/IDL/reporter/v1";
26     public static final String OILS_NS_REPORTER_PREFIX="reporter";
27
28     /** The source for the IDL XML */
29     InputStream inStream;
30     HashMap<String, IDLObject> IDLObjects;
31     IDLObject current;
32
33     /** If true, we retain the full set of IDL objects in memory.  This is true by default. */
34     private boolean keepIDLObjects;
35
36     public IDLParser() {
37         IDLObjects = new HashMap<String, IDLObject>();
38         keepIDLObjects = true;
39     }
40
41     public IDLParser(String fileName) throws IOException {
42         this(new FileInputStream(fileName));
43     }
44
45     public IDLParser(InputStream inStream) {
46         this();
47         this.inStream = inStream;
48     }
49
50     /**
51     * Parses the IDL XML
52     */
53     public void parse() throws IOException {
54     
55         try {
56             XMLInputFactory factory = XMLInputFactory.newInstance();
57     
58             /** disable as many unused features as possible to speed up the parsing */
59             factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
60             factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
61             factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
62             factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
63             factory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
64     
65     
66         /** create the stream reader */
67             XMLStreamReader reader = factory.createXMLStreamReader(this.inStream);
68             int eventType;
69     
70             while(reader.hasNext()) {
71                 /** cycle through the XML events */
72     
73                 eventType = reader.next();
74     
75                 switch(eventType) {
76     
77                     case XMLEvent.START_ELEMENT:
78                         handleStartElement(reader);
79                         break;
80     
81                     case XMLEvent.END_ELEMENT: 
82                         handleEndElement(reader);
83                         break;
84                 }
85             }
86
87         } catch(javax.xml.stream.XMLStreamException se) {
88             /* throw local exception */
89         }
90    }
91
92    /**
93     * Returns the IDLObject with the given IDLClass 
94     */
95    public IDLObject getObject(String IDLClass) {
96       return (IDLObject) IDLObjects.get(IDLClass);
97    }
98
99     public void handleStartElement(XMLStreamReader reader) {
100
101         if(!OILS_NS_BASE.equals(reader.getNamespaceURI())) return;
102         String localpart = reader.getLocalName();
103     
104         if( "class".equals(localpart) ) {
105             current = new IDLObject();
106             current.setIDLClass(reader.getAttributeValue(null, "id"));
107             current.setController(reader.getAttributeValue(null, "controller"));
108             String persist = reader.getAttributeValue(OILS_NS_PERSIST, "virtual");
109             current.setIsVirtual("persist".equals(reader.getAttributeValue(OILS_NS_PERSIST, "virtual")));
110             return;
111         }
112     
113         if( "field".equals(localpart) ) {
114             IDLField field = new IDLField();
115             field.setName(reader.getAttributeValue(null, "name"));
116             field.setArrayPos(new Integer(reader.getAttributeValue(OILS_NS_OBJ, "array_position")));
117             field.setIsVirtual("true".equals(reader.getAttributeValue(OILS_NS_PERSIST, "virtual")));
118             current.addField(field);
119         }
120
121         if( "link".equals(localpart) ) {
122             IDLLink link = new IDLLink();
123             link.setField(reader.getAttributeValue(null, "field"));
124             link.setReltype(reader.getAttributeValue(null, "reltype"));
125             link.setKey(reader.getAttributeValue(null, "key"));
126             link.setMap(reader.getAttributeValue(null, "map"));
127             link.setIDLClass(reader.getAttributeValue(null, "class"));
128             current.addLink(link);
129         }
130     }
131
132     public void handleEndElement(XMLStreamReader reader) {
133
134         if(!OILS_NS_BASE.equals(reader.getNamespaceURI())) return;
135         String localpart = reader.getLocalName();
136
137         if("class".equals(localpart)) {
138
139             if(keepIDLObjects)
140                 IDLObjects.put(current.getIDLClass(), current);
141
142             HashMap fields = current.getFields();
143             String fieldNames[] = new String[fields.size()];
144
145             for(Iterator itr = fields.keySet().iterator(); itr.hasNext(); ) {
146                 String key = (String) itr.next();
147                 IDLField field = (IDLField) fields.get(key);
148                 fieldNames[ field.getArrayPos() ] = field.getName();
149             }
150
151             OSRFRegistry.registerObject(
152                 current.getIDLClass(), OSRFRegistry.WireProtocol.ARRAY, fieldNames);
153
154             current = null;
155         }
156     }
157
158
159     public String toXML() {
160         StringBuffer sb = new StringBuffer();
161         Set keys = IDLObjects.keySet();
162         Iterator itr = IDLObjects.keySet().iterator();
163         String IDLClass;
164         IDLObject obj;
165         while(itr.hasNext()) {
166             IDLClass = (String) itr.next();
167             obj = IDLObjects.get(IDLClass);
168             obj.toXML(sb);
169         }
170         return sb.toString();
171     }
172 }
173
174
175
176
177
178