Changeset 6621


Ignore:
Timestamp:
07/01/10 15:16:40 (9 years ago)
Author:
melissa
Message:

Merged r6614 and r6615 into trunk.

Location:
trunk/components
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/components/bio-formats/src/loci/formats/services

  • trunk/components/bio-formats/src/loci/formats/services/OMEXMLServiceImpl.java

    r6549 r6621  
    248248        Element e = XMLTools.parseDOM(xml).getDocumentElement(); 
    249249        String namespace = e.getAttribute("xmlns"); 
     250        if (namespace == null || namespace.equals("")) 
     251          namespace = e.getAttribute("xmlns:ome"); 
    250252 
    251253        return namespace.endsWith("ome.xsd") ? "2003-FC" : 
  • trunk/components/common/src/loci/common/xml/XMLTools.java

    r6612 r6621  
    3333import java.net.MalformedURLException; 
    3434import java.net.URL; 
     35import java.util.HashSet; 
    3536import java.util.Hashtable; 
     37import java.util.Set; 
    3638import java.util.StringTokenizer; 
     39import java.util.regex.Matcher; 
     40import java.util.regex.Pattern; 
    3741 
    3842import javax.xml.parsers.DocumentBuilder; 
     
    342346  } 
    343347 
     348  /** Replaces NS:tag with NS_tag for undeclared namespaces */ 
     349  public static String avoidUndeclaredNamespaces(String xml) { 
     350    int gt = xml.indexOf('>'); 
     351    if (gt > 0 && xml.startsWith("<?xml ")) 
     352      gt = xml.indexOf('>', gt + 1); 
     353    if (gt > 0) { 
     354      String firstTag = xml.substring(0, gt + 1).toLowerCase(); 
     355      Set namespaces = new HashSet(); 
     356      Pattern pattern = Pattern.compile(" xmlns:(\\w+)"); 
     357      Matcher matcher = pattern.matcher(firstTag); 
     358      while (matcher.find()) 
     359        namespaces.add(matcher.group(1)); 
     360 
     361      pattern = Pattern.compile("</?(\\w+):"); 
     362      matcher = pattern.matcher(xml); 
     363      while (matcher.find()) { 
     364        String namespace = matcher.group(1); 
     365        if (!namespace.startsWith("ns") && 
     366          !namespaces.contains(namespace.toLowerCase())) 
     367        { 
     368          int end = matcher.end(); 
     369          xml = xml.substring(0, end - 1) + "_" + xml.substring(end); 
     370        } 
     371      } 
     372    } 
     373    return xml; 
     374  } 
     375 
    344376  /** Transforms the given XML string using the specified XSLT stylesheet. */ 
    345377  public static String transformXML(String xml, Templates xslt) 
    346378    throws IOException 
    347379  { 
     380    xml = avoidUndeclaredNamespaces(xml); 
    348381    return transformXML(new StreamSource(new StringReader(xml)), xslt); 
    349382  } 
Note: See TracChangeset for help on using the changeset viewer.