程序员的示例代码

以下代码示例举例说明了如何通过程序从 Java 应用程序使用“XML 脚本编制”工具:

package com.ibm.mobileservices.xmlscripting.sample;
 
/**
 * Title:       XML Scripting tool Sample Application for DB2 Everyplace
                Sync Server
 * Description: Example of using the XML tool API to write custom
                administration utilities.
 * Copyright:   Copyright (c) 2002
 * Company:     IBM
 */
 
import java.io.FileWriter;
import java.io.FileReader;
 
import com.ibm.mobileservices.xmlscripting.XMLTool;
import com.ibm.mobileservices.xmlscripting.XMLToolFactory;
 
/**
 * Main class for command line invocation of the XML Scripting sample tool.
 */
public class RunXML {
  
  private RunXML(){}
  
  /**
   * Method used when running as an application from the command line.
   * @param String[] an array of command line parameters
   * @return void
   * 
   * Command line arguments: -x|-d filename
   * 
   * -x        create XML file from control database.
   * -d        apply XML file to control database.
   * -v        validate XML file with its DTD.
   * filename       XML file name to process.
   * 
   * Example: java com.ibm.mobileservices.xmlscripting.RunXML -x
              "c:\mydb.xml"
   * Example: java com.ibm.mobileservices.xmlscripting.RunXML -d
              "c:\mycommands.xml"
   */
  public static void main(String args[]){
    String filename = null;
    String toOrFromXML = null;
    int xmlType = XMLTool.ADD;
    
    String xml = null;
    if (args != null && (args.length > 1 && args.length < 3) ){
      toOrFromXML = args[0]; //create xml or db from xml?
      "-x" means create xml,
      "-d" means populate control db from xml.
      filename = args[1]; //xml file name.
    }
    
    //check if filename is passed in otherwise display help.
    if (filename == null || 
        filename.equals("") || 
        filename.indexOf("?") > -1 ||
        toOrFromXML.indexOf("?") > -1){
      System.out.println("Command line arguments: -x|-d filename\n"+
            " -x        create XML file from control database.\n"+
            " -d        apply XML file to control database.\n"+
            " -v        validate XML file with its DTD.\n"+
            " filename       XML file name to process."+
                     
            " Example: java com.ibm.mobileservices.xmlscripting.sample.RunXML
                       -x \"c:\\mydb.xml\"\n"+
            " Example: java com.ibm.mobileservices.xmlscripting.sample.RunXML
                       -d \"c:\\mycommands.xml\"\n\n");
      return;
    }
    
    if (toOrFromXML.equalsIgnoreCase("-x") || toOrFromXML.equalsIgnoreCase
         ("/x")){ //generate xml file:
      System.out.println("Generating xml file: " + filename);
       try{
        //backUpOldFile(filename);        
        
        FileWriter fileWriter = null;      
       try{
          //get ready to write to the file:
          fileWriter = new FileWriter(filename);
          
          //create an instance of XMLTool. Each instance opens separate 
          //connections to the control database. It is strongly recommended 
          //that you use one instance to operate from single thread at a time. 
          //If you use the same instance from multiple threads, the threads
          //will typically be synchronized on that database connection. If
          //that causes a bottleneck,create multiple instances of the xml
          //needed.The only case tool to use from multiple threads. Usually
          //that should not be it may become handy is the creation and
          //removalof users from the system.
          //XMLTool xmlTool = XMLToolFactory.createXMLTool();
          
          //set progress listener
          //    (ProgressListener provides hooks to intercept events).
          //Default progress listener outputs messages to the System.out.
          //A custom progress listener is typically used to re-throw an
          //exception to abort processing of xml tags if the programmar
          //thinks that thinks that is necessary.
        xmlTool.setProgressListener
             (XMLToolFactory.createDefaultProgressListener());
          
          //There are different versions of generateXML, this is one of them:
          xmlTool.generateXML(fileWriter,xmlType);
        }
        finally{
          if (fileWriter!=null) fileWriter.close();
        }
        
        System.out.println("Done generating xml file: " + filename);
        
      }catch(Exception ex){
        System.out.println("Failed generating xml file.");
        System.out.println(ex.getMessage());
      }
    }    
    else if (toOrFromXML.equalsIgnoreCase("-d") || 
           toOrFromXML.equalsIgnoreCase("/d")) 
    { //apply xml to database:
      System.out.println("Applying xml file: " + filename);
       try{
       FileReader aReader = null;
       try{
          //get ready to read the file:
        aReader = new FileReader(filename);
          
          //create an instance of XML Tool:
        XMLTool xmlTool = XMLToolFactory.createXMLTool();
          
          //set progress listener:
        xmlTool.setProgressListener
             (XMLToolFactory.createDefaultProgressListener());
          
          //apply the xml commands:
          xmlTool.applyXML(aReader);
        }
        finally{
          if (aReader!=null) aReader.close();
        }
        System.out.println("Done applying xml.");
      }
      catch(Exception ex){
        System.out.println("Failed applying xml.");
        System.out.println(ex.getMessage());
      }
    }
    else if (toOrFromXML.equalsIgnoreCase("-v") || 
             toOrFromXML.equalsIgnoreCase("/v"))
    { //validate xml System.out.println("Validating xml file: " + filename);
       try{
       FileReader aReader = null;
       try{
        aReader = new FileReader(filename);
        XMLTool xmlTool = XMLToolFactory.createXMLTool();
        xmlTool.setProgressListener
             (XMLToolFactory.createDefaultProgressListener());
        xmlTool.validateXML(aReader);
        }
        finally{
          if (aReader!=null) aReader.close();
        }
        System.out.println("Done validating xml.");
      }
      catch(Exception ex){
        System.out.println("Failed validating xml.");
        System.out.println(ex.getMessage());
      }
    }
  }
} 

相关概念

相关任务

相关参考