~ubuntu-branches/ubuntu/oneiric/jing-trang/oneiric

« back to all changes in this revision

Viewing changes to mod/picl/src/main/com/thaiopensource/validate/picl/SchemaParser.java

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Thibault
  • Date: 2009-09-01 15:53:03 UTC
  • Revision ID: james.westby@ubuntu.com-20090901155303-2kweef05h5v9j3ni
Tags: upstream-20090818
ImportĀ upstreamĀ versionĀ 20090818

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.thaiopensource.validate.picl;
 
2
 
 
3
import com.thaiopensource.util.PropertyMap;
 
4
import com.thaiopensource.util.SinglePropertyMap;
 
5
import com.thaiopensource.util.Localizer;
 
6
import com.thaiopensource.validate.IncorrectSchemaException;
 
7
import com.thaiopensource.validate.Schema;
 
8
import com.thaiopensource.validate.ValidateProperty;
 
9
import com.thaiopensource.validate.Validator;
 
10
import com.thaiopensource.validate.auto.SchemaFuture;
 
11
import com.thaiopensource.xml.sax.CountingErrorHandler;
 
12
import com.thaiopensource.xml.sax.DelegatingContentHandler;
 
13
import com.thaiopensource.xml.util.WellKnownNamespaces;
 
14
import org.xml.sax.Locator;
 
15
import org.xml.sax.SAXException;
 
16
import org.xml.sax.Attributes;
 
17
 
 
18
import java.util.Vector;
 
19
import java.util.Stack;
 
20
 
 
21
class SchemaParser extends DelegatingContentHandler implements SchemaFuture, NamespaceContext {
 
22
  private final Vector constraints = new Vector();
 
23
  private final PropertyMap properties;
 
24
  private final CountingErrorHandler ceh;
 
25
  private Locator locator;
 
26
  private final Stack prefixes = new Stack();
 
27
  private final Localizer localizer = new Localizer(SchemaParser.class);
 
28
  private final PatternParser patternParser;
 
29
 
 
30
  SchemaParser(PropertyMap properties, Schema piclSchema) {
 
31
    this.properties = properties;
 
32
    ceh = new CountingErrorHandler(properties.get(ValidateProperty.ERROR_HANDLER));
 
33
    Validator validator = piclSchema.createValidator(SinglePropertyMap.newInstance(ValidateProperty.ERROR_HANDLER, ceh));
 
34
    setDelegate(validator.getContentHandler());
 
35
    patternParser = new PatternParser(ceh, localizer);
 
36
  }
 
37
 
 
38
  public void setDocumentLocator(Locator locator) {
 
39
    super.setDocumentLocator(locator);
 
40
    this.locator = locator;
 
41
  }
 
42
 
 
43
  public void startDocument()
 
44
          throws SAXException {
 
45
    super.startDocument();
 
46
    prefixes.push("xml");
 
47
    prefixes.push(WellKnownNamespaces.XML);
 
48
  }
 
49
 
 
50
  public void startPrefixMapping(String prefix, String uri)
 
51
          throws SAXException {
 
52
    if (prefix == null)
 
53
      prefix = "";
 
54
    prefixes.push(prefix);
 
55
    if (uri != null && uri.length() == 0)
 
56
      uri = null;
 
57
    prefixes.push(uri);
 
58
    super.startPrefixMapping(prefix, uri);
 
59
  }
 
60
 
 
61
  public void endPrefixMapping(String prefix)
 
62
          throws SAXException {
 
63
    prefixes.pop();
 
64
    prefixes.pop();
 
65
    super.endPrefixMapping(prefix);
 
66
  }
 
67
 
 
68
  public void startElement(String namespaceURI, String localName,
 
69
                           String qName, Attributes atts)
 
70
          throws SAXException {
 
71
    super.startElement(namespaceURI, localName, qName, atts);
 
72
    if (ceh.getHadErrorOrFatalError())
 
73
      return;
 
74
    if (!localName.equals("constraint"))
 
75
      return;
 
76
    String key = atts.getValue("", "key");
 
77
    try {
 
78
      Pattern keyPattern = patternParser.parse(key, locator, this);
 
79
      String ref = atts.getValue("", "ref");
 
80
      if (ref != null) {
 
81
        Pattern refPattern = patternParser.parse(ref, locator, this);
 
82
        constraints.addElement(new KeyRefConstraint(keyPattern, refPattern));
 
83
      }
 
84
      else
 
85
        constraints.addElement(new KeyConstraint(keyPattern));
 
86
    }
 
87
    catch (InvalidPatternException e) {
 
88
    }
 
89
  }
 
90
 
 
91
  public Schema getSchema() throws IncorrectSchemaException {
 
92
    if (ceh.getHadErrorOrFatalError())
 
93
      throw new IncorrectSchemaException();
 
94
    Constraint constraint;
 
95
    if (constraints.size() == 1)
 
96
      constraint = (Constraint)constraints.elementAt(0);
 
97
    else {
 
98
      Constraint[] v = new Constraint[constraints.size()];
 
99
      for (int i = 0; i < v.length; i++)
 
100
        v[i] = (Constraint)constraints.elementAt(i);
 
101
      constraint = new MultiConstraint(v);
 
102
    }
 
103
    return new SchemaImpl(properties, constraint);
 
104
  }
 
105
 
 
106
  public RuntimeException unwrapException(RuntimeException e) {
 
107
    return e;
 
108
  }
 
109
 
 
110
  public String getNamespaceUri(String prefix) {
 
111
    for (int i = prefixes.size(); i > 0; i -= 2) {
 
112
      if (prefixes.elementAt(i - 2).equals(prefix))
 
113
        return (String)prefixes.elementAt(i - 1);
 
114
    }
 
115
    return null;
 
116
  }
 
117
 
 
118
  public String defaultPrefix() {
 
119
    return "";
 
120
  }
 
121
}