KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > relaxng > CompactVerifierFactoryImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.relaxng;
30
31 import com.caucho.vfs.MergePath;
32 import com.caucho.vfs.Path;
33 import com.caucho.vfs.ReadStream;
34
35 import org.xml.sax.InputSource JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38 import java.io.IOException JavaDoc;
39 import java.lang.ref.SoftReference JavaDoc;
40 import java.util.HashMap JavaDoc;
41
42 /**
43  * JARV Verifier factory.
44  */

45 public class CompactVerifierFactoryImpl implements VerifierFactory {
46   private static HashMap JavaDoc<Path,SoftReference JavaDoc<Schema>> _schemaMap =
47     new HashMap JavaDoc<Path,SoftReference JavaDoc<Schema>>();
48              
49   /**
50    * Reads the schema from the classpath.
51    */

52   public static Schema compileFromResource(String JavaDoc schemaName)
53     throws SAXException JavaDoc, IOException JavaDoc
54   {
55     CompactVerifierFactoryImpl factory = new CompactVerifierFactoryImpl();
56
57     MergePath mp = new MergePath();
58     mp.addClassPath();
59     
60     return factory.compileSchema(mp.lookup(schemaName));
61   }
62   
63   /**
64    * Compile a schema.
65    */

66   public static Schema compileFromPath(Path path)
67     throws SAXException JavaDoc, IOException JavaDoc
68   {
69     return new CompactVerifierFactoryImpl().compileSchema(path);
70   }
71   
72   /**
73    * Compile a schema.
74    */

75   public Schema compileSchema(Path path)
76     throws SAXException JavaDoc, IOException JavaDoc
77   {
78     String JavaDoc nativePath = path.getNativePath();
79     
80     SoftReference JavaDoc<Schema> schemaRef = _schemaMap.get(path);
81     Schema schema = null;
82
83     if (schemaRef != null && (schema = schemaRef.get()) != null) {
84       // XXX: probably eventually add an isModified
85
return schema;
86     }
87     
88     ReadStream is = path.openRead();
89
90     try {
91       InputSource JavaDoc source = new InputSource JavaDoc(is);
92
93       source.setSystemId(path.getUserPath());
94
95       schema = compileSchema(source);
96
97       if (schema != null)
98     _schemaMap.put(path, new SoftReference JavaDoc<Schema>(schema));
99     } finally {
100       is.close();
101     }
102
103     return schema;
104   }
105   /**
106    * Compile a schema.
107    */

108   public Schema compileSchema(InputSource JavaDoc is)
109     throws SAXException JavaDoc, IOException JavaDoc
110   {
111     try {
112       CompactParser parser = new CompactParser();
113
114       parser.parse(is);
115       
116       SchemaImpl schema = new SchemaImpl(parser.getGrammar());
117
118       schema.setFilename(is.getSystemId());
119
120       return schema;
121     } catch (SAXException JavaDoc e) {
122       throw e;
123     } catch (Exception JavaDoc e) {
124       throw new SAXException JavaDoc(e);
125     }
126   }
127 }
128
Popular Tags