1 19 20 package org.apache.cayenne.jpa.conf; 21 22 import java.io.IOException ; 23 import java.net.URL ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Properties ; 27 28 import javax.xml.XMLConstants ; 29 import javax.xml.parsers.ParserConfigurationException ; 30 import javax.xml.parsers.SAXParser ; 31 import javax.xml.parsers.SAXParserFactory ; 32 import javax.xml.transform.stream.StreamSource ; 33 import javax.xml.validation.Schema ; 34 import javax.xml.validation.SchemaFactory ; 35 36 import org.apache.cayenne.jpa.JpaUnit; 37 import org.apache.cayenne.jpa.Provider; 38 import org.apache.cayenne.jpa.instrument.InstrumentingUnit; 39 import org.xml.sax.Attributes ; 40 import org.xml.sax.InputSource ; 41 import org.xml.sax.SAXException ; 42 import org.xml.sax.SAXParseException ; 43 import org.xml.sax.helpers.DefaultHandler ; 44 45 51 public class UnitDescriptorParser { 52 53 static final String PERSISTENCE_SCHEMA = "META-INF/schemas/persistence_1_0.xsd"; 54 55 static final String PERSISTENCE = "persistence"; 56 static final String PERSISTENCE_UNIT = "persistence-unit"; 57 static final String DESCRIPTION = "description"; 58 static final String NAME = "name"; 59 static final String PROVIDER = "provider"; 60 static final String TRANSACTION_TYPE = "transaction-type"; 61 static final String JTA_DATASOURCE = "jta-data-source"; 62 static final String NON_JTA_DATASOURCE = "non-jta-data-source"; 63 static final String MAPPING_FILE = "mapping-file"; 64 static final String JAR_FILE = "jar-file"; 65 static final String CLASS = "class"; 66 static final String EXCLUDE_UNLISTED_CLASSES = "exclude-unlisted-classes"; 67 static final String PROPERTIES = "properties"; 68 static final String PROPERTY = "property"; 69 static final String VALUE = "value"; 70 71 protected SAXParserFactory parserFactory; 72 73 public UnitDescriptorParser() throws SAXException , ParserConfigurationException { 74 this(false); 75 } 76 77 public UnitDescriptorParser(boolean validatesAgainstSchema) throws SAXException { 78 79 parserFactory = SAXParserFactory.newInstance(); 80 parserFactory.setNamespaceAware(true); 81 82 88 if (validatesAgainstSchema) { 89 URL schemaURL = Thread.currentThread().getContextClassLoader().getResource( 90 PERSISTENCE_SCHEMA); 91 92 SchemaFactory factory = SchemaFactory 93 .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 94 StreamSource ss = new StreamSource (schemaURL.toExternalForm()); 95 Schema schema = factory.newSchema(ss); 96 parserFactory.setSchema(schema); 97 } 98 } 99 100 103 public Collection <JpaUnit> getPersistenceUnits( 104 InputSource in, 105 final URL persistenceUnitRootUrl) throws SAXException , IOException , 106 ParserConfigurationException { 107 108 final Collection <JpaUnit> unitInfos = new ArrayList <JpaUnit>(2); 109 110 SAXParser parser = parserFactory.newSAXParser(); 113 parser.parse(in, new DefaultHandler () { 114 115 JpaUnit unit; 116 Properties properties; 117 StringBuilder charBuffer; 118 119 @Override 120 public void error(SAXParseException e) throws SAXException { 121 throw e; 122 } 123 124 @Override 125 public void startElement( 126 String uri, 127 String localName, 128 String qName, 129 Attributes attributes) throws SAXException { 130 131 if (PERSISTENCE_UNIT.equals(qName)) { 132 String name = attributes.getValue("", NAME); 133 String transactionType = attributes.getValue("", TRANSACTION_TYPE); 134 135 unit = new InstrumentingUnit(); 136 unit.setPersistenceUnitName(name); 137 unit.setPersistenceUnitRootUrl(persistenceUnitRootUrl); 138 139 if (transactionType != null) { 140 unit.putProperty( 141 Provider.TRANSACTION_TYPE_PROPERTY, 142 transactionType); 143 } 144 } 145 else if (PROPERTIES.equals(qName)) { 146 properties = new Properties (); 147 } 148 else if (PROPERTY.equals(qName)) { 149 String name = attributes.getValue("", NAME); 150 String value = attributes.getValue("", VALUE); 151 properties.put(name, value); 152 } 153 else if (EXCLUDE_UNLISTED_CLASSES.equals(qName)) { 154 unit.setExcludeUnlistedClasses(true); 155 } 156 } 157 158 @Override 159 public void endElement(String uri, String localName, String qName) 160 throws SAXException { 161 if (PERSISTENCE_UNIT.equals(qName)) { 162 unitInfos.add(unit); 163 } 164 else if (PROPERTIES.equals(qName)) { 165 unit.addProperties(properties); 166 } 167 else { 168 String string = resetCharBuffer(); 170 171 if (string != null) { 172 if (CLASS.equals(qName)) { 173 unit.addManagedClassName(string); 174 } 175 else if (PROVIDER.equals(qName)) { 176 unit.putProperty(Provider.PROVIDER_PROPERTY, string); 177 } 178 else if (JAR_FILE.equals(qName)) { 179 unit.addJarFileUrl(string); 180 } 181 else if (MAPPING_FILE.equals(qName)) { 182 unit.addMappingFileName(string); 183 } 184 else if (JTA_DATASOURCE.equals(qName)) { 185 unit.putProperty(Provider.JTA_DATA_SOURCE_PROPERTY, string); 186 } 187 else if (NON_JTA_DATASOURCE.equals(qName)) { 188 unit.putProperty( 189 Provider.NON_JTA_DATA_SOURCE_PROPERTY, 190 string); 191 } 192 else if (DESCRIPTION.equals(qName)) { 193 unit.setDescription(string); 194 } 195 } 196 } 197 } 198 199 @Override 200 public void characters(char[] ch, int start, int length) throws SAXException { 201 if (charBuffer == null) { 202 charBuffer = new StringBuilder (); 203 } 204 205 charBuffer.append(ch, start, length); 206 } 207 208 String resetCharBuffer() { 209 if (charBuffer == null) { 210 return null; 211 } 212 213 String string = charBuffer.toString().trim(); 214 if (string.length() == 0) { 215 string = null; 216 } 217 charBuffer = null; 218 219 return string; 220 } 221 222 }); 223 return unitInfos; 224 } 225 226 } 227 | Popular Tags |