1 22 23 package org.xquark.bridge; 24 25 import java.io.CharArrayWriter ; 26 import java.io.IOException ; 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 import java.util.HashMap ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 33 import javax.sql.DataSource ; 34 import javax.xml.parsers.ParserConfigurationException ; 35 import javax.xml.parsers.SAXParserFactory ; 36 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 import org.xml.sax.*; 40 import org.xquark.mapper.dbms.AbstractConnection; 41 import org.xquark.mapper.dbms.AbstractConnectionFactory; 42 import org.xquark.mapper.mapping.*; 43 import org.xquark.mapper.storage.MapperTupleBuilder; 44 import org.xquark.mapper.storage.SAXHandler; 45 import org.xquark.mapper.storage.ValidatingFilerAdapter; 46 import org.xquark.mapper.util.RepositoryProperties; 47 import org.xquark.schema.SchemaManager; 48 import org.xquark.schema.validation.SchemaValidationContext; 49 import org.xquark.schema.validation.ValidatingSchemaFilter; 50 import org.xquark.serialize.XMLSerializer; 51 import org.xquark.util.SAXConstants; 52 import org.xquark.xml.xdbc.XMLDBCException; 53 import org.xquark.xpath.XTreeReader; 54 55 61 62 public class Mapping 63 { 64 private static final String RCSRevision = "$Revision: 1.12 $"; 65 private static final String RCSName = "$Name: $"; 66 67 private static Log log = LogFactory.getLog(Mapping.class); 68 private static SAXParserFactory parserFactory; 69 static 70 { 71 parserFactory = SAXParserFactory.newInstance(); 72 parserFactory.setNamespaceAware(true); 73 74 75 RepositoryProperties.load(); 76 } 77 78 private DataSource dataSource; 79 private String systemId = null; 80 private RepositoryMapping metadata = null; 81 private HashSet mappers = new HashSet (); 82 private SchemaManager schemaManager; 84 97 public Mapping(DataSource dataSource, InputSource source, boolean reload, 98 ClassLoader classLoader, SchemaManager schemaManager, HashMap mappingMetadata) 99 throws XMLDBCException { 100 this.dataSource = dataSource; 101 this.schemaManager = schemaManager; 102 Connection jdbcConn = null; 103 AbstractConnection connection = null; 104 try { 105 jdbcConn = dataSource.getConnection(); 106 connection = AbstractConnectionFactory.newConnection(jdbcConn, true); 107 systemId = source.getSystemId(); 108 if (mappingMetadata != null && systemId != null && !reload) { 109 metadata = (RepositoryMapping) mappingMetadata.get(systemId); 110 } 111 if (metadata == null) { 112 XMLReader reader = new ValidatingSchemaFilter(parserFactory.newSAXParser().getXMLReader(), 113 new SchemaValidationContext(schemaManager)); 114 reader.setErrorHandler(new ValidationErrorHandler()); 115 Loader loader = new Loader(reader, connection, schemaManager, classLoader, false); 116 MappingFactory mappingFactory = new MappingFactory(schemaManager, schemaManager); 117 mappingFactory.createTree(); 118 metadata = loader.load(source, mappingFactory, true); 119 if (log.isDebugEnabled()) { 120 XMLReader parser = new XTreeReader(metadata); 121 parser.setFeature(SAXConstants.TRACE_FEATURE, true); 122 CharArrayWriter caw = new CharArrayWriter (); 123 parser.setContentHandler(new XMLSerializer(caw)); 124 parser.parse(""); 125 log.debug(caw); 126 } 127 if (mappingMetadata != null && systemId != null) { 128 mappingMetadata.put(systemId, metadata); 129 } 130 } 131 } catch (ParserConfigurationException ex) { 132 throw new XMLDBCException("Could not load SAX parser", ex); 133 } catch (SQLException e) { 134 throw new XMLDBCException("Error while loading mapping", e); 135 } catch (IOException e) { 136 throw new XMLDBCException("Error while loading mapping", e); 137 } catch (SAXException e) { 138 throw new XMLDBCException("Error while loading mapping", e); 139 } finally { 140 try { 141 if (connection != null) connection.close(); else if (jdbcConn != null) jdbcConn.close(); 143 } catch (SQLException e) { 144 } 145 } 146 } 147 148 152 public synchronized void close() throws XMLDBCException 153 { 154 Iterator it = mappers.iterator(); 155 while (it.hasNext()) ((Mapper)it.next()).close(); 156 } 157 158 165 public Mapper getMapper() throws XMLDBCException 166 { 167 SchemaValidationContext validationContext = new SchemaValidationContext(schemaManager); 168 ValidatingFilerAdapter validator = new ValidatingFilerAdapter(validationContext, schemaManager); 169 Connection jdbcConn = null; 170 AbstractConnection connection = null; 171 try { 172 jdbcConn = dataSource.getConnection(); 173 connection = AbstractConnectionFactory.newConnection(jdbcConn, true); 174 } catch (SQLException ex) { 175 throw new XMLDBCException("Could not create connection", ex); 176 } 177 SAXHandler handler = new SAXHandler(new MappingIterator(metadata), 178 connection, 179 new MapperTupleBuilder(connection, 180 metadata, 181 validationContext) 182 ); 183 validator.setSAXHandler(handler); 184 Mapper result = new MapperImpl(this, connection, validator, handler, handler, handler); 185 synchronized (mappers) { 186 mappers.add(result); 187 } 188 return result; 189 } 190 191 195 public String getSystemId() { return systemId;} 196 197 201 public DataSource getDataSource() { return dataSource;} 202 203 208 RepositoryMapping getMetaData() { return metadata;} 209 210 213 synchronized void remove(MapperImpl mapper) { mappers.remove(mapper);} 214 215 private class ValidationErrorHandler implements ErrorHandler 216 { 217 public static final String RCSRevision = "$Revision: 1.12 $"; 218 public static final String RCSName = "$Name: $"; 219 220 public void warning(SAXParseException ex) throws SAXException { 224 throw ex; 225 } 226 227 public void error(SAXParseException ex) throws SAXException { 228 throw ex; 229 } 230 231 public void fatalError(SAXParseException ex) throws SAXException { 232 throw ex; 233 } 234 } 235 } 236 237 238 | Popular Tags |