Changeset 5467


Ignore:
Timestamp:
09/14/09 10:15:03 (11 years ago)
Author:
curtis
Message:

Reorder generated OME-XML using the reorder-2008-09.xsl stylesheet.

Location:
trunk/components
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/components/bio-formats/build.properties

    r5076 r5467  
    1414component.deprecation    = true 
    1515 
    16 component.resources-bin  = loci/formats/bio-formats-logo.png 
     16component.resources-bin  = loci/formats/bio-formats-logo.png \ 
     17                           loci/formats/meta/reorder-2008-09.xsl 
    1718component.resources-text = loci/formats/*.txt 
    1819 
  • trunk/components/bio-formats/src/loci/formats/MetadataTools.java

    r5433 r5467  
    2424package loci.formats; 
    2525 
     26import java.io.IOException; 
    2627import java.util.Arrays; 
    2728import java.util.Hashtable; 
     29 
     30import javax.xml.transform.Templates; 
    2831 
    2932import loci.common.DateTools; 
     
    5053 */ 
    5154public final class MetadataTools { 
     55 
     56  // -- Static fields -- 
     57 
     58  private static Templates reorderXSLT = 
     59    XMLTools.getStylesheet("meta/reorder-2008-09.xsl", MetadataTools.class); 
    5260 
    5361  // -- Constructor -- 
     
    233241    ReflectedUniverse r = new ReflectedUniverse(); 
    234242    r.setVar("omexmlMeta", omexmlMeta); 
    235     try { 
    236       return (String) r.exec("omexmlMeta.dumpXML()"); 
     243    String xml = null; 
     244    try { 
     245      xml = (String) r.exec("omexmlMeta.dumpXML()"); 
    237246    } 
    238247    catch (ReflectException exc) { 
    239248      LogTools.traceDebug(exc); 
    240249    } 
    241     return null; 
     250    String reordered = null; 
     251    try { 
     252      reordered = XMLTools.transformXML(xml, reorderXSLT); 
     253    } 
     254    catch (IOException exc) { 
     255      LogTools.traceDebug(exc); 
     256    } 
     257    return reordered == null ? xml : reordered; 
    242258  } 
    243259 
  • trunk/components/bio-formats/src/loci/formats/tools/ImageInfo.java

    r5405 r5467  
    760760    if (mr != null) { 
    761761      String xml = MetadataTools.getOMEXML(mr); 
    762       LogTools.println(XMLTools.indentXML(xml)); 
     762      LogTools.println(XMLTools.indentXML(xml, true)); 
    763763      MetadataTools.validateOMEXML(xml); 
    764764    } 
  • trunk/components/common/src/loci/common/XMLTools.java

    r5384 r5467  
    2828import java.io.InputStream; 
    2929import java.io.StringReader; 
     30import java.io.StringWriter; 
    3031import java.net.MalformedURLException; 
    3132import java.net.URL; 
     
    3637import javax.xml.parsers.SAXParser; 
    3738import javax.xml.parsers.SAXParserFactory; 
     39import javax.xml.transform.Result; 
     40import javax.xml.transform.Source; 
     41import javax.xml.transform.Templates; 
     42import javax.xml.transform.Transformer; 
     43import javax.xml.transform.TransformerConfigurationException; 
     44import javax.xml.transform.TransformerException; 
     45import javax.xml.transform.TransformerFactory; 
    3846import javax.xml.transform.sax.SAXSource; 
     47import javax.xml.transform.stream.StreamResult; 
     48import javax.xml.transform.stream.StreamSource; 
    3949import javax.xml.validation.Schema; 
    4050import javax.xml.validation.SchemaFactory; 
     
    6777    SAXParserFactory.newInstance(); 
    6878 
     79  /** Factory for generating XSLT transformers. */ 
     80  public static final TransformerFactory TRANSFORM_FACTORY = 
     81    TransformerFactory.newInstance(); 
     82 
    6983  // -- Constructor -- 
    7084 
     
    8498  } 
    8599 
     100  /** Parses the given XML string into a list of key/value pairs. */ 
    86101  public static Hashtable<String, String> parseXML(String xml) 
    87102    throws IOException 
     
    92107  } 
    93108 
     109  /** 
     110   * Parses the given XML string into a list of key/value pairs 
     111   * using the specified XML handler. 
     112   */ 
    94113  public static void parseXML(String xml, DefaultHandler handler) 
    95114    throws IOException 
     
    98117  } 
    99118 
     119  /** 
     120   * Parses the XML string from the given input stream into 
     121   * a list of key/value pairs using the specified XML handler. 
     122   */ 
    100123  public static void parseXML(RandomAccessInputStream stream, 
    101124    DefaultHandler handler) throws IOException 
     
    107130  } 
    108131 
     132  /** 
     133   * Parses the XML string from the given byte array into 
     134   * a list of key/value pairs using the specified XML handler. 
     135   */ 
    109136  public static void parseXML(byte[] xml, DefaultHandler handler) 
    110137    throws IOException 
     
    125152 
    126153    } 
     154  } 
     155 
     156  /** Gets an XSLT template from the given resource location. */ 
     157  public static Templates getStylesheet(String resourcePath, 
     158    Class sourceClass) 
     159  { 
     160    InputStream xsltStream = sourceClass.getResourceAsStream(resourcePath); 
     161    StreamSource xsltSource = new StreamSource(xsltStream); 
     162    TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
     163    try { 
     164      return transformerFactory.newTemplates(xsltSource); 
     165    } 
     166    catch (TransformerConfigurationException exc) { 
     167      LogTools.traceDebug(exc); 
     168    } 
     169    return null; 
     170  } 
     171 
     172  /** 
     173   * Transforms the given XMl string according to the specified XSLT stylesheet. 
     174   */ 
     175  public static String transformXML(String xml, Templates xslt) 
     176    throws IOException 
     177  { 
     178    Transformer trans; 
     179    try { 
     180      trans = xslt.newTransformer(); 
     181    } 
     182    catch (TransformerConfigurationException exc) { 
     183      IOException e = new IOException(); 
     184      e.initCause(exc); 
     185      throw e; 
     186    } 
     187    StringReader xmlReader = new StringReader(xml); 
     188    Source xmlSource = new StreamSource(xmlReader); 
     189    StringWriter xmlWriter = new StringWriter(); 
     190    StreamResult xmlResult = new StreamResult(xmlWriter); 
     191    try { 
     192      trans.transform(xmlSource, xmlResult); 
     193    } 
     194    catch (TransformerException exc) { 
     195      IOException e = new IOException(); 
     196      e.initCause(exc); 
     197      throw e; 
     198    } 
     199    return xmlWriter.toString(); 
    127200  } 
    128201 
     
    230303  public static String indentXML(String xml, int spacing) { 
    231304    return indentXML(xml, spacing, false); 
     305  } 
     306 
     307  /** 
     308   * Indents XML to be more readable, avoiding any whitespace 
     309   * injection into CDATA if the preserveCData flag is set. 
     310   */ 
     311  public static String indentXML(String xml, boolean preserveCData) { 
     312    return indentXML(xml, 3, preserveCData); 
    232313  } 
    233314 
Note: See TracChangeset for help on using the changeset viewer.