Changeset 3147


Ignore:
Timestamp:
09/07/07 14:15:04 (13 years ago)
Author:
curtis
Message:

Smarter XML validation using SAX to parse out the schema path first.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/XMLTools.java

    r3145 r3147  
    2525package loci.formats; 
    2626 
     27import java.io.*; 
    2728import java.util.StringTokenizer; 
    28 import org.xml.sax.ErrorHandler; 
    29 import org.xml.sax.SAXParseException; 
     29import javax.xml.parsers.*; 
     30import org.xml.sax.*; 
     31import org.xml.sax.helpers.DefaultHandler; 
    3032 
    3133/** 
     
    7981 
    8082    // get path to schema from root element using SAX 
    81     // CTR TODO 
    82     String schemaPath = 
    83       "http://www.openmicroscopy.org/XMLschemas/OME/FC/ome.xsd"; 
     83    LogTools.println("Parsing schema path"); 
     84    ValidationSAXHandler saxHandler = new ValidationSAXHandler(); 
     85    SAXParserFactory saxFactory = SAXParserFactory.newInstance(); 
     86    Exception exception = null; 
     87    try { 
     88      SAXParser saxParser = saxFactory.newSAXParser(); 
     89      InputStream is = new ByteArrayInputStream(xml.getBytes()); 
     90      saxParser.parse(is, saxHandler); 
     91    } 
     92    catch (ParserConfigurationException exc) { exception = exc; } 
     93    catch (SAXException exc) { exception = exc; } 
     94    catch (IOException exc) { exception = exc; } 
     95    if (exception != null) { 
     96      LogTools.println("Error parsing schema path from " + label + ":"); 
     97      LogTools.trace(exception); 
     98      return; 
     99    } 
     100    String schemaPath = saxHandler.getSchemaPath(); 
     101    if (schemaPath == null) { 
     102      LogTools.println("No schema path found. Validation cannot continue."); 
     103      return; 
     104    } 
     105    else LogTools.println(schemaPath); 
    84106 
    85107    LogTools.println("Validating " + label); 
     
    118140 
    119141      // validate the XML 
    120       ValidationHandler handler = new ValidationHandler(); 
    121       r.setVar("handler", handler); 
    122       r.exec("validator.setErrorHandler(handler)"); 
     142      ValidationErrorHandler errorHandler = new ValidationErrorHandler(); 
     143      r.setVar("errorHandler", errorHandler); 
     144      r.exec("validator.setErrorHandler(errorHandler)"); 
    123145      r.exec("validator.validate(source)"); 
    124       if (handler.ok()) LogTools.println("No validation errors found."); 
     146      if (errorHandler.ok()) LogTools.println("No validation errors found."); 
    125147    } 
    126148    catch (ReflectException exc) { 
     
    167189  // -- Helper classes -- 
    168190 
    169   /** Used by testRead to handle XML validation errors. */ 
    170   private static class ValidationHandler implements ErrorHandler { 
     191  /** Used by validateXML to parse the XML block's schema path using SAX. */ 
     192  private static class ValidationSAXHandler extends DefaultHandler { 
     193    private String schemaPath; 
     194    private boolean first; 
     195    public String getSchemaPath() { return schemaPath; } 
     196    public void startDocument() { 
     197      schemaPath = null; 
     198      first = true; 
     199    } 
     200    public void startElement(String uri, 
     201      String localName, String qName, Attributes attributes) 
     202    { 
     203      if (!first) return; 
     204      first = false; 
     205 
     206      int len = attributes.getLength(); 
     207      String xmlns = null, xsiSchemaLocation = null; 
     208      for (int i=0; i<len; i++) { 
     209        String name = attributes.getQName(i); 
     210        if (name.equals("xmlns")) xmlns = attributes.getValue(i); 
     211        else if (name.equals("xsi:schemaLocation")) { 
     212          xsiSchemaLocation = attributes.getValue(i); 
     213        } 
     214      } 
     215      if (xmlns == null || xsiSchemaLocation == null) return; // not found 
     216 
     217      StringTokenizer st = new StringTokenizer(xsiSchemaLocation); 
     218      while (st.hasMoreTokens()) { 
     219        String token = st.nextToken(); 
     220        if (xmlns.equals(token)) { 
     221          // next token is the actual schema path 
     222          if (st.hasMoreTokens()) schemaPath = st.nextToken(); 
     223          break; 
     224        } 
     225      } 
     226    } 
     227  } 
     228 
     229  /** Used by validateXML to handle XML validation errors. */ 
     230  private static class ValidationErrorHandler implements ErrorHandler { 
    171231    private boolean ok = true; 
    172232    public boolean ok() { return ok; } 
     
    186246 
    187247} 
     248 
Note: See TracChangeset for help on using the changeset viewer.