Changeset 3143 for trunk


Ignore:
Timestamp:
09/06/07 14:34:58 (12 years ago)
Author:
curtis
Message:

Split non-OME-XML-specific XML functionality into separate XMLTools class.
Move XMLIndent program into Bio-Formats command line tools package.

Location:
trunk/loci/formats
Files:
1 added
2 edited
1 moved

Legend:

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

    r3108 r3143  
    2525package loci.formats; 
    2626 
    27 import java.util.StringTokenizer; 
    2827import org.xml.sax.ErrorHandler; 
    2928import org.xml.sax.SAXParseException; 
     
    144143   */ 
    145144  public static void validateOMEXML(String xml) { 
    146     // check Java version (XML validation only works in Java 1.5+) 
    147     String version = System.getProperty("java.version"); 
    148     int dot = version.indexOf("."); 
    149     if (dot >= 0) dot = version.indexOf(".", dot + 1); 
    150     float ver = Float.NaN; 
    151     if (dot >= 0) { 
    152       try { 
    153         ver = Float.parseFloat(version.substring(0, dot)); 
    154       } 
    155       catch (NumberFormatException exc) { } 
    156     } 
    157     if (ver != ver) { 
    158       LogTools.println("Warning: cannot determine if Java version\"" + 
    159         version + "\" supports Java v1.5. OME-XML validation may fail."); 
    160     } 
    161  
    162     if (ver != ver || ver >= 1.5f) { 
    163       // validate OME-XML (Java 1.5+ only) 
    164       LogTools.println("Validating OME-XML"); 
    165  
    166       // use reflection to avoid dependency on optional 
    167       // org.openmicroscopy.xml or javax.xml.validation packages 
    168       ReflectedUniverse r = new ReflectedUniverse(); 
    169  
    170       try { 
    171         // look up a factory for the W3C XML Schema language 
    172         r.setVar("schemaPath", "http://www.w3.org/2001/XMLSchema"); 
    173         r.exec("import javax.xml.validation.SchemaFactory"); 
    174         r.exec("factory = SchemaFactory.newInstance(schemaPath)"); 
    175  
    176         // compile the schema 
    177         r.exec("import java.net.URL"); 
    178         r.setVar("omePath", 
    179           "http://www.openmicroscopy.org/XMLschemas/OME/FC/ome.xsd"); 
    180         r.exec("schemaLocation = new URL(omePath)"); 
    181         r.exec("schema = factory.newSchema(schemaLocation)"); 
    182  
    183         // HACK - workaround for weird Linux bug preventing use of 
    184         // schema.newValidator() method even though it is "public final" 
    185         r.setAccessibilityIgnored(true); 
    186  
    187         // get a validator from the schema 
    188         r.exec("validator = schema.newValidator()"); 
    189  
    190         // prepare the XML source 
    191         r.exec("import java.io.StringReader"); 
    192         r.setVar("xml", xml); 
    193         r.exec("reader = new StringReader(xml)"); 
    194         r.exec("import org.xml.sax.InputSource"); 
    195         r.exec("is = new InputSource(reader)"); 
    196         r.exec("import javax.xml.transform.sax.SAXSource"); 
    197         r.exec("source = new SAXSource(is)"); 
    198  
    199         // validate the OME-XML 
    200         ValidationHandler handler = new ValidationHandler(); 
    201         r.setVar("handler", handler); 
    202         r.exec("validator.setErrorHandler(handler)"); 
    203         r.exec("validator.validate(source)"); 
    204         if (handler.ok()) LogTools.println("No validation errors found."); 
    205       } 
    206       catch (ReflectException exc) { 
    207         LogTools.println("Error validating OME-XML:"); 
    208         LogTools.trace(exc); 
    209       } 
    210     } 
    211   } 
    212  
    213   /** Indents XML to be more readable. */ 
    214   public static String indentXML(String xml) { return indentXML(xml, 3); } 
    215  
    216   /** Indents XML by the given spacing to be more readable. */ 
    217   public static String indentXML(String xml, int spacing) { 
    218     int indent = 0; 
    219     StringBuffer sb = new StringBuffer(); 
    220     StringTokenizer st = new StringTokenizer(xml, "<>", true); 
    221     boolean element = false; 
    222     while (st.hasMoreTokens()) { 
    223       String token = st.nextToken().trim(); 
    224       if (token.equals("")) continue; 
    225       if (token.equals("<")) { 
    226         element = true; 
    227         continue; 
    228       } 
    229       if (element && token.equals(">")) { 
    230         element = false; 
    231         continue; 
    232       } 
    233       if (element && token.startsWith("/")) indent -= spacing; 
    234       for (int j=0; j<indent; j++) sb.append(" "); 
    235       if (element) sb.append("<"); 
    236       sb.append(token); 
    237       if (element) sb.append(">"); 
    238       sb.append("\n"); 
    239       if (element && !token.startsWith("?") && 
    240         !token.startsWith("/") && !token.endsWith("/")) 
    241       { 
    242         indent += spacing; 
    243       } 
    244     } 
    245     return sb.toString(); 
     145    XMLTools.validateXML(xml, "OME-XML"); 
    246146  } 
    247147 
  • trunk/loci/formats/tools/ImageInfo.java

    r3137 r3143  
    526526      if (MetadataTools.isOMEXMLMetadata(ms)) { 
    527527        String xml = MetadataTools.getOMEXML((MetadataRetrieve) ms); 
    528         LogTools.println(MetadataTools.indentXML(xml)); 
     528        LogTools.println(XMLTools.indentXML(xml)); 
    529529        MetadataTools.validateOMEXML(xml); 
    530530      } 
  • trunk/loci/formats/tools/XMLIndent.java

    r3136 r3143  
    33// 
    44 
     5/* 
     6LOCI Bio-Formats package for reading and converting biological file formats. 
     7Copyright (C) 2005-@year@ Melissa Linkert, Curtis Rueden, Chris Allan, 
     8Eric Kjellman and Brian Loranger. 
     9 
     10This program is free software; you can redistribute it and/or modify 
     11it under the terms of the GNU Library General Public License as published by 
     12the Free Software Foundation; either version 2 of the License, or 
     13(at your option) any later version. 
     14 
     15This program is distributed in the hope that it will be useful, 
     16but WITHOUT ANY WARRANTY; without even the implied warranty of 
     17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     18GNU Library General Public License for more details. 
     19 
     20You should have received a copy of the GNU Library General Public License 
     21along with this program; if not, write to the Free Software 
     22Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
     23*/ 
     24 
     25package loci.formats.tools; 
     26 
    527import java.io.BufferedReader; 
    628import java.io.FileReader; 
    7 import loci.formats.MetadataTools; 
     29import loci.formats.XMLTools; 
    830 
    931/** Indents XML to be more readable. */ 
     
    2143      } 
    2244      in.close(); 
    23       System.out.println(MetadataTools.indentXML(sb.toString())); 
     45      System.out.println(XMLTools.indentXML(sb.toString())); 
    2446    } 
    2547  } 
Note: See TracChangeset for help on using the changeset viewer.