Changeset 13470


Ignore:
Timestamp:
2006-12-08T16:23:29+13:00 (17 years ago)
Author:
shaoqun
Message:

make it accept xml files from a command line

Location:
trunk/gsdl
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/src/java/org/nzdl/gsdl/ApplyXSLT.java

    r13223 r13470  
    3737    private String mapping_file;
    3838
     39    public ApplyXSLT(){}
     40   
    3941    public ApplyXSLT(String xsl_file)
    4042    {
     
    257259
    258260
     261    private void translate(String xml_file, String xsl_file, String output_file)throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException, IOException{             
     262        if (output_file.equals("")) output_file = xml_file + ".out";
     263    TransformerFactory tFactory = TransformerFactory.newInstance();
     264    Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file));
     265    transformer.transform(new StreamSource(new File(xml_file)), new StreamResult(new FileOutputStream(output_file)));
     266        System.out.println("done");     
     267    }
     268
    259269    public static void main(String[] args)
    260270    {
     271
     272        String xml_file="";
     273        String xsl_file="";
     274        String mapping_file="";
     275        String output_file="";
     276     
    261277    // Checking Arguments
    262278    if(args.length < 1)
    263279        {
    264         System.err.println("Must provide a XSL file !");
    265         System.exit(-1);
    266         }
     280           printUsage();
     281        }           
     282
     283    for (int i=0;i<args.length;i++){
     284        if (args[i].equals("-m") && i+1 < args.length && !args[i+1].startsWith("-")){
     285         mapping_file = args[++i];
     286         checkFile(mapping_file);
     287        }
     288        else if (args[i].equals("-x") && i+1 < args.length && !args[i+1].startsWith("-")){
     289               xml_file = args[++i];
     290               checkFile(xml_file);
     291        }
     292        else if(args[i].equals("-t") && i+1 < args.length && !args[i+1].startsWith("-")){
     293                xsl_file = args[++i];
     294        checkFile(xsl_file);
     295        }
     296            else if(args[i].equals("-o") && i+1 < args.length && !args[i+1].startsWith("-")){
     297                output_file = args[++i];
     298       
     299        }
     300        else if(args[i].equals("-h")){
     301                printUsage();
     302        }
     303        else{
     304                printUsage();
     305        }
     306         
     307    }
     308
     309         
    267310    ApplyXSLT core = null;
    268 
    269     if (args.length == 1){
    270          core = new ApplyXSLT(args[0]);
    271     }
    272     else if(args.length == 2){
    273          core = new ApplyXSLT(args[0],args[1]);
    274     }
    275 
    276     if(core.process())
    277         {
    278         //System.err.println("System Message: Process Completed");
    279         }
    280     else
    281         {
    282         //System.err.println("System Message: Process Incompleted");
    283         }
     311   
     312    if (xml_file.equals("") && !xsl_file.equals("")){//read from pipe line
     313        if (mapping_file.equals("")){
     314             core = new ApplyXSLT(xsl_file);
     315        }
     316        else{
     317                core = new ApplyXSLT(xsl_file,mapping_file); 
     318        }
     319           
     320        if (core != null){
     321        core.process();
     322        }
     323        else{
     324        printUsage();
     325        }
     326    }
     327    else if(!xml_file.equals("") && !xsl_file.equals("")){
     328        core = new ApplyXSLT();
     329            System.out.println("hree");
     330            try {
     331        core.translate(xml_file,xsl_file,output_file);
     332        } 
     333        catch(Exception e){e.printStackTrace();}
     334    }
     335    else{
     336        printUsage();   
     337    }
     338   
     339    }
     340
     341    private static void checkFile(String filename){
     342         File file = new File(filename);
     343     if (!file.exists()){
     344         System.out.println("Error: "+filename+" doesn't exist!");
     345         System.exit(-1);
     346     }
     347    }
     348 
     349    private  static void printUsage(){
     350    System.out.println("Usage:ApplyXSLT  -x File -t File [-m File] [-o File]");
     351        System.out.println("\t-x spacifies the xml file (Note: optional for piped xml data)");
     352        System.out.println("\t-t spacifies the xsl file");
     353        System.out.println("\t-m specifies the mapping file (for MARCXMLPlugout.pm only)");
     354        System.out.println("\t-o specifies the translation output file name (if this option is absent, the name of the xml file  plus .out is used)");
     355        System.exit(-1);
    284356    }
    285357}
Note: See TracChangeset for help on using the changeset viewer.