KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > validate > mns > SchemaReceiverImpl


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 JavaDoc;
19 import org.xml.sax.InputSource JavaDoc;
20 import org.xml.sax.SAXException JavaDoc;
21 import org.xml.sax.XMLReader JavaDoc;
22
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25
26 class SchemaReceiverImpl implements SchemaReceiver {
27   private static final String JavaDoc MNS_SCHEMA = "mns.rng";
28   private static final String JavaDoc 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 JavaDoc xr) {
52     return new SchemaImpl(attributesSchema).installHandlers(xr, this);
53   }
54
55   Schema getMnsSchema() throws IOException JavaDoc, IncorrectSchemaException, SAXException JavaDoc {
56    if (mnsSchema == null) {
57       String JavaDoc className = SchemaReceiverImpl.class.getName();
58       String JavaDoc resourceName = className.substring(0, className.lastIndexOf('.')).replace('.', '/') + "/resources/" + MNS_SCHEMA;
59       URL JavaDoc mnsSchemaUrl = getResource(resourceName);
60       mnsSchema = SAXSchemaReader.getInstance().createSchema(
61               new InputSource JavaDoc(mnsSchemaUrl.toString()),
62               properties);
63     }
64     return mnsSchema;
65   }
66
67   private static URL JavaDoc getResource(String JavaDoc resourceName) {
68     ClassLoader JavaDoc cl = SchemaReceiverImpl.class.getClassLoader();
69     // XXX see if we should borrow 1.2 code from Service
70
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 JavaDoc inputSource, String JavaDoc schemaType, boolean isAttributesSchema) throws IOException JavaDoc, IncorrectSchemaException, SAXException JavaDoc {
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 JavaDoc schemaType) {
87     if (schemaType == null)
88       return false;
89     schemaType = schemaType.trim();
90     return schemaType.equals(RNC_MEDIA_TYPE);
91   }
92 }
93
Popular Tags