1
2
3
4 package net.sf.bddbddb;
5
6 import java.util.Iterator;
7 import java.util.LinkedList;
8 import java.util.List;
9 import java.io.BufferedWriter;
10 import java.io.FileWriter;
11 import java.io.IOException;
12 import java.io.Writer;
13 import net.sf.bddbddb.FindBestDomainOrder.ConstraintInfoCollection;
14 import net.sf.bddbddb.order.OrderConstraint;
15 import net.sf.bddbddb.order.EpisodeCollection;
16
17 import org.jdom.Document;
18 import org.jdom.Element;
19 import org.jdom.output.Format;
20 import org.jdom.output.XMLOutputter;
21
22 /***
23 * XMLFactory
24 *
25 * @author jwhaley
26 * @version $Id: XMLFactory.java 450 2005-03-07 20:49:47Z cs343 $
27 */
28 public class XMLFactory {
29
30 Solver solver;
31
32 XMLFactory(Solver s) {
33 solver = s;
34 }
35
36 public InferenceRule getRule(String s) {
37 return solver.getRule(Integer.parseInt(s.substring(4)));
38 }
39
40 public Relation getRelation(String s) {
41 return (Relation) solver.nameToRelation.get(s);
42 }
43
44 public Object fromXML(Element e) {
45 String name = e.getName();
46 Object o;
47 if (name.equals("trialCollections") || name.equals("episodeCollections")) {
48 String fn = e.getAttributeValue("datalog");
49 List results = new LinkedList();
50 if (fn.equals(solver.inputFilename)) {
51 for (Iterator i = e.getContent().iterator(); i.hasNext(); ) {
52 Object q = i.next();
53 if (q instanceof Element) {
54 Element e2 = (Element) q;
55 if (name.equals("trialCollection") || e2.getName().equals("episodeCollection")) {
56 results.add(fromXML(e2));
57 }
58 }
59 }
60 }
61 o = results;
62 } else if (name.equals("trialCollection") || name.equals("episodeCollection")) {
63 EpisodeCollection tc = EpisodeCollection.fromXMLElement(e, solver);
64 o = tc;
65 } else if (name.equals("findBestOrder")) {
66 ConstraintInfoCollection c = null;
67 for (Iterator i = e.getContent().iterator(); i.hasNext(); ) {
68 Object q = i.next();
69 if (q instanceof Element) {
70 Element e2 = (Element) q;
71 if (e2.getName().equals("constraintInfoCollection")) {
72 c = (ConstraintInfoCollection) fromXML(e2);
73 }
74 }
75 }
76 if (c == null) c = new ConstraintInfoCollection(solver);
77 o = new FindBestDomainOrder(c);
78 } else if (name.equals("rule")) {
79 o = InferenceRule.fromXMLElement(e, solver);
80 } else if (name.equals("relation")) {
81 o = Relation.fromXMLElement(e, this);
82 } else if (name.equals("attribute")) {
83 o = Attribute.fromXMLElement(e, this);
84 } else if (name.equals("variable")) {
85 o = Variable.fromXMLElement(e, this);
86 } else if (name.endsWith("Constraint")) {
87 o = OrderConstraint.fromXMLElement(e, this);
88 } else {
89 throw new IllegalArgumentException("Cannot parse XML element "+name);
90 }
91 return o;
92 }
93
94 public static void dumpXML(String filename, Element e) {
95 Writer w = null;
96 try {
97 w = new BufferedWriter(new FileWriter(filename));
98 dumpXML(w, e);
99 w.close();
100 } catch (IOException x) {
101 System.err.println("Error writing "+filename+": "+x);
102 x.printStackTrace();
103 } finally {
104 try { if (w != null) w.close(); } catch (IOException _) { }
105 }
106 }
107
108 /***
109 * Dumps the given element as an XML document to the given writer.
110 *
111 * @param out output writer
112 * @param e element to write
113 */
114 public static void dumpXML(Writer out, Element e) throws IOException {
115 Document d = new Document(e);
116 XMLOutputter serializer = new XMLOutputter();
117 serializer.setFormat(Format.getPrettyFormat());
118 serializer.output(d, out);
119 }
120
121 }