| 1 |
package com.thaiopensource.validate.rng.impl; |
| 2 |
|
| 3 |
import com.thaiopensource.datatype.DatatypeLibraryLoader; |
| 4 |
import com.thaiopensource.relaxng.parse.IllegalSchemaException; |
| 5 |
import com.thaiopensource.relaxng.parse.Parseable; |
| 6 |
import com.thaiopensource.relaxng.pattern.AnnotationsImpl; |
| 7 |
import com.thaiopensource.relaxng.pattern.CommentListImpl; |
| 8 |
import com.thaiopensource.relaxng.pattern.FeasibleTransform; |
| 9 |
import com.thaiopensource.relaxng.pattern.IdTypeMap; |
| 10 |
import com.thaiopensource.relaxng.pattern.IdTypeMapBuilder; |
| 11 |
import com.thaiopensource.relaxng.pattern.NameClass; |
| 12 |
import com.thaiopensource.relaxng.pattern.Pattern; |
| 13 |
import com.thaiopensource.relaxng.pattern.PatternDumper; |
| 14 |
import com.thaiopensource.relaxng.pattern.SchemaBuilderImpl; |
| 15 |
import com.thaiopensource.relaxng.pattern.SchemaPatternBuilder; |
| 16 |
import com.thaiopensource.resolver.xml.sax.SAXResolver; |
| 17 |
import com.thaiopensource.util.PropertyId; |
| 18 |
import com.thaiopensource.util.PropertyMap; |
| 19 |
import com.thaiopensource.util.VoidValue; |
| 20 |
import com.thaiopensource.validate.AbstractSchema; |
| 21 |
import com.thaiopensource.validate.AbstractSchemaReader; |
| 22 |
import com.thaiopensource.validate.CombineSchema; |
| 23 |
import com.thaiopensource.validate.IncorrectSchemaException; |
| 24 |
import com.thaiopensource.validate.Option; |
| 25 |
import com.thaiopensource.validate.ResolverFactory; |
| 26 |
import com.thaiopensource.validate.Schema; |
| 27 |
import com.thaiopensource.validate.ValidateProperty; |
| 28 |
import com.thaiopensource.validate.prop.rng.RngProperty; |
| 29 |
import com.thaiopensource.validate.prop.wrap.WrapProperty; |
| 30 |
import org.relaxng.datatype.DatatypeLibraryFactory; |
| 31 |
import org.xml.sax.ErrorHandler; |
| 32 |
import org.xml.sax.Locator; |
| 33 |
import org.xml.sax.SAXException; |
| 34 |
|
| 35 |
import javax.xml.transform.sax.SAXSource; |
| 36 |
import java.io.IOException; |
| 37 |
|
| 38 |
public abstract class SchemaReaderImpl extends AbstractSchemaReader { |
| 39 |
private static final PropertyId<?>[] supportedPropertyIds = { |
| 40 |
ValidateProperty.XML_READER_CREATOR, |
| 41 |
ValidateProperty.ERROR_HANDLER, |
| 42 |
ValidateProperty.ENTITY_RESOLVER, |
| 43 |
ValidateProperty.URI_RESOLVER, |
| 44 |
ValidateProperty.RESOLVER, |
| 45 |
RngProperty.DATATYPE_LIBRARY_FACTORY, |
| 46 |
RngProperty.CHECK_ID_IDREF, |
| 47 |
RngProperty.FEASIBLE, |
| 48 |
WrapProperty.ATTRIBUTE_OWNER, |
| 49 |
}; |
| 50 |
|
| 51 |
public Schema createSchema(SAXSource source, PropertyMap properties) |
| 52 |
throws IOException, SAXException, IncorrectSchemaException { |
| 53 |
SchemaPatternBuilder spb = new SchemaPatternBuilder(); |
| 54 |
SAXResolver resolver = ResolverFactory.createResolver(properties); |
| 55 |
ErrorHandler eh = properties.get(ValidateProperty.ERROR_HANDLER); |
| 56 |
DatatypeLibraryFactory dlf = properties.get(RngProperty.DATATYPE_LIBRARY_FACTORY); |
| 57 |
if (dlf == null) |
| 58 |
dlf = new DatatypeLibraryLoader(); |
| 59 |
try { |
| 60 |
Pattern start = SchemaBuilderImpl.parse(createParseable(source, resolver, eh, properties), eh, dlf, spb, |
| 61 |
properties.contains(WrapProperty.ATTRIBUTE_OWNER)); |
| 62 |
return wrapPattern(start, spb, properties); |
| 63 |
} |
| 64 |
catch (IllegalSchemaException e) { |
| 65 |
throw new IncorrectSchemaException(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
public Option getOption(String uri) { |
| 70 |
return RngProperty.getOption(uri); |
| 71 |
} |
| 72 |
|
| 73 |
static private class SimplifiedSchemaPropertyMap implements PropertyMap { |
| 74 |
private final PropertyMap base; |
| 75 |
private final Pattern start; |
| 76 |
|
| 77 |
SimplifiedSchemaPropertyMap(PropertyMap base, Pattern start) { |
| 78 |
this.base = base; |
| 79 |
this.start = start; |
| 80 |
} |
| 81 |
|
| 82 |
public <T> T get(PropertyId<T> pid) { |
| 83 |
if (pid == RngProperty.SIMPLIFIED_SCHEMA) { |
| 84 |
String simplifiedSchema = PatternDumper.toString(start); |
| 85 |
return pid.getValueClass().cast(simplifiedSchema); |
| 86 |
} |
| 87 |
else |
| 88 |
return base.get(pid); |
| 89 |
} |
| 90 |
|
| 91 |
public PropertyId<?> getKey(int i) { |
| 92 |
return i == base.size() ? RngProperty.SIMPLIFIED_SCHEMA : base.getKey(i); |
| 93 |
} |
| 94 |
|
| 95 |
public int size() { |
| 96 |
return base.size() + 1; |
| 97 |
} |
| 98 |
|
| 99 |
public boolean contains(PropertyId<?> pid) { |
| 100 |
return base.contains(pid) || pid == RngProperty.SIMPLIFIED_SCHEMA; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
static Schema wrapPattern(Pattern start, SchemaPatternBuilder spb, PropertyMap properties) throws SAXException, IncorrectSchemaException { |
| 105 |
if (properties.contains(RngProperty.FEASIBLE)) |
| 106 |
start = FeasibleTransform.transform(spb, start); |
| 107 |
properties = new SimplifiedSchemaPropertyMap(AbstractSchema.filterProperties(properties, supportedPropertyIds), |
| 108 |
start); |
| 109 |
Schema schema = new PatternSchema(spb, start, properties); |
| 110 |
if (spb.hasIdTypes() && properties.contains(RngProperty.CHECK_ID_IDREF)) { |
| 111 |
ErrorHandler eh = properties.get(ValidateProperty.ERROR_HANDLER); |
| 112 |
IdTypeMap idTypeMap = new IdTypeMapBuilder(eh, start).getIdTypeMap(); |
| 113 |
if (idTypeMap == null) |
| 114 |
throw new IncorrectSchemaException(); |
| 115 |
Schema idSchema; |
| 116 |
if (properties.contains(RngProperty.FEASIBLE)) |
| 117 |
idSchema = new FeasibleIdTypeMapSchema(idTypeMap, properties); |
| 118 |
else |
| 119 |
idSchema = new IdTypeMapSchema(idTypeMap, properties); |
| 120 |
schema = new CombineSchema(schema, idSchema, properties); |
| 121 |
} |
| 122 |
return schema; |
| 123 |
} |
| 124 |
|
| 125 |
protected abstract Parseable<Pattern, NameClass, Locator, VoidValue, CommentListImpl, AnnotationsImpl> createParseable(SAXSource source, SAXResolver resolver, ErrorHandler eh, PropertyMap properties) |
| 126 |
throws SAXException; |
| 127 |
|
| 128 |
} |