Changeset 28709

Show
Ignore:
Timestamp:
28.11.2013 15:32:52 (6 years ago)
Author:
kjdon
Message:

allow mapping file when processing an xml file

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/build-src/src/java/org/nzdl/gsdl/ApplyXSLT.java

    r28384 r28709  
    349349         
    350350  } 
     351  private void translateWithMapping(String xml_file, String xsl_file, String mapping_file, String output_file)throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException {              
     352          
     353    TransformerFactory tFactory = TransformerFactory.newInstance(); 
     354    Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file)); 
     355            
     356    OutputStreamWriter output = null;   
     357    if (output_file.equals("")) { 
     358      output = new OutputStreamWriter(System.out, "UTF-8"); 
     359    } 
     360    else{ 
     361      output = new OutputStreamWriter(new FileOutputStream(output_file), "UTF-8");  
     362    } 
     363    try { 
     364      Document mapping_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(mapping_file); 
     365      Element mapping =mapping_doc.getDocumentElement();  
     366         
     367      transformer.setParameter("mapping",mapping); 
     368    } catch (Exception e) { 
     369      System.err.println("Couldn't load in mapping file"); 
     370      e.printStackTrace(); 
     371    } 
     372    setTransformerParams(transformer); // sourcelang and targetlang and any further custom parameters to be passed into the XSLT 
     373    transformer.transform(new StreamSource(new File(xml_file)),new StreamResult(output)); 
     374         
     375  } 
    351376 
    352377  static public String replaceAll(String source_string, String match_regexp, String replace_string) 
     
    483508    core = new ApplyXSLT(sourcelang, targetlang, paramMap); 
    484509      try { 
    485     core.translate(xml_file,xsl_file,output_file);  
    486       }   
     510    if (mapping_file.equals("")) { 
     511      core.translate(xml_file,xsl_file,output_file);  
     512    } else { 
     513      core.translateWithMapping(xml_file,xsl_file,mapping_file, output_file);  
     514    }   
     515      } 
    487516      catch(Exception e){e.printStackTrace();} 
    488517    }