1
package com.thaiopensource.validate.picl;
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;
18
import java.util.Vector;
19
import java.util.Stack;
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;
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);
38
public void setDocumentLocator(Locator locator) {
39
super.setDocumentLocator(locator);
40
this.locator = locator;
43
public void startDocument()
45
super.startDocument();
47
prefixes.push(WellKnownNamespaces.XML);
50
public void startPrefixMapping(String prefix, String uri)
54
prefixes.push(prefix);
55
if (uri != null && uri.length() == 0)
58
super.startPrefixMapping(prefix, uri);
61
public void endPrefixMapping(String prefix)
65
super.endPrefixMapping(prefix);
68
public void startElement(String namespaceURI, String localName,
69
String qName, Attributes atts)
71
super.startElement(namespaceURI, localName, qName, atts);
72
if (ceh.getHadErrorOrFatalError())
74
if (!localName.equals("constraint"))
76
String key = atts.getValue("", "key");
78
Pattern keyPattern = patternParser.parse(key, locator, this);
79
String ref = atts.getValue("", "ref");
81
Pattern refPattern = patternParser.parse(ref, locator, this);
82
constraints.addElement(new KeyRefConstraint(keyPattern, refPattern));
85
constraints.addElement(new KeyConstraint(keyPattern));
87
catch (InvalidPatternException e) {
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);
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);
103
return new SchemaImpl(properties, constraint);
106
public RuntimeException unwrapException(RuntimeException e) {
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);
118
public String defaultPrefix() {