1 22 23 package org.xquark.extractor.runtime; 24 25 import java.io.IOException ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.Properties ; 29 30 import javax.xml.parsers.ParserConfigurationException ; 31 import javax.xml.parsers.SAXParserFactory ; 32 33 import org.xml.sax.*; 34 import org.xml.sax.helpers.DefaultHandler ; 35 import org.xquark.extractor.common.Constants; 36 import org.xquark.extractor.common.MessageLibrary; 37 import org.xquark.schema.SchemaManager; 38 import org.xquark.schema.validation.SchemaValidationContext; 39 import org.xquark.schema.validation.ValidatingSchemaFilter; 40 import org.xquark.xml.xdbc.XMLDBCException; 41 42 45 public class ExtractorConfiguration extends DefaultHandler implements Constants { 46 47 private static final String RCSRevision = "$Revision: 1.1 $"; 48 private static final String RCSName = "$Name: $"; 49 50 51 private static final byte IN_CATALOG = 1; 52 private static final byte IN_SCHEMA = 2; 53 private static final byte IN_TABLE = 1; 54 private static final byte IN_COLUMN = 1; 55 56 private static SAXParserFactory spf = SAXParserFactory.newInstance(); 57 static { 58 spf.setNamespaceAware(true); 59 } 60 61 public static final String DATASOURCE_XSD = "/org/xquark/extractor/resources/Datasource.xsd"; 62 private static SchemaManager loadingManager = null; 63 static { 64 try { 65 SchemaValidationContext context = new SchemaValidationContext(); 66 loadingManager = context.getSchemaManager(); 67 68 URL url = ExtractorConfiguration.class.getResource(DATASOURCE_XSD); 69 InputSource source = new InputSource(url.toString()); 70 loadingManager.loadSchema(context, source); 71 } catch (SAXException saxe) { 72 } 73 } 74 75 76 protected Properties xmlproperties; 77 78 private Selection current_site; 79 private Selection current_catalog; 80 private Selection current_schema; 81 private Selection current_table; 82 private Selection current_column; 83 private String _currentView = null; 84 private StringBuffer _currentChars = new StringBuffer (); 85 private String _dataSourceName = null; 86 private Properties _substitutions = null; 87 private Integer _nameCase = new Integer (CASE_MIXED); 88 private byte includeType = Selection.NONE; 89 90 91 public ExtractorConfiguration() { 92 xmlproperties = new Properties (); 93 } 94 95 public static Properties loadConfiguration(String configURI) throws MalformedURLException , XMLDBCException { 96 URL url = new URL (configURI); 97 try { 98 XMLReader reader = spf.newSAXParser().getXMLReader(); 99 ExtractorConfiguration handler = new ExtractorConfiguration(); 100 ValidatingSchemaFilter filter = new ValidatingSchemaFilter(reader, new SchemaValidationContext(loadingManager)); 101 filter.setContentHandler(handler); 102 filter.setErrorHandler(handler); 103 filter.parse(url.toString()); 104 return handler.getProperties(); 105 } catch (SAXParseException ex) { 106 String msg = "Error in parsing file " + ex.getSystemId() + " at line " + Integer.toString(ex.getLineNumber()) + 107 " : " + ex.getMessage(); 108 throw new XMLDBCException(msg, ex); 109 } catch (SAXException ex) { 110 throw new XMLDBCException("Could not parse configuration file", ex); 111 } catch (IOException ex) { 112 throw new XMLDBCException("Could not read configuration file", ex); 113 } catch (ParserConfigurationException ex) { 114 throw new XMLDBCException("Could not allocate XML parser", ex); 115 } 116 117 } 118 119 public static Properties getDefaultProperties(String url, String user) { 120 Properties properties = new Properties (); 121 properties.put(LABEL_SPEC_SUBST_NAMECASE, new Integer (CASE_MIXED)); 122 properties.put(LABEL_SPEC_URL, url); 123 properties.put(LABEL_SPEC_USER, user); 124 Properties substitutions = new Properties (); 125 substitutions.setProperty("$", "_"); 126 substitutions.setProperty("#", "_"); 127 properties.put(LABEL_SPEC_SUBST, substitutions); 128 return properties; 129 } 130 131 public Properties getProperties() { 132 return xmlproperties; 133 } 134 135 public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes atts) throws SAXException { 136 _currentChars.setLength(0); 137 if (localName.equalsIgnoreCase(LABEL_DATASOURCE)) { 138 String name = atts.getValue(LABEL_SPEC_NAME); 139 _dataSourceName = name; 140 setProperty(LABEL_SPEC_NAME, name); 141 current_site = new Selection(name, null, Selection.NONE, null); 142 xmlproperties.put("selections", current_site); 143 includeType = Selection.INCLUDES; 144 } else if (localName.equalsIgnoreCase(LABEL_SPEC_SUBST)) { 145 _substitutions = new Properties (); 146 xmlproperties.put(LABEL_SPEC_SUBST, _substitutions); 147 } else if (localName.equalsIgnoreCase(LABEL_SPEC_SUBST_CHAR)) { 148 _substitutions.setProperty(atts.getValue(LABEL_SPEC_SUBST_VALUE), atts.getValue(LABEL_SPEC_SUBST_SUBST)); 149 } else if (localName.equalsIgnoreCase(LABEL_SPEC_CATALOG)) { 150 String name = atts.getValue(LABEL_SPEC_NAME); 151 current_catalog = new Selection(name, null, includeType, null); 152 current_site.addToMap(name, current_catalog); 153 includeType = Selection.INCLUDES; 154 } else if (localName.equalsIgnoreCase(LABEL_SPEC_SCHEMA)) { 155 String name = atts.getValue(LABEL_SPEC_NAME); 156 String targetNamespace = atts.getValue(LABEL_SPEC_TARGETNAMESPACE); 157 current_schema = new Selection(name, targetNamespace, includeType, null); 158 String efd = atts.getValue(LABEL_SPEC_ELEMENTFORMDEFAULT); 159 int elementFormDefault; 160 if ("qualified".equalsIgnoreCase(efd)) { 161 elementFormDefault = Selection.QUALIFIED; 162 } else { 163 elementFormDefault = Selection.UNQUALIFIED; 164 } 165 current_schema.setElementFormDefault(elementFormDefault); 166 167 current_catalog.addToMap(name, current_schema); 168 } else if (localName.equalsIgnoreCase(LABEL_SPEC_TABLE)) { 169 String name = atts.getValue(LABEL_SPEC_NAME); 170 if (null != name) { 171 current_table = new Selection(name, null, includeType, null); 172 String alias = atts.getValue(LABEL_SPEC_ALIAS); 173 if (null != alias && !Selection.verifyName(alias)) { 174 throw new SAXException(MessageLibrary.getMessage("C_INV_NCNAME", alias)); 175 } 176 current_table.setAlias(alias); 177 current_schema.addToMap(name, current_table); 178 } else { 179 name = atts.getValue(LABEL_SPEC_REGEX); 180 if (null != name) { 181 current_table = new Selection(name, true, null, includeType, null); 182 current_schema.addToMap(name, current_table); 183 } else { 184 } 186 } 187 } else if (localName.equalsIgnoreCase(LABEL_SPEC_COLUMN)) { 188 String name = atts.getValue(LABEL_SPEC_NAME); 189 190 if (null != name) { 191 current_column = new Selection(name, null, includeType, null); 192 String alias = atts.getValue(LABEL_SPEC_ALIAS); 193 if (null != alias && !Selection.verifyName(alias)) { 194 throw new SAXException(MessageLibrary.getMessage("C_INV_NCNAME", alias)); 195 } 196 current_column.setAlias(alias); 197 current_table.addToMap(name, current_column); 198 } else { 199 name = atts.getValue(LABEL_SPEC_REGEX); 200 if (null != name) { 201 current_column = new Selection(name, true, null, includeType, null); 202 current_table.addToMap(name, current_table); 203 } else { 204 } 206 } 207 208 String isPK = atts.getValue(LABEL_SPEC_PK_COLUMN); 209 210 if (null != isPK && isPK.equalsIgnoreCase("true")) { 211 current_column.setPKColumn(true); 212 } 213 214 } else if (localName.equalsIgnoreCase(LABEL_SPEC_INCLUDES)) { 215 includeType = Selection.INCLUDES; 216 } else if (localName.equalsIgnoreCase(LABEL_SPEC_EXCLUDES)) { 217 includeType = Selection.EXCLUDES; 218 } 219 220 } 221 222 public void characters(char ch[], int start, int length) throws SAXException { 223 _currentChars.append(ch, start, length); 224 } 225 226 public void endElement(String namespaceURI, String localName, String qualifiedName) throws SAXException { 227 if (localName.equalsIgnoreCase(LABEL_SPEC_DESCRIPTION)) { 228 setProperty(LABEL_SPEC_DESCRIPTION, _currentChars.toString().trim()); 229 } else if (localName.equalsIgnoreCase(LABEL_SPEC_CASE)) { 230 setProperty(LABEL_SPEC_CASE, _currentChars.toString().trim()); 231 } else if (localName.equalsIgnoreCase(LABEL_SPEC_SUBST_NAMECASE)) { 232 String cur_car = _currentChars.toString().trim(); 233 if (cur_car.equalsIgnoreCase(CASE_LOWER_TOKEN)) { 234 _nameCase = new Integer (CASE_LOWER); 235 } else if (cur_car.equalsIgnoreCase(CASE_UPPER_TOKEN)) { 236 _nameCase = new Integer (CASE_UPPER); 237 } else if (cur_car.equalsIgnoreCase(CASE_MIXED_TOKEN)) { 238 _nameCase = new Integer (CASE_MIXED); 239 } 240 xmlproperties.put(LABEL_SPEC_SUBST_NAMECASE, _nameCase); 241 } else if (localName.equalsIgnoreCase(LABEL_SPEC_DRIVER)) { 242 setProperty(LABEL_SPEC_DRIVER, _currentChars.toString().trim()); 243 } else if (localName.equalsIgnoreCase(LABEL_SPEC_URL)) { 244 setProperty(LABEL_SPEC_URL, _currentChars.toString().trim()); 245 } else if (localName.equalsIgnoreCase(LABEL_SPEC_USER)) { 246 setProperty(LABEL_SPEC_USER, _currentChars.toString().trim()); 247 } else if (localName.equalsIgnoreCase(LABEL_SPEC_PASSWORD)) { 248 setProperty(LABEL_SPEC_PASSWORD, _currentChars.toString().trim()); 249 } else if (localName.equalsIgnoreCase(LABEL_SPEC_JNDI)) { 250 setProperty(LABEL_SPEC_JNDI, _currentChars.toString().trim()); 251 } 252 _currentChars.setLength(0); 253 } 254 255 260 protected void setProperty(String key, String value) { 261 if (key == null) { 262 System.err.println("Key " + key + " is null."); 263 return; 264 } 265 if (value == null) { 266 System.err.println("Value " + value + " of " + key + " is null."); 267 return; 268 } 269 xmlproperties.setProperty(key, value); 270 } 271 272 277 protected String getProperty(String key) { 278 return (String ) xmlproperties.getProperty(key); 279 } 280 281 public void error(SAXParseException spe) throws SAXException { 282 throw spe; 283 } 284 } 285 | Popular Tags |