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