1 package com.thaiopensource.validate.mns; 2 3 import com.thaiopensource.util.PropertyMap; 4 import com.thaiopensource.util.PropertyMapBuilder; 5 import com.thaiopensource.validate.rng.CompactSchemaReader; 6 import com.thaiopensource.validate.IncorrectSchemaException; 7 import com.thaiopensource.validate.rng.RngProperty; 8 import com.thaiopensource.validate.rng.SAXSchemaReader; 9 import com.thaiopensource.validate.nrl.NrlSchemaReceiverFactory; 10 import com.thaiopensource.validate.nrl.NrlProperty; 11 import com.thaiopensource.validate.Schema; 12 import com.thaiopensource.validate.SchemaReader; 13 import com.thaiopensource.validate.ValidateProperty; 14 import com.thaiopensource.validate.auto.AutoSchemaReader; 15 import com.thaiopensource.validate.auto.SchemaFuture; 16 import com.thaiopensource.validate.auto.SchemaReceiver; 17 import com.thaiopensource.validate.auto.SchemaReceiverFactory; 18 import org.xml.sax.ErrorHandler ; 19 import org.xml.sax.InputSource ; 20 import org.xml.sax.SAXException ; 21 import org.xml.sax.XMLReader ; 22 23 import java.io.IOException ; 24 import java.net.URL ; 25 26 class SchemaReceiverImpl implements SchemaReceiver { 27 private static final String MNS_SCHEMA = "mns.rng"; 28 private static final String RNC_MEDIA_TYPE = "application/x-rnc"; 29 private final PropertyMap properties; 30 private final PropertyMap attributeSchemaProperties; 31 private final boolean attributesSchema; 32 private final SchemaReader autoSchemaLanguage; 33 private Schema mnsSchema = null; 34 35 public SchemaReceiverImpl(PropertyMap properties) { 36 this.attributesSchema = properties.contains(NrlProperty.ATTRIBUTES_SCHEMA); 37 PropertyMapBuilder builder = new PropertyMapBuilder(properties); 38 if (attributesSchema) { 39 attributeSchemaProperties = properties; 40 builder.put(NrlProperty.ATTRIBUTES_SCHEMA, null); 41 this.properties = builder.toPropertyMap(); 42 } 43 else { 44 this.properties = properties; 45 NrlProperty.ATTRIBUTES_SCHEMA.add(builder); 46 attributeSchemaProperties = builder.toPropertyMap(); 47 } 48 this.autoSchemaLanguage = new AutoSchemaReader(SchemaReceiverFactory.PROPERTY.get(properties)); 49 } 50 51 public SchemaFuture installHandlers(XMLReader xr) { 52 return new SchemaImpl(attributesSchema).installHandlers(xr, this); 53 } 54 55 Schema getMnsSchema() throws IOException , IncorrectSchemaException, SAXException { 56 if (mnsSchema == null) { 57 String className = SchemaReceiverImpl.class.getName(); 58 String resourceName = className.substring(0, className.lastIndexOf('.')).replace('.', '/') + "/resources/" + MNS_SCHEMA; 59 URL mnsSchemaUrl = getResource(resourceName); 60 mnsSchema = SAXSchemaReader.getInstance().createSchema( 61 new InputSource (mnsSchemaUrl.toString()), 62 properties); 63 } 64 return mnsSchema; 65 } 66 67 private static URL getResource(String resourceName) { 68 ClassLoader cl = SchemaReceiverImpl.class.getClassLoader(); 69 if (cl == null) 71 return ClassLoader.getSystemResource(resourceName); 72 else 73 return cl.getResource(resourceName); 74 } 75 76 PropertyMap getProperties() { 77 return properties; 78 } 79 80 Schema createChildSchema(InputSource inputSource, String schemaType, boolean isAttributesSchema) throws IOException , IncorrectSchemaException, SAXException { 81 SchemaReader lang = isRnc(schemaType) ? CompactSchemaReader.getInstance() : autoSchemaLanguage; 82 return lang.createSchema(inputSource, 83 isAttributesSchema ? attributeSchemaProperties : properties); 84 } 85 86 private static boolean isRnc(String schemaType) { 87 if (schemaType == null) 88 return false; 89 schemaType = schemaType.trim(); 90 return schemaType.equals(RNC_MEDIA_TYPE); 91 } 92 } 93 | Popular Tags |