1 package org.sat4j.csp.xml;
2 import org.xml.sax.Attributes;
3
4 class Parameters extends Element {
5
6 private StringBuilder allParameters;
7
8 private InstanceParser ip;
9
10 public Parameters(ICSPCallback out, String tagName, InstanceParser ip) {
11 super(out,tagName);
12 this.ip = ip;
13 }
14
15 public void startElement(Attributes att) {
16 allParameters = new StringBuilder();
17 }
18
19 public void characters(String s) {
20 allParameters.append(s);
21 }
22
23 public void endElement() {
24 if (ip.getParentElement().tagName().equals("constraint"))
25 effectiveParameters();
26 else
27 formalParameters();
28 }
29
30 private void formalParameters() {
31 String[] tokens = allParameters.toString().trim().split("\\s+");
32 int i = 0;
33 String type;
34 String name;
35 while (i < tokens.length && !tokens[i].equals("")) {
36 type = tokens[i];
37
38 if (!type.equals("int"))
39 throw new CSPFormatException(type
40 + " type for parameters not supported");
41 i++;
42 if (i == tokens.length || tokens[i].equals(""))
43 throw new CSPFormatException("a parameter name is missing.");
44 name = tokens[i];
45 getCB().addFormalParameter(name, type);
46 i++;
47 }
48 }
49
50 private void effectiveParameters() {
51 String[] tokens = allParameters.toString().trim().split("\\s+");
52 for (String tok : tokens) {
53 if (!tok.equals("")) {
54 try {
55 getCB().addEffectiveParameter(Integer.parseInt(tok));
56 } catch (NumberFormatException e) {
57 getCB().addEffectiveParameter(tok);
58 }
59 }
60 }
61 }
62 }