001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.relaxng;
030:
031: import com.caucho.vfs.MergePath;
032: import com.caucho.vfs.Path;
033: import com.caucho.vfs.ReadStream;
034:
035: import org.xml.sax.InputSource;
036: import org.xml.sax.SAXException;
037:
038: import java.io.IOException;
039: import java.lang.ref.SoftReference;
040: import java.util.HashMap;
041:
042: /**
043: * JARV Verifier factory.
044: */
045: public class CompactVerifierFactoryImpl implements VerifierFactory {
046: private static HashMap<Path, SoftReference<Schema>> _schemaMap = new HashMap<Path, SoftReference<Schema>>();
047:
048: /**
049: * Reads the schema from the classpath.
050: */
051: public static Schema compileFromResource(String schemaName)
052: throws SAXException, IOException {
053: CompactVerifierFactoryImpl factory = new CompactVerifierFactoryImpl();
054:
055: MergePath mp = new MergePath();
056: mp.addClassPath();
057:
058: return factory.compileSchema(mp.lookup(schemaName));
059: }
060:
061: /**
062: * Compile a schema.
063: */
064: public static Schema compileFromPath(Path path)
065: throws SAXException, IOException {
066: return new CompactVerifierFactoryImpl().compileSchema(path);
067: }
068:
069: /**
070: * Compile a schema.
071: */
072: public Schema compileSchema(Path path) throws SAXException,
073: IOException {
074: String nativePath = path.getNativePath();
075:
076: SoftReference<Schema> schemaRef = _schemaMap.get(path);
077: Schema schema = null;
078:
079: if (schemaRef != null && (schema = schemaRef.get()) != null) {
080: // XXX: probably eventually add an isModified
081: return schema;
082: }
083:
084: ReadStream is = path.openRead();
085:
086: try {
087: InputSource source = new InputSource(is);
088:
089: source.setSystemId(path.getUserPath());
090:
091: schema = compileSchema(source);
092:
093: if (schema != null)
094: _schemaMap.put(path, new SoftReference<Schema>(schema));
095: } finally {
096: is.close();
097: }
098:
099: return schema;
100: }
101:
102: /**
103: * Compile a schema.
104: */
105: public Schema compileSchema(InputSource is) throws SAXException,
106: IOException {
107: try {
108: CompactParser parser = new CompactParser();
109:
110: parser.parse(is);
111:
112: SchemaImpl schema = new SchemaImpl(parser.getGrammar());
113:
114: schema.setFilename(is.getSystemId());
115:
116: return schema;
117: } catch (SAXException e) {
118: throw e;
119: } catch (Exception e) {
120: throw new SAXException(e);
121: }
122: }
123: }
|