KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > relaxng > impl > SchemaReaderImpl


1 package com.thaiopensource.relaxng.impl;
2
3 import com.thaiopensource.relaxng.parse.Parseable;
4 import com.thaiopensource.util.PropertyId;
5 import com.thaiopensource.util.PropertyMap;
6 import com.thaiopensource.validate.AbstractSchema;
7 import com.thaiopensource.validate.IncorrectSchemaException;
8 import com.thaiopensource.validate.Option;
9 import com.thaiopensource.validate.Schema;
10 import com.thaiopensource.validate.SchemaReader;
11 import com.thaiopensource.validate.ValidateProperty;
12 import com.thaiopensource.validate.nrl.NrlSchemaReceiverFactory;
13 import com.thaiopensource.validate.nrl.NrlProperty;
14 import com.thaiopensource.validate.rng.RngProperty;
15 import com.thaiopensource.xml.sax.XMLReaderCreator;
16 import org.relaxng.datatype.DatatypeLibraryFactory;
17 import org.relaxng.datatype.helpers.DatatypeLibraryLoader;
18 import org.xml.sax.ErrorHandler JavaDoc;
19 import org.xml.sax.InputSource JavaDoc;
20 import org.xml.sax.SAXException JavaDoc;
21
22 import java.io.IOException JavaDoc;
23
24 public abstract class SchemaReaderImpl implements SchemaReader {
25   private static final PropertyId[] supportedPropertyIds = {
26     ValidateProperty.XML_READER_CREATOR,
27     ValidateProperty.ERROR_HANDLER,
28     RngProperty.DATATYPE_LIBRARY_FACTORY,
29     RngProperty.CHECK_ID_IDREF,
30     RngProperty.FEASIBLE,
31     NrlProperty.ATTRIBUTES_SCHEMA,
32   };
33
34   public Schema createSchema(InputSource JavaDoc in, PropertyMap properties)
35           throws IOException JavaDoc, SAXException JavaDoc, IncorrectSchemaException {
36     SchemaPatternBuilder spb = new SchemaPatternBuilder();
37     XMLReaderCreator xrc = ValidateProperty.XML_READER_CREATOR.get(properties);
38     ErrorHandler JavaDoc eh = ValidateProperty.ERROR_HANDLER.get(properties);
39     DatatypeLibraryFactory dlf = RngProperty.DATATYPE_LIBRARY_FACTORY.get(properties);
40     if (dlf == null)
41       dlf = new DatatypeLibraryLoader();
42     Pattern start = SchemaBuilderImpl.parse(createParseable(xrc, in, eh), eh, dlf, spb,
43                                             properties.contains(NrlProperty.ATTRIBUTES_SCHEMA));
44     return wrapPattern(start, spb, properties);
45   }
46
47   public Option getOption(String JavaDoc uri) {
48     return RngProperty.getOption(uri);
49   }
50
51   static Schema wrapPattern(Pattern start, SchemaPatternBuilder spb, PropertyMap properties) throws SAXException JavaDoc, IncorrectSchemaException {
52     properties = AbstractSchema.filterProperties(properties, supportedPropertyIds);
53     if (properties.contains(RngProperty.FEASIBLE))
54       start = FeasibleTransform.transform(spb, start);
55     Schema schema = new PatternSchema(spb, start, properties);
56     if (spb.hasIdTypes() && properties.contains(RngProperty.CHECK_ID_IDREF)) {
57       ErrorHandler JavaDoc eh = ValidateProperty.ERROR_HANDLER.get(properties);
58       IdTypeMap idTypeMap = new IdTypeMapBuilder(eh, start).getIdTypeMap();
59       if (idTypeMap == null)
60         throw new IncorrectSchemaException();
61       Schema idSchema;
62       if (properties.contains(RngProperty.FEASIBLE))
63         idSchema = new FeasibleIdTypeMapSchema(idTypeMap, properties);
64       else
65         idSchema = new IdTypeMapSchema(idTypeMap, properties);
66       schema = new CombineSchema(schema, idSchema, properties);
67     }
68     return schema;
69   }
70
71   protected abstract Parseable createParseable(XMLReaderCreator xrc, InputSource JavaDoc in, ErrorHandler JavaDoc eh);
72 }
73
Popular Tags