1 22 23 package org.xquark.xquery.metadata; 24 25 import java.io.IOException ; 26 import java.util.ArrayList ; 27 28 import javax.xml.parsers.ParserConfigurationException ; 29 import javax.xml.parsers.SAXParser ; 30 import javax.xml.parsers.SAXParserFactory ; 31 32 import org.xml.sax.*; 33 import org.xml.sax.helpers.AttributesImpl ; 34 import org.xml.sax.helpers.DefaultHandler ; 35 import org.xquark.schema.Schema; 36 import org.xquark.schema.loader.Loader; 37 import org.xquark.schema.validation.SchemaValidationContext; 38 import org.xquark.schema.validation.ValidatingSchemaFilter; 39 40 41 49 public class MetaParser 50 extends DefaultHandler { 51 52 private static final String RCSRevision = "$Revision: 1.3 $"; 56 private static final String RCSName = "$Name: $"; 57 private final static String TAG_SOURCE = "metadata"; 59 private final static String TAG_SCHEMAS = "schemas"; 60 private final static String TAG_SCHEMA = "schema"; 61 private final static String TAG_SCHEMADEFINITION = "schemadefinition"; 62 private final static String TAG_COLLECTIONS = "collections"; 63 private final static String TAG_COLLECTION = "collection"; 64 private final static String TAG_STEP = "step"; 65 private final static String ATT_SOURCE_NAME = "source"; 66 private final static String ATT_COLLECTION_NAME = "name"; 67 private final static String ATT_STEP_NS = "ns"; 68 private final static String ATT_STEP_NAME = "name"; 69 private final static String ATT_STEP_TYPE = "type"; 70 private final static String ATT_STEP_TYPE_ATT = "attribute"; 71 private final static String ATT_STEP_TYPE_ELEMENT = "element"; 72 73 private MetaDataImpl metadata = null; 79 80 private MetaCollection metacollection = null; 82 83 private MetaWrapper metawrapper = null; 85 86 private String sourcename = null; 88 89 private boolean inSchema = false; 92 93 private ArrayList tmppref = null; 100 private ArrayList tmpuri = null; 101 102 private StringBuffer bufSchema = new StringBuffer (); 106 107 private static SAXParserFactory spf = null; 109 static { 110 spf = SAXParserFactory.newInstance() ; 112 spf.setNamespaceAware(true); 113 spf.setValidating(false); 114 } 115 116 private XMLReader reader = null; 117 private Loader loader = null; 118 private static SchemaValidationContext svContext = null; 119 120 121 129 public MetaParser(MetaDataImpl metadata) { 130 this.metadata = metadata; 131 svContext = new SchemaValidationContext(metadata.getLoadingManager()) ; 132 tmppref = new ArrayList () ; 133 tmpuri = new ArrayList () ; 134 } 135 136 137 public void error(SAXParseException spe) { 141 System.err.println("Caught SAXParseException : " + spe.getMessage()); 142 } 143 144 private void createReader() throws SAXException, ParserConfigurationException { 145 SAXParser saxParser = spf.newSAXParser(); 147 148 reader = saxParser.getXMLReader(); 150 } 151 152 153 159 public MetaWrapper createWrapper(String uri, boolean validate) throws MetadataException { 160 try { 161 metawrapper = new MetaWrapper(metadata); 162 createReader() ; 163 164 ValidatingSchemaFilter filter = new ValidatingSchemaFilter(reader, svContext); 165 filter.setContentHandler(this); 166 filter.setErrorHandler(this); 167 168 filter.parse(uri); 169 170 return metawrapper; 171 } 172 catch (org.xml.sax.SAXParseException e) {} 173 catch (SAXException e) {} 174 catch (IOException e) {} 175 catch (ParserConfigurationException e) {} 176 throw new MetadataException(); 177 } 178 179 180 183 public MetaWrapper createWrapper(InputSource inputSource, boolean validate) throws MetadataException { 184 try { 185 metawrapper = new MetaWrapper(metadata); 186 createReader() ; 187 188 ValidatingSchemaFilter filter = new ValidatingSchemaFilter(reader, svContext); 189 filter.setContentHandler(this); 190 filter.setErrorHandler(this); 191 filter.parse(inputSource); 192 return metawrapper; 193 } 194 catch (org.xml.sax.SAXParseException e) {} 195 catch (SAXException e) {} 196 catch (IOException e) {} 197 catch (ParserConfigurationException e) {} 198 throw new MetadataException(); 199 } 200 201 202 public MetaWrapper createWrapper(boolean validate) throws MetadataException { 203 try { 204 metawrapper = new MetaWrapper(metadata); 205 createReader() ; 206 207 ValidatingSchemaFilter filter = new ValidatingSchemaFilter(reader, svContext); 208 filter.setContentHandler(this); 209 filter.setErrorHandler(this); 210 211 return metawrapper; 212 } 213 catch (org.xml.sax.SAXParseException e) {} 214 catch (SAXException e) {} 215 catch (ParserConfigurationException e) {} 216 throw new MetadataException(); 217 } 218 219 230 public void startDocument() 231 throws SAXException { 232 } 233 234 235 public void startPrefixMapping(String prefix, String uri) throws SAXException { 236 if (loader != null && inSchema) { 237 loader.startPrefixMapping(prefix, uri); 238 } 239 else { 240 tmppref.add(prefix) ; 241 tmpuri.add(uri) ; 242 } 243 } 244 245 246 public void endPrefixMapping(String prefix) 247 throws SAXException { 248 if (loader != null && inSchema) { 249 loader.endPrefixMapping(prefix); 250 } 251 } 252 253 254 257 public void startElement(String uri, String local, String raw, Attributes attrs) 258 throws SAXException { 259 260 if (local.equalsIgnoreCase(TAG_SCHEMA)) { 261 bufSchema.setLength(0); 262 inSchema = true; 263 loader = new Loader(metadata.getSchemaManager()); 264 loader.startDocument(); 265 266 267 bufSchema.append("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"); 269 bufSchema.append("<"); 270 if (uri != null) { 271 int indexPref = tmpuri.indexOf(uri); 272 if (indexPref >= 0) { 273 String pref = (String )tmppref.get(indexPref); 274 if (pref != null && pref.length() > 0) { 275 bufSchema.append(pref); 276 bufSchema.append(":"); 277 } 278 } 279 } 280 bufSchema.append(local); 281 for (int i = 0 ; i < tmppref.size() ; i ++) { 282 283 bufSchema.append(" xmlns"); 284 if (tmppref.get(i) != null && ((String )tmppref.get(i)).length() != 0) bufSchema.append(":"); 285 bufSchema.append(tmppref.get(i)); 286 bufSchema.append("=\""); 287 bufSchema.append(tmpuri.get(i)); 288 bufSchema.append("\""); 289 290 loader.startPrefixMapping((String ) tmppref.get(i), (String ) tmpuri.get(i)); 291 } 292 for (int i = 0 ; i < attrs.getLength() ; i ++) { 293 bufSchema.append(" "); 294 bufSchema.append(attrs.getLocalName(i)); 295 bufSchema.append("=\""); 296 bufSchema.append(attrs.getValue(i)); 297 bufSchema.append("\""); 298 } 299 bufSchema.append(">" ); 300 302 loader.startElement(uri, local, (raw == null) ? "" : raw, (attrs == null) ? new AttributesImpl () : attrs); 303 304 return; 305 } 306 307 if (inSchema) { 308 bufSchema.append("<"); 309 if (uri != null) { 310 int indexPref = tmpuri.indexOf(uri); 311 if (indexPref >= 0) { 312 String pref = (String )tmppref.get(indexPref); 313 if (pref != null && pref.length() > 0) { 314 bufSchema.append(pref); 315 bufSchema.append(":"); 316 } 317 } 318 } 319 bufSchema.append(local); 320 for (int i = 0 ; i < attrs.getLength() ; i ++) { 321 bufSchema.append(" "); 322 bufSchema.append(attrs.getLocalName(i)); 323 bufSchema.append("=\""); 324 bufSchema.append(attrs.getValue(i)); 325 bufSchema.append("\""); 326 } 327 bufSchema.append(">"); 328 329 loader.startElement(uri, local, (raw == null) ? "" : raw, (attrs == 330 null) ? new AttributesImpl () : attrs); 331 return; 332 } 333 334 if (local.equalsIgnoreCase(TAG_SOURCE)) { 335 sourcename = attrs.getValue("", ATT_SOURCE_NAME); 336 metawrapper.setSourceName(sourcename); 337 } 338 else { 339 if (local.equalsIgnoreCase(TAG_SCHEMAS)) { 340 } 342 else { 343 if (local.equalsIgnoreCase(TAG_COLLECTIONS)) { 344 metawrapper.initMetaCollections(); 345 } 346 else { 347 if (local.equalsIgnoreCase(TAG_COLLECTION)) { 348 metacollection = new MetaCollection(metadata, attrs.getValue("", ATT_COLLECTION_NAME)); 349 } 350 else { 351 if (local.equalsIgnoreCase(TAG_STEP)) { 352 boolean isattribute = isAttribute(attrs.getValue("", ATT_STEP_TYPE)); 353 metacollection.addMetaElement(attrs.getValue("", ATT_STEP_NS), attrs.getValue("", ATT_STEP_NAME), isattribute); 354 } 355 } 356 } 357 } 358 } 359 } 360 361 362 private boolean isAttribute(String name) { 363 if (name == null) { 364 return false; 365 } 366 if (name.equalsIgnoreCase(ATT_STEP_TYPE_ATT)) { 367 return true; 368 } 369 return false; 370 } 371 372 373 394 public void endElement(String uri, String local, String raw) throws SAXException { 395 if (local.equalsIgnoreCase(TAG_SCHEMA)) { 396 bufSchema.append("</"); 397 if (uri != null) { 398 int indexPref = tmpuri.indexOf(uri); 399 if (indexPref >= 0) { 400 String pref = (String )tmppref.get(indexPref); 401 if (pref != null && pref.length() > 0) { 402 bufSchema.append(pref); 403 bufSchema.append(":"); 404 } 405 } 406 } 407 bufSchema.append(local); 408 bufSchema.append(">"); 409 loader.endElement(uri, local, raw); 410 inSchema = false; 411 412 tmppref.clear(); 413 tmpuri.clear(); 414 415 if (loader != null) { 416 loader.endDocument(); 417 Schema schema = loader.getSchema(); 418 if (metawrapper != null) 419 metadata.putSchema(schema, bufSchema.toString()); 420 } 421 return; 422 } 423 424 if (inSchema) { 425 loader.endElement(uri, local, raw); 426 bufSchema.append("</"); 427 if (uri != null) { 428 int indexPref = tmpuri.indexOf(uri); 429 if (indexPref >= 0) { 430 String pref = (String )tmppref.get(indexPref); 431 if (pref != null && pref.length() > 0) { 432 bufSchema.append(pref); 433 bufSchema.append(":"); 434 } 435 } 436 } 437 bufSchema.append(local); 438 bufSchema.append(">"); 439 return; 440 } 441 442 if (local.equalsIgnoreCase(TAG_SOURCE)) { 443 } 445 else { 446 if (local.equalsIgnoreCase(TAG_SCHEMAS)) { 447 } 449 else { 450 if (local.equalsIgnoreCase(TAG_SCHEMADEFINITION)) { 451 } 455 else { 456 if (local.equalsIgnoreCase(TAG_COLLECTIONS)) { 457 } 459 else { 460 if (local.equalsIgnoreCase(TAG_COLLECTION)) { 461 metawrapper.addMetaCollection(metacollection); 462 } 463 else { 464 if (local.equalsIgnoreCase(TAG_STEP)) { 465 metacollection.addEndElement(); 466 } 467 } 468 } 469 } 470 } 471 } 472 } 473 474 475 500 public void characters(char ch[], int start, int length) 501 throws SAXException { 502 } 503 504 505 517 public void endDocument() 518 throws SAXException { 519 } 520 } 521 | Popular Tags |