1 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 ; 36 import org.xml.sax.SAXException ; 37 38 import java.io.IOException ; 39 import java.lang.ref.SoftReference ; 40 import java.util.HashMap ; 41 42 45 public class CompactVerifierFactoryImpl implements VerifierFactory { 46 private static HashMap <Path,SoftReference <Schema>> _schemaMap = 47 new HashMap <Path,SoftReference <Schema>>(); 48 49 52 public static Schema compileFromResource(String schemaName) 53 throws SAXException , IOException 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 66 public static Schema compileFromPath(Path path) 67 throws SAXException , IOException 68 { 69 return new CompactVerifierFactoryImpl().compileSchema(path); 70 } 71 72 75 public Schema compileSchema(Path path) 76 throws SAXException , IOException 77 { 78 String nativePath = path.getNativePath(); 79 80 SoftReference <Schema> schemaRef = _schemaMap.get(path); 81 Schema schema = null; 82 83 if (schemaRef != null && (schema = schemaRef.get()) != null) { 84 return schema; 86 } 87 88 ReadStream is = path.openRead(); 89 90 try { 91 InputSource source = new InputSource (is); 92 93 source.setSystemId(path.getUserPath()); 94 95 schema = compileSchema(source); 96 97 if (schema != null) 98 _schemaMap.put(path, new SoftReference <Schema>(schema)); 99 } finally { 100 is.close(); 101 } 102 103 return schema; 104 } 105 108 public Schema compileSchema(InputSource is) 109 throws SAXException , IOException 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 e) { 122 throw e; 123 } catch (Exception e) { 124 throw new SAXException (e); 125 } 126 } 127 } 128 | Popular Tags |