1 21 package oracle.toplink.essentials.internal.ejb.cmp3.xml.parser; 23 24 import org.xml.sax.Locator ; 25 import org.xml.sax.SAXException ; 26 import org.xml.sax.Attributes ; 27 import org.xml.sax.ContentHandler ; 28 29 import java.net.URL ; 30 import java.net.MalformedURLException ; 31 import java.util.Vector ; 32 import javax.persistence.spi.PersistenceUnitTransactionType; 33 34 import oracle.toplink.essentials.ejb.cmp3.persistence.SEPersistenceUnitInfo; 35 import oracle.toplink.essentials.internal.ejb.cmp3.jdbc.base.DataSourceImpl; 36 import oracle.toplink.essentials.logging.AbstractSessionLog; 37 38 public class PersistenceContentHandler implements ContentHandler { 39 private static final String NAMESPACE_URI = "http://java.sun.com/xml/ns/persistence"; 40 private static final String ELEMENT_PERSISTENCE_UNIT = "persistence-unit"; 41 private static final String ELEMENT_PROVIDER = "provider"; 42 private static final String ELEMENT_JTA_DATA_SOURCE = "jta-data-source"; 43 private static final String ELEMENT_NON_JTA_DATA_SOURCE = "non-jta-data-source"; 44 private static final String ELEMENT_MAPPING_FILE = "mapping-file"; 45 private static final String ELEMENT_JAR_FILE = "jar-file"; 46 private static final String ELEMENT_CLASS = "class"; 47 private static final String ELEMENT_EXCLUDE_UNLISTED_CLASSES = "exclude-unlisted-classes"; 48 private static final String ELEMENT_PROPERTY = "property"; 49 private static final String ATTRIBUTE_NAME = "name"; 50 private static final String ATTRIBUTE_VALUE = "value"; 51 private static final String ATTRIBUTE_TRANSACTION_TYPE = "transaction-type"; 52 53 private SEPersistenceUnitInfo persistenceUnitInfo; 54 private Vector <SEPersistenceUnitInfo> persistenceUnits; 55 private StringBuffer stringBuffer; 56 private boolean readCharacters = false; 57 58 public PersistenceContentHandler() { 59 super(); 60 stringBuffer = new StringBuffer (); 61 persistenceUnits = new Vector (); 62 } 63 64 public Vector <SEPersistenceUnitInfo> getPersistenceUnits() { 65 return persistenceUnits; 66 } 67 68 public void setDocumentLocator(Locator locator) { 69 } 70 71 public void startDocument() throws SAXException { 72 } 73 74 public void endDocument() throws SAXException { 75 } 76 77 public void startPrefixMapping(String prefix, String uri) throws SAXException { 78 } 79 80 public void endPrefixMapping(String prefix) throws SAXException { 81 } 82 83 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 84 if (NAMESPACE_URI.equals(namespaceURI)) { 85 if (ELEMENT_PERSISTENCE_UNIT.equals(localName)) { 86 persistenceUnitInfo = new SEPersistenceUnitInfo(); 87 persistenceUnitInfo.setPersistenceUnitName(atts.getValue(ATTRIBUTE_NAME)); 88 String transactionType = atts.getValue(ATTRIBUTE_TRANSACTION_TYPE); 89 if(transactionType != null) { 90 persistenceUnitInfo.setTransactionType(PersistenceUnitTransactionType.valueOf(transactionType)); 91 } 92 return; 93 } else if (ELEMENT_PROPERTY.equals(localName)) { 94 String name = atts.getValue(ATTRIBUTE_NAME); 95 String value = atts.getValue(ATTRIBUTE_VALUE); 96 persistenceUnitInfo.getProperties().setProperty(name, value); 97 } else if (ELEMENT_PROVIDER.equals(localName)) { 98 readCharacters = true; 99 return; 100 } else if (ELEMENT_JTA_DATA_SOURCE.equals(localName)) { 101 readCharacters = true; 102 return; 103 } else if (ELEMENT_NON_JTA_DATA_SOURCE.equals(localName)) { 104 readCharacters = true; 105 return; 106 } else if (ELEMENT_MAPPING_FILE.equals(localName)) { 107 readCharacters = true; 108 return; 109 } else if (ELEMENT_JAR_FILE.equals(localName)) { 110 readCharacters = true; 111 return; 112 } else if (ELEMENT_EXCLUDE_UNLISTED_CLASSES.equals(localName)) { 113 readCharacters = true; 114 return; 115 } else if (ELEMENT_CLASS.equals(localName)) { 116 readCharacters = true; 117 return; 118 } 119 } 120 } 121 122 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 123 String string = stringBuffer.toString().trim(); 124 stringBuffer.delete(0, stringBuffer.length()); 125 readCharacters = false; 126 127 if (NAMESPACE_URI.equals(namespaceURI)) { 128 if (ELEMENT_PROVIDER.equals(localName)) { 129 persistenceUnitInfo.setPersistenceProviderClassName(string); 130 return; 131 } else if (ELEMENT_JTA_DATA_SOURCE.equals(localName)) { 132 persistenceUnitInfo.setJtaDataSource( 133 new DataSourceImpl(string, null, null, null)); 136 return; 137 } else if (ELEMENT_NON_JTA_DATA_SOURCE.equals(localName)) { 138 persistenceUnitInfo.setNonJtaDataSource( 139 new DataSourceImpl(string, null, null, null)); 142 return; 143 } else if (ELEMENT_MAPPING_FILE.equals(localName)) { 144 persistenceUnitInfo.getMappingFileNames().add(string); 145 return; 146 } else if (ELEMENT_JAR_FILE.equals(localName)) { 147 URL url = null; 148 try{ 149 url = new URL (string); 150 } catch (MalformedURLException exc){ 151 try{ 152 url = new URL ("file:///" + string); 153 } catch (MalformedURLException exception){ 154 AbstractSessionLog.getLog().log(AbstractSessionLog.INFO, "jar_file_url_exception", exception); 155 } 156 } 157 persistenceUnitInfo.getJarFileUrls().add(url); 158 return; 159 } else if (ELEMENT_CLASS.equals(localName)) { 160 persistenceUnitInfo.getManagedClassNames().add(string); 161 return; 162 } else if (ELEMENT_EXCLUDE_UNLISTED_CLASSES.equals(localName)) { 163 if (string.equals("true") || string.equals("1")){ 164 persistenceUnitInfo.setExcludeUnlistedClasses(true); 165 } else { 166 persistenceUnitInfo.setExcludeUnlistedClasses(false); 167 } 168 return; 169 } else if (ELEMENT_PERSISTENCE_UNIT.equals(localName)) { 170 if (persistenceUnitInfo != null){ 171 persistenceUnits.add(persistenceUnitInfo); 172 persistenceUnitInfo = null; 173 } 174 } 175 } 176 } 177 178 public void characters(char[] ch, int start, int length) throws SAXException { 179 if (readCharacters) { 180 stringBuffer.append(ch, start, length); 181 } 182 } 183 184 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { 185 } 186 187 public void processingInstruction(String target, String data) throws SAXException { 188 } 189 190 public void skippedEntity(String name) throws SAXException { 191 } 192 } 193 | Popular Tags |