001: /**
002: * Miroslav Popov, Apr 6, 2006 miroslav.popov@gmail.com
003: */package org.enhydra.shark.xpdl;
004:
005: import java.util.Iterator;
006: import java.util.List;
007:
008: import org.enhydra.shark.xpdl.elements.Condition;
009: import org.enhydra.shark.xpdl.elements.Deadlines;
010: import org.enhydra.shark.xpdl.elements.ExtendedAttribute;
011: import org.enhydra.shark.xpdl.elements.ImplementationTypes;
012: import org.enhydra.shark.xpdl.elements.Namespace;
013: import org.enhydra.shark.xpdl.elements.Namespaces;
014: import org.enhydra.shark.xpdl.elements.Package;
015: import org.enhydra.shark.xpdl.elements.SchemaType;
016: import org.enhydra.shark.xpdl.elements.Tools;
017: import org.w3c.dom.Attr;
018: import org.w3c.dom.Document;
019: import org.w3c.dom.Element;
020: import org.w3c.dom.NamedNodeMap;
021: import org.w3c.dom.Node;
022: import org.w3c.dom.NodeList;
023:
024: /**
025: * @author Miroslav Popov
026: */
027: public class XPDLRepositoryHandler {
028:
029: protected static boolean logging = false;
030:
031: protected String xpdlPrefix = "";
032:
033: public void setXPDLPrefixEnabled(boolean enable) {
034: if (enable) {
035: this .xpdlPrefix = "xpdl:";
036: } else {
037: this .xpdlPrefix = "";
038: }
039: }
040:
041: public boolean isXPDLPrefixEnabled() {
042: return "xpdl:".equals(this .xpdlPrefix);
043: }
044:
045: public void fromXML(Element node, Package pkg) {
046: // long t1,t2;
047: // t1=System.currentTimeMillis();
048: NamedNodeMap attribs = node.getAttributes();
049: Namespaces nss = pkg.getNamespaces();
050: for (int i = 0; i < attribs.getLength(); i++) {
051: Node n = attribs.item(i);
052: String nn = n.getNodeName();
053: if (nn.startsWith("xmlns:") && !nn.equals("xmlns:xsi")) {
054: Namespace ns = (Namespace) nss.generateNewElement();
055: ns.setName(nn.substring(6, nn.length()));
056: fromXML(n, (XMLAttribute) ns.get("location"));
057: nss.add(ns);
058: }
059: }
060: fromXML(node, (XMLComplexElement) pkg);
061: // t2=System.currentTimeMillis();
062: // System.out.println("MFXML="+(t2-t1));
063: }
064:
065: public void fromXML(Node node, XMLCollection cel) {
066: if (node == null || !node.hasChildNodes())
067: return;
068: String nameSpacePrefix = XMLUtil.getNameSpacePrefix(node);
069:
070: XMLElement newOne = cel.generateNewElement();
071: String elName = newOne.toName();
072:
073: NodeList children = node.getChildNodes();
074: int lng = children.getLength();
075: if (logging)
076: System.out.println("FROMXML for " + cel.toName() + ", c="
077: + cel.getClass().getName());
078: for (int i = 0; i < lng; i++) {
079: Node child = children.item(i);
080: if (child.getNodeName().equals(nameSpacePrefix + elName)) {
081: // System.out.println("I="+i);
082: newOne = cel.generateNewElement();
083: // System.out.println("Now the collection element of collection
084: // "+node.getNodeName()+" will be processed");
085: if (newOne instanceof XMLComplexElement) {
086: fromXML(children.item(i),
087: (XMLComplexElement) newOne);
088: } else {
089: fromXML(children.item(i), (XMLSimpleElement) newOne);
090: }
091: cel.add(newOne);
092: } else {
093: // System.err.println("Something's wrong with collection "+elName+" parsing -
094: // c="+cel.getClass().getName()+" !");
095: }
096: }
097: // System.out.println("The length of collection "+name+" after importing
098: // is"+size());
099: }
100:
101: public void fromXML(Node node, XMLComplexElement cel) {
102: if (node == null
103: || (!node.hasChildNodes() && !node.hasAttributes()))
104: return;
105:
106: String nameSpacePrefix = node.getPrefix();
107: if (nameSpacePrefix != null) {
108: nameSpacePrefix += ":";
109: } else {
110: nameSpacePrefix = "";
111: }
112: if (logging)
113: System.out.println("FROMXML for " + cel.toName() + ", c="
114: + cel.getClass().getName());
115:
116: if (node.hasAttributes()) {
117: NamedNodeMap attribs = node.getAttributes();
118: for (int i = 0; i < attribs.getLength(); ++i) {
119: Node attrib = attribs.item(i);
120: try {
121: // System.out.println("Getting attrib "+attrib.getNodeName());
122: fromXML(attrib, (XMLAttribute) cel.get(attrib
123: .getNodeName()));
124: } catch (NullPointerException npe) {
125: /*
126: * System.err.println("Processing attributes for "+ cel.toName() +" element
127: * having problems with " + attrib.getNodeName()+" attribute\n" +
128: * attrib.getNodeValue().trim());
129: */
130: }
131: }
132: }
133: // getting elements
134: if (node.hasChildNodes()) {
135: // Specific code for handling Condition element - we don't support Xpression
136: // element
137: if (cel instanceof Condition) {
138: String newVal = node.getChildNodes().item(0)
139: .getNodeValue();
140: if (newVal != null) {
141: cel.setValue(newVal);
142: }
143: }
144: // Specific code for handling SchemaType element
145: if (cel instanceof SchemaType) {
146: NodeList nl = node.getChildNodes();
147: for (int j = 0; j < nl.getLength(); j++) {
148: Node sn = nl.item(j);
149: if (sn instanceof Element) {
150: cel.setValue(XMLUtil.getContent(sn, true));
151: break;
152: }
153: }
154: }
155: // Specific code for handling ExtendedAttribute element
156: if (cel instanceof ExtendedAttribute) {
157: cel.setValue(XMLUtil.getChildNodesContent(node));
158: }
159: Iterator it = cel.getXMLElements().iterator();
160: while (it.hasNext()) {
161: XMLElement el = (XMLElement) it.next();
162: String elName = el.toName();
163: if (el instanceof XMLComplexElement) {
164: Node child = XMLUtil.getChildByName(node,
165: nameSpacePrefix + elName);
166: fromXML(child, (XMLComplexElement) el);
167: // Specific case if element is Deadlines
168: } else if (el instanceof Deadlines) {
169: fromXML(node, (XMLCollection) el);
170: } else if (el instanceof XMLCollection) {
171: Node child = XMLUtil.getChildByName(node,
172: nameSpacePrefix + elName);
173: fromXML(child, (XMLCollection) el);
174: } else if (el instanceof XMLComplexChoice) {
175: fromXML(node, (XMLComplexChoice) el);
176: } else if (el instanceof XMLSimpleElement) {
177: Node child = XMLUtil.getChildByName(node,
178: nameSpacePrefix + elName);
179: fromXML(child, (XMLSimpleElement) el);
180: }
181: }
182: }
183: }
184:
185: public void fromXML(Node node, XMLComplexChoice el) {
186: String nameSpacePrefix = XMLUtil.getNameSpacePrefix(node);
187: List ch = el.getChoices();
188: if (logging)
189: System.out.println("FROMXML for " + el.toName() + ", c="
190: + el.getClass().getName());
191: for (int i = 0; i < ch.size(); i++) {
192: XMLElement chc = (XMLElement) ch.get(i);
193: String chname = chc.toName();
194: // Specific code for handling Tools
195: if (chname.equals("Tools")) {
196: chname = "Tool";
197: }
198: Node child = XMLUtil.getChildByName(node, nameSpacePrefix
199: + chname);
200: if (child != null) {
201: if (chc instanceof XMLComplexElement) {
202: fromXML(child, (XMLComplexElement) chc);
203: } else { // it is XMLCollection
204: // Specific code for handling Tools
205: if (chc instanceof Tools) {
206: fromXML(node, (XMLCollection) chc);
207: } else {
208: fromXML(child, (XMLCollection) chc);
209: }
210: }
211: el.setChoosen(chc);
212: break;
213: }
214: }
215: }
216:
217: public void fromXML(Node node, XMLSimpleElement el) {
218: fromXMLBasic(node, el);
219: }
220:
221: public void fromXML(Node node, XMLAttribute el) {
222: fromXMLBasic(node, el);
223: }
224:
225: public void fromXMLBasic(Node node, XMLElement el) {
226: if (node != null) {
227: if (logging)
228: System.out.println("FROMXML for " + el.toName()
229: + ", c=" + el.getClass().getName());
230: String newVal;
231: if (node.hasChildNodes()) {
232: newVal = node.getChildNodes().item(0).getNodeValue();
233: if (logging)
234: System.out.println("11111");
235: // should never happen
236: } else {
237: if (logging)
238: System.out.println("22222");
239: newVal = node.getNodeValue();
240: }
241: if (logging)
242: System.out.println("NV=" + newVal);
243:
244: if (newVal != null) {
245: el.setValue(newVal);
246: }
247: }
248: }
249:
250: public void toXML(Document parent, Package pkg) {
251: Node node = parent.createElement(xpdlPrefix + pkg.toName());
252: ((Element) node).setAttribute("xmlns", XMLUtil.XMLNS);
253: // ((Element) node).setAttribute("xmlns:xpdl", XMLNS_XPDL);
254: // save additional namespaces
255: Iterator itNs = pkg.getNamespaces().toElements().iterator();
256: while (itNs.hasNext()) {
257: Namespace ns = (Namespace) itNs.next();
258: ((Element) node).setAttribute("xmlns:" + ns.getName(), ns
259: .getLocation());
260: }
261: ((Element) node).setAttribute("xmlns:xsi", XMLUtil.XMLNS_XSI);
262: ((Element) node).setAttribute("xsi:schemaLocation",
263: XMLUtil.XSI_SCHEMA_LOCATION);
264:
265: toXML(node, pkg);
266: parent.appendChild(node);
267: }
268:
269: public void toXML(Node parent, XMLCollection cel) {
270: if (!cel.isEmpty() || cel.isRequired()) {
271: if (parent != null) {
272: if (logging)
273: System.out.println("TOXML for " + cel.toName()
274: + ", c=" + cel.getClass().getName()
275: + ", parent=" + cel.getParent() + ", size="
276: + cel.size());
277: String elName = cel.toName();
278: Node node = parent;
279: // Specific code for handling Deadlines and Tools
280: if (!(elName.equals("Deadlines") || elName
281: .equals("Tools"))) {
282: node = (parent.getOwnerDocument())
283: .createElement(xpdlPrefix + elName);
284: }
285: for (Iterator it = cel.toElements().iterator(); it
286: .hasNext();) {
287: XMLElement el = (XMLElement) it.next();
288: if (el instanceof XMLSimpleElement) {
289: toXML(node, (XMLSimpleElement) el);
290: } else {
291: toXML(node, (XMLComplexElement) el);
292: }
293: }
294: // If Deadlines or Tools are handled, node==parent
295: if (node != parent) {
296: parent.appendChild(node);
297: }
298: }
299: }
300: }
301:
302: public void toXML(Node parent, XMLComplexElement cel) {
303: if (cel.isEmpty() && !cel.isRequired())
304: return;
305: if (parent != null) {
306: if (logging)
307: System.out.println("TOXML for " + cel.toName() + ", c="
308: + cel.getClass().getName() + ", parent="
309: + cel.getParent());
310: Node node = parent;
311: // Specific code for handling Package
312: if (!(cel instanceof Package)) {
313: node = (parent.getOwnerDocument())
314: .createElement(xpdlPrefix + cel.toName());
315: }
316: if (cel.toValue() != null && cel.toValue().length() > 0) {
317: // Specific code for handling Condition
318: if (cel instanceof Condition) {
319: if (!cel.toValue().equals("")) {
320: Node textNode = node.getOwnerDocument()
321: .createTextNode(cel.toValue());
322: node.appendChild(textNode);
323: }
324: }
325: // Specific code for handling SchemaType
326: if (cel instanceof SchemaType) {
327: Node schema = XMLUtil.parseSchemaNode(
328: cel.toValue(), false);
329: if (schema != null) {
330: node.appendChild(node.getOwnerDocument()
331: .importNode(schema, true));
332: }
333: }
334: // Specific code for handling ExtendedAttribute
335: if (cel instanceof ExtendedAttribute) {
336: try {
337: Node n = XMLUtil
338: .parseExtendedAttributeContent(cel
339: .toValue());
340: NodeList nl = n.getChildNodes();
341: for (int i = 0; i < nl.getLength(); i++) {
342: node.appendChild(parent.getOwnerDocument()
343: .importNode(nl.item(i), true));
344: }
345: } catch (Exception ex) {
346: }
347: }
348: }
349: for (Iterator it = cel.toElements().iterator(); it
350: .hasNext();) {
351: XMLElement el = (XMLElement) it.next();
352: if (el instanceof XMLComplexElement) {
353: toXML(node, (XMLComplexElement) el);
354: } else if (el instanceof XMLCollection) {
355: toXML(node, (XMLCollection) el);
356: } else if (el instanceof XMLComplexChoice) {
357: toXML(node, (XMLComplexChoice) el);
358: } else if (el instanceof XMLSimpleElement) {
359: toXML(node, (XMLSimpleElement) el);
360: } else { // it's XMLAttribute
361: toXML(node, (XMLAttribute) el);
362: }
363: }
364: // If Package is handled, parent==node
365: if (node != parent) {
366: parent.appendChild(node);
367: }
368: }
369: }
370:
371: public void toXML(Node parent, XMLComplexChoice el) {
372: XMLElement choosen = el.getChoosen();
373: if (logging)
374: System.out.println("TOXML for " + el.toName() + ", c="
375: + el.getClass().getName() + ", parent="
376: + el.getParent());
377: if (choosen != null) {
378: if (choosen instanceof XMLComplexElement) {
379: toXML(parent, (XMLComplexElement) choosen);
380: } else {
381: if (choosen.toName().equals("Tools")
382: && ((Tools) choosen).size() == 0) {
383: toXML(parent, ((ImplementationTypes) el).getNo());
384: } else {
385: toXML(parent, (XMLCollection) choosen);
386: }
387: }
388: }
389: }
390:
391: public void toXML(Node parent, XMLSimpleElement el) {
392: if (!el.isEmpty() || el.isRequired()) {
393: if (parent != null) {
394: if (logging)
395: System.out.println("TOXML for " + el.toName()
396: + ", c=" + el.getClass().getName()
397: + ", parent=" + el.getParent() + ", val="
398: + el.toValue());
399: Node node = (parent.getOwnerDocument())
400: .createElement(xpdlPrefix + el.toName());
401: node.appendChild(parent.getOwnerDocument()
402: .createTextNode(el.toValue().trim()));
403: parent.appendChild(node);
404: }
405: }
406: }
407:
408: public void toXML(Node parent, XMLAttribute el) {
409: if (!el.isEmpty() || el.isRequired()) {
410: if (parent != null) {
411: if (logging)
412: System.out.println("TOXML for " + el.toName()
413: + ", c=" + el.getClass().getName()
414: + ", parent=" + el.getParent() + ", val="
415: + el.toValue());
416: Attr node = (parent.getOwnerDocument())
417: .createAttribute(el.toName());
418: node.setValue(el.toValue().trim());
419: ((Element) parent).setAttributeNode(node);
420: }
421: }
422: }
423: }
|