1 package com.thaiopensource.validate.nrl; 2 3 import com.thaiopensource.util.PropertyId; 4 import com.thaiopensource.util.PropertyMap; 5 import com.thaiopensource.util.PropertyMapBuilder; 6 import com.thaiopensource.validate.IncorrectSchemaException; 7 import com.thaiopensource.validate.Option; 8 import com.thaiopensource.validate.Schema; 9 import com.thaiopensource.validate.SchemaReader; 10 import com.thaiopensource.validate.ValidateProperty; 11 import com.thaiopensource.validate.auto.AutoSchemaReader; 12 import com.thaiopensource.validate.auto.SchemaFuture; 13 import com.thaiopensource.validate.auto.SchemaReceiver; 14 import com.thaiopensource.validate.auto.SchemaReceiverFactory; 15 import com.thaiopensource.validate.rng.CompactSchemaReader; 16 import com.thaiopensource.validate.rng.SAXSchemaReader; 17 import org.xml.sax.InputSource ; 18 import org.xml.sax.SAXException ; 19 import org.xml.sax.XMLReader ; 20 21 import java.io.IOException ; 22 import java.net.URL ; 23 24 class SchemaReceiverImpl implements SchemaReceiver { 25 private static final String NRL_SCHEMA = "nrl.rng"; 26 private static final String RNC_MEDIA_TYPE = "application/x-rnc"; 27 private final PropertyMap properties; 28 private final boolean attributesSchema; 29 private final SchemaReader autoSchemaReader; 30 private Schema nrlSchema = null; 31 private static final PropertyId subSchemaProperties[] = { 32 ValidateProperty.ERROR_HANDLER, 33 ValidateProperty.XML_READER_CREATOR, 34 ValidateProperty.ENTITY_RESOLVER, 35 SchemaReceiverFactory.PROPERTY, 36 }; 37 38 public SchemaReceiverImpl(PropertyMap properties) { 39 this.attributesSchema = properties.contains(NrlProperty.ATTRIBUTES_SCHEMA); 40 PropertyMapBuilder builder = new PropertyMapBuilder(); 41 for (int i = 0; i < subSchemaProperties.length; i++) { 42 Object value = properties.get(subSchemaProperties[i]); 43 if (value != null) 44 builder.put(subSchemaProperties[i], value); 45 } 46 this.properties = builder.toPropertyMap(); 47 this.autoSchemaReader = new AutoSchemaReader(SchemaReceiverFactory.PROPERTY.get(properties)); 48 } 49 50 public SchemaFuture installHandlers(XMLReader xr) { 51 PropertyMapBuilder builder = new PropertyMapBuilder(properties); 52 if (attributesSchema) 53 NrlProperty.ATTRIBUTES_SCHEMA.add(builder); 54 return new SchemaImpl(builder.toPropertyMap()).installHandlers(xr, this); 55 } 56 57 Schema getNrlSchema() throws IOException , IncorrectSchemaException, SAXException { 58 if (nrlSchema == null) { 59 String className = SchemaReceiverImpl.class.getName(); 60 String resourceName = className.substring(0, className.lastIndexOf('.')).replace('.', '/') + "/resources/" + NRL_SCHEMA; 61 URL nrlSchemaUrl = getResource(resourceName); 62 nrlSchema = SAXSchemaReader.getInstance().createSchema(new InputSource (nrlSchemaUrl.toString()), 63 properties); 64 } 65 return nrlSchema; 66 } 67 68 private static URL getResource(String resourceName) { 69 ClassLoader cl = SchemaReceiverImpl.class.getClassLoader(); 70 if (cl == null) 72 return ClassLoader.getSystemResource(resourceName); 73 else 74 return cl.getResource(resourceName); 75 } 76 77 PropertyMap getProperties() { 78 return properties; 79 } 80 81 Schema createChildSchema(InputSource inputSource, String schemaType, PropertyMap options, boolean isAttributesSchema) throws IOException , IncorrectSchemaException, SAXException { 82 SchemaReader reader = isRnc(schemaType) ? CompactSchemaReader.getInstance() : autoSchemaReader; 83 PropertyMapBuilder builder = new PropertyMapBuilder(properties); 84 if (isAttributesSchema) 85 NrlProperty.ATTRIBUTES_SCHEMA.add(builder); 86 for (int i = 0, len = options.size(); i < len; i++) 87 builder.put(options.getKey(i), options.get(options.getKey(i))); 88 return reader.createSchema(inputSource, builder.toPropertyMap()); 89 } 90 91 Option getOption(String uri) { 92 Option option = autoSchemaReader.getOption(uri); 93 if (option != null) 94 return option; 95 return CompactSchemaReader.getInstance().getOption(uri); 96 } 97 98 private static boolean isRnc(String schemaType) { 99 if (schemaType == null) 100 return false; 101 schemaType = schemaType.trim(); 102 return schemaType.equals(RNC_MEDIA_TYPE); 103 } 104 } 105 | Popular Tags |