1 package org.sat4j.csp.xml;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.Vector;
6
7 import org.xml.sax.Attributes;
8 import org.xml.sax.SAXParseException;
9 import org.xml.sax.helpers.DefaultHandler;
10
11 class InstanceParser extends DefaultHandler {
12
13
14 private Vector<Element> parents = new Vector<Element>();
15
16
17 private Map<String, Element> theElts;
18
19 public InstanceParser(ICSPCallback cb) {
20 theElts = new HashMap<String, Element>();
21 theElts.put("instance", new Instance(cb, "instance"));
22 theElts.put("presentation", new Presentation(cb, "presentation"));
23 theElts.put("domains", new Domains(cb, "domains"));
24 theElts.put("domain", new Domain(cb, "domain"));
25 theElts.put("variables", new Variables(cb, "variables"));
26 theElts.put("variable", new Variable(cb, "variable"));
27 theElts.put("relations", new Relations(cb, "relations"));
28 theElts.put("relation", new Relation(cb, "relation"));
29 theElts.put("predicates", new Predicates(cb, "predicates"));
30 theElts.put("predicate", new Predicate(cb, "predicate"));
31 theElts.put("parameters", new Parameters(cb, "parameters", this));
32 theElts.put("expression", new Expression(cb, "expression"));
33 theElts.put("functional", new Functional(cb, "functional"));
34 theElts.put("constraints", new Constraints(cb, "constraints"));
35 theElts.put("constraint", new Constraint(cb, "constraint"));
36 theElts.put("list", new ListOfParameters(cb, "list"));
37 theElts.put("cst", new ConstantParameter(cb, "cst"));
38 }
39
40
41 public void characters(char[] ch, int start, int length) {
42 parents.lastElement().characters(new String(ch, start, length));
43 }
44
45
46 public void endElement(String uri, String localName, String qName) {
47 Element current = theElts.get(qName);
48 if (current != null) {
49 current.endElement();
50 assert current.tagName().equals(parents.lastElement().tagName());
51 parents.remove(parents.size() - 1);
52 } else
53 throw new CSPFormatException(qName + " : undefined tag");
54 }
55
56
57 public void startElement(String uri, String localName, String qName,
58 Attributes atts) {
59 Element current = theElts.get(qName);
60 if (current != null) {
61 parents.add(current);
62 current.startElement(atts);
63 } else
64 throw new CSPFormatException(qName + " : undefined tag");
65 }
66
67 public Element getParentElement() {
68 if (parents.size() >= 2)
69 return parents.get(parents.size() - 2);
70 else
71 return null;
72 }
73
74
75
76
77 public void error(SAXParseException exception) {
78 System.out.println("error");
79 System.out.println(exception.getMessage());
80 System.out.println(exception);
81 System.out.println("Colonne : " + exception.getColumnNumber()
82 + " Ligne : " + exception.getLineNumber());
83 System.exit(1);
84 }
85
86
87
88
89 public void fatalError(SAXParseException exception) {
90 System.out.println("fatalError");
91 System.out.println(exception.getMessage());
92 System.out.println(exception);
93 System.out.println("Colonne : " + exception.getColumnNumber()
94 + " Ligne : " + exception.getLineNumber());
95 System.exit(1);
96 }
97
98
99
100
101 public void warning(SAXParseException exception) {
102 System.out.println("warning");
103 System.out.println(exception.getMessage());
104 System.out.println(exception);
105 System.out.println("Colonne : " + exception.getColumnNumber()
106 + " Ligne : " + exception.getLineNumber());
107 }
108
109 }