Index: gsdl/trunk/build-src/src/java/org/nzdl/gsdl/ApplyXSLT.java
===================================================================
--- gsdl/trunk/build-src/src/java/org/nzdl/gsdl/ApplyXSLT.java	(revision 20874)
+++ gsdl/trunk/build-src/src/java/org/nzdl/gsdl/ApplyXSLT.java	(revision 20875)
@@ -23,350 +23,350 @@
 {
 
-    public static final String DOC_START = new String ("<?DocStart?>"); 
-    public static final String DOC_END = new String ("<?DocEnd?>"); 
-    public static final String INPUT_END = new String ("<?Done?>"); 
-
-    private static final String RECORD_ELEMENT = "record";
-    private static final String CONTROLFIELD_ELEMENT = "controlfield";
-    private static final String SUBFIELD_ELEMENT = "subfield";
-    private static final String LEADER_ELEMENT = "leader";
-
-    private final int BEFORE_READING = 0;
-    private final int IS_READING = 1;
-    private String xsl_file;
-    private String mapping_file;
-
-    public ApplyXSLT(){}
+  public static final String DOC_START = new String ("<?DocStart?>"); 
+  public static final String DOC_END = new String ("<?DocEnd?>"); 
+  public static final String INPUT_END = new String ("<?Done?>"); 
+
+  private static final String RECORD_ELEMENT = "record";
+  private static final String CONTROLFIELD_ELEMENT = "controlfield";
+  private static final String SUBFIELD_ELEMENT = "subfield";
+  private static final String LEADER_ELEMENT = "leader";
+
+  private final int BEFORE_READING = 0;
+  private final int IS_READING = 1;
+  private String xsl_file;
+  private String mapping_file;
+
+  public ApplyXSLT(){}
    
-    public ApplyXSLT(String xsl_file)
-    {
-	this.xsl_file = xsl_file; 
-    }
-
-    public ApplyXSLT(String xsl_file,String mapping_file){
-	this.xsl_file = xsl_file; 
-	this.mapping_file = mapping_file;
-    }
-
-    private boolean process()
-    {
-	try{
-
-	// Use System InputStream to receive piped data from the perl program
-	InputStreamReader ir = new InputStreamReader(System.in, "UTF8");
-	BufferedReader br = new BufferedReader(ir);
-	
-	int system_status = BEFORE_READING;
- 	String a_doc = new String();
-	String output_file = new String();
-
-
-	    while (br.ready()) {
+  public ApplyXSLT(String xsl_file)
+  {
+    this.xsl_file = xsl_file; 
+  }
+
+  public ApplyXSLT(String xsl_file,String mapping_file){
+    this.xsl_file = xsl_file; 
+    this.mapping_file = mapping_file;
+  }
+
+  private boolean process()
+  {
+    try{
+
+      // Use System InputStream to receive piped data from the perl program
+      InputStreamReader ir = new InputStreamReader(System.in, "UTF8");
+      BufferedReader br = new BufferedReader(ir);
+	
+      int system_status = BEFORE_READING;
+      String a_doc = new String();
+      String output_file = new String();
+
+
+      while (br.ready()) {
 		                   
-		String this_line = br.readLine();
-		if(system_status == BEFORE_READING){
-		    if(this_line.compareTo(DOC_START) == 0){
-			output_file = br.readLine(); // read the next line as the output file name
-			system_status = IS_READING;
-			a_doc = "";
-		    }
-		    else if(this_line.compareTo(INPUT_END) == 0){
-			return true;
-		    }
-		    else{
-			System.err.println("Undefined process status:" + this_line);
-			system_status = BEFORE_READING;
-		    }
+	String this_line = br.readLine();
+	if(system_status == BEFORE_READING){
+	  if(this_line.compareTo(DOC_START) == 0){
+	    output_file = br.readLine(); // read the next line as the output file name
+	    system_status = IS_READING;
+	    a_doc = "";
+	  }
+	  else if(this_line.compareTo(INPUT_END) == 0){
+	    return true;
+	  }
+	  else{
+	    System.err.println("Undefined process status:" + this_line);
+	    system_status = BEFORE_READING;
+	  }
 		    
-		}
-		else if(system_status == IS_READING){
-		    if(this_line.compareTo(DOC_END) == 0){
-			boolean result = false;
-			if (mapping_file !=null && !mapping_file.equals("")){
-			    result = translateXMLWithMapping(a_doc,output_file);			    
-			}
-			else{
-			    result = translateXML(a_doc,output_file); 
-			}
+	}
+	else if(system_status == IS_READING){
+	  if(this_line.compareTo(DOC_END) == 0){
+	    boolean result = false;
+	    if (mapping_file !=null && !mapping_file.equals("")){
+	      result = translateXMLWithMapping(a_doc,output_file);			    
+	    }
+	    else{
+	      result = translateXML(a_doc,output_file); 
+	    }
 			
-			if (!result){
-			    System.err.println("Translation Failed!!");
-			    return false;
-			}
+	    if (!result){
+	      System.err.println("Translation Failed!!");
+	      return false;
+	    }
 			
-			system_status = BEFORE_READING;
+	    system_status = BEFORE_READING;
 			
-		    }
-		    else{
-			a_doc = a_doc + this_line + "\n";
+	  }
+	  else{
+	    a_doc = a_doc + this_line + "\n";
 			
-		    }
-		}
-		else{		  
-		    System.err.println ("Undefined system status in ApplyXSLT.java main().");
-		    System.exit(-1);
-		    }
+	  }
+	}
+	else{		  
+	  System.err.println ("Undefined system status in ApplyXSLT.java main().");
+	  System.exit(-1);
+	}
 		
-	    }
-	}catch (Exception e)
-	    {
-		System.err.println("Receiving piped data error!" + e.toString());
-	    }
+      }
+    }catch (Exception e)
+      {
+	System.err.println("Receiving piped data error!" + e.toString());
+      }
 				
-	return false;
-    }
+    return false;
+  }
     
     
-    private boolean translateXML(String full_doc, String output_file) 
-	throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException
-    {
-	
- 	StringReader str = new StringReader(full_doc) ;
-	
-	TransformerFactory tFactory = TransformerFactory.newInstance();
-   	Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file));
-   	transformer.transform(new StreamSource(str), new StreamResult(new FileOutputStream(output_file)));
-	return true;
-    }
-
-    private boolean translateXMLWithMapping(String full_doc, String output_file) 
-	throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException
-    {	
-	StringReader str = new StringReader(full_doc) ;
-
-	try{
-	    TransformerFactory tFactory = TransformerFactory.newInstance();
-	    Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file));
-	    
-	    Document mapping_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(mapping_file);
-	    Element mapping =mapping_doc.getDocumentElement(); 
-	    
-	    transformer.setParameter("mapping",mapping);
-	    
-	    Document output_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
-	    
-	    transformer.transform(new StreamSource(str), new DOMResult(output_doc));
-	    
-	    calculateRecordsLength(output_doc);
+  private boolean translateXML(String full_doc, String output_file) 
+    throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException
+  {
+	
+    StringReader str = new StringReader(full_doc) ;
+	
+    TransformerFactory tFactory = TransformerFactory.newInstance();
+    Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file));
+    transformer.transform(new StreamSource(str), new StreamResult(new FileOutputStream(output_file)));
+    return true;
+  }
+
+  private boolean translateXMLWithMapping(String full_doc, String output_file) 
+    throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException
+  {	
+    StringReader str = new StringReader(full_doc) ;
+
+    try{
+      TransformerFactory tFactory = TransformerFactory.newInstance();
+      Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file));
+	    
+      Document mapping_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(mapping_file);
+      Element mapping =mapping_doc.getDocumentElement(); 
+	    
+      transformer.setParameter("mapping",mapping);
+	    
+      Document output_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+	    
+      transformer.transform(new StreamSource(str), new DOMResult(output_doc));
+	    
+      calculateRecordsLength(output_doc);
 	   
-	    transformer = tFactory.newTransformer();
+      transformer = tFactory.newTransformer();
 	   
-	    transformer.transform(new DOMSource(output_doc), new StreamResult(new FileOutputStream(output_file)));
-
+      transformer.transform(new DOMSource(output_doc), new StreamResult(new FileOutputStream(output_file)));
+
+    }
+    catch(Exception e){
+      e.printStackTrace();
+      return false;
+    }
+
+    return true;
+  }
+
+  private void calculateRecordsLength(Document output_doc){
+    NodeList records = output_doc.getDocumentElement().getElementsByTagName(RECORD_ELEMENT);
+
+    for(int i=0;i<records.getLength();i++){
+      Element record = (Element)records.item(i);
+      calculateRecordLength(record);
+    }
+  }
+
+  private void calculateRecordLength(Element record){
+    int total_length =0;	
+    NodeList controlfileds = record.getElementsByTagName(CONTROLFIELD_ELEMENT);
+    for(int i=0;i<controlfileds.getLength();i++){
+      Element controlfiled = (Element)controlfileds.item(i);
+      total_length +=getElementTextValue(controlfiled).length();
+    }
+	
+    NodeList subfileds = record.getElementsByTagName(SUBFIELD_ELEMENT);
+    for(int i=0;i<subfileds.getLength();i++){
+      Element subfiled = (Element)subfileds.item(i);
+      total_length +=getElementTextValue(subfiled).length();
+    }
+
+    String record_length = total_length+"";
+    //fill in a extra digit as record length needs to be five characters long
+    if (total_length < 10000){
+      record_length = "0"+record_length;
+      if (total_length < 1000){
+	record_length = "0"+record_length;	
+      }
+      if (total_length < 100){
+	record_length = "0"+record_length;	
+      }
+      if (total_length < 10){
+	record_length = "0"+record_length;	
+      }
+	   
+    }
+
+    NodeList leaders = record.getElementsByTagName(LEADER_ELEMENT);	
+	
+    //only one leader element
+    if (leaders.getLength() >0){
+      Element leader_element = (Element)leaders.item(0);
+      removeFirstTextNode(leader_element);
+      leader_element.insertBefore(leader_element.getOwnerDocument().createTextNode(record_length),leader_element.getFirstChild()); 
+    }
+
+  }
+
+  private void removeFirstTextNode(Element element){
+    //remove the first text node
+    NodeList children_nodelist = element.getChildNodes();
+    for (int i = 0; i < children_nodelist.getLength(); i++) {
+      Node child_node = children_nodelist.item(i);
+      if (child_node.getNodeType() == Node.TEXT_NODE) {
+	element.removeChild(child_node);
+	return;
+      }
+    }
+
+  }
+
+  private String getElementTextValue(Element element)
+  {
+    String text ="";	
+
+    // Find the node child
+    NodeList children_nodelist = element.getChildNodes();
+    for (int i = 0; i < children_nodelist.getLength(); i++) {
+      Node child_node = children_nodelist.item(i);
+      if (child_node.getNodeType() == Node.TEXT_NODE) {
+	text +=child_node.getNodeValue();
+      }
+    }
+
+    return text;
+  }
+ 
+
+  private void setMappingVariable(Document style_doc){
+    Node child = style_doc.getDocumentElement().getFirstChild();
+    while(child != null) {
+      String name = child.getNodeName();
+      if (name.equals("xsl:variable")) {
+	Element variable_element = (Element)child;
+	if ( variable_element.getAttribute("name").trim().equals("mapping")){
+	  variable_element.setAttribute("select","document('"+mapping_file+"')/Mapping");
 	}
-	catch(Exception e){
-	    e.printStackTrace();
-	    return false;
-	}
-
-	return true;
-    }
-
-    private void calculateRecordsLength(Document output_doc){
-	NodeList records = output_doc.getDocumentElement().getElementsByTagName(RECORD_ELEMENT);
-
-	for(int i=0;i<records.getLength();i++){
-	    Element record = (Element)records.item(i);
-	    calculateRecordLength(record);
-	}
-    }
-
-    private void calculateRecordLength(Element record){
-	int total_length =0;	
-	NodeList controlfileds = record.getElementsByTagName(CONTROLFIELD_ELEMENT);
-	for(int i=0;i<controlfileds.getLength();i++){
-	    Element controlfiled = (Element)controlfileds.item(i);
-	    total_length +=getElementTextValue(controlfiled).length();
-	}
-	
-	NodeList subfileds = record.getElementsByTagName(SUBFIELD_ELEMENT);
-	for(int i=0;i<subfileds.getLength();i++){
-	    Element subfiled = (Element)subfileds.item(i);
-	    total_length +=getElementTextValue(subfiled).length();
-	}
-
-	String record_length = total_length+"";
-	//fill in a extra digit as record length needs to be five characters long
-	if (total_length < 10000){
-	    record_length = "0"+record_length;
-	    if (total_length < 1000){
-		record_length = "0"+record_length;	
-	    }
-	    if (total_length < 100){
-		record_length = "0"+record_length;	
-	    }
-	    if (total_length < 10){
-		 record_length = "0"+record_length;	
-	    }
-	   
-	}
-
-	NodeList leaders = record.getElementsByTagName(LEADER_ELEMENT);	
-	
-	//only one leader element
-        if (leaders.getLength() >0){
-	    Element leader_element = (Element)leaders.item(0);
-	    removeFirstTextNode(leader_element);
-	    leader_element.insertBefore(leader_element.getOwnerDocument().createTextNode(record_length),leader_element.getFirstChild()); 
-	}
-
-    }
-
-    private void removeFirstTextNode(Element element){
-	//remove the first text node
-	NodeList children_nodelist = element.getChildNodes();
-	for (int i = 0; i < children_nodelist.getLength(); i++) {
-	    Node child_node = children_nodelist.item(i);
-	    if (child_node.getNodeType() == Node.TEXT_NODE) {
-		element.removeChild(child_node);
-		return;
-	    }
-	}
-
-    }
-
-    private String getElementTextValue(Element element)
-    {
-	String text ="";	
-
-	// Find the node child
-	NodeList children_nodelist = element.getChildNodes();
-	for (int i = 0; i < children_nodelist.getLength(); i++) {
-	    Node child_node = children_nodelist.item(i);
-	    if (child_node.getNodeType() == Node.TEXT_NODE) {
-		text +=child_node.getNodeValue();
-	    }
-	}
-
-       	return text;
-    }
- 
-
-    private void setMappingVariable(Document style_doc){
-	Node child = style_doc.getDocumentElement().getFirstChild();
-	while(child != null) {
-	    String name = child.getNodeName();
-	    if (name.equals("xsl:variable")) {
-		Element variable_element = (Element)child;
-		if ( variable_element.getAttribute("name").trim().equals("mapping")){
-		    variable_element.setAttribute("select","document('"+mapping_file+"')/Mapping");
-		}
-	    }
-	    child = child.getNextSibling();
-	}
-	
+      }
+      child = child.getNextSibling();
+    }
+	
+  } 
+
+
+  private void translate(String xml_file, String xsl_file, String output_file)throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException, IOException{             
+         
+    TransformerFactory tFactory = TransformerFactory.newInstance();
+    Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file));
+           
+    OutputStreamWriter output = null;  
+    if (output_file.equals("")) {
+      output = new OutputStreamWriter(System.out, "UTF-8");
+    }
+    else{
+      output = new OutputStreamWriter(new FileOutputStream(output_file), "UTF-8"); 
+    }
+       
+    transformer.transform(new StreamSource(new File(xml_file)),new StreamResult(output));
+        
+  }
+
+  static public String replaceAll(String source_string, String match_regexp, String replace_string)
+  {
+    return source_string.replaceAll(match_regexp, replace_string);
+  }
+
+
+  public static void main(String[] args) 
+  { 
+
+    String xml_file="";
+    String xsl_file="";
+    String mapping_file="";
+    String output_file="";
+      
+    // Checking Arguments
+    if(args.length < 1)
+      {
+	printUsage(); 
+      }           
+
+    for (int i=0;i<args.length;i++){
+      if (args[i].equals("-m") && i+1 < args.length && !args[i+1].startsWith("-")){
+	mapping_file = args[++i];
+	checkFile(mapping_file.replaceAll("file:///",""));
+      }
+      else if (args[i].equals("-x") && i+1 < args.length && !args[i+1].startsWith("-")){
+	xml_file = args[++i]; 
+	checkFile(xml_file.replaceAll("file:///",""));
+      }
+      else if(args[i].equals("-t") && i+1 < args.length && !args[i+1].startsWith("-")){
+	xsl_file = args[++i];
+	checkFile( xsl_file.replaceAll("file:///","")); 
+      }
+      else if(args[i].equals("-o") && i+1 < args.length && !args[i+1].startsWith("-")){
+	output_file = args[++i];
+	    
+      }
+      else if(args[i].equals("-h")){
+	printUsage();
+      }
+      else{
+	printUsage();
+      }
+		  
+    }
+
+         
+    ApplyXSLT core = null;
+	
+    if (xml_file.equals("") && !xsl_file.equals("")){//read from pipe line
+      if (mapping_file.equals("")){ 
+	core = new ApplyXSLT(xsl_file);
+      }
+      else{
+	core = new ApplyXSLT(xsl_file,mapping_file);  
+      }
+            
+      if (core != null){
+	core.process();
+      }
+      else{
+	printUsage(); 
+      }
+    }
+    else if(!xml_file.equals("") && !xsl_file.equals("")){
+      core = new ApplyXSLT();
+      try {
+	core.translate(xml_file,xsl_file,output_file); 
+      }  
+      catch(Exception e){e.printStackTrace();}
     } 
-
-
-    private void translate(String xml_file, String xsl_file, String output_file)throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException, IOException{             
-         
-      	TransformerFactory tFactory = TransformerFactory.newInstance();
-   	Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file));
-           
-	OutputStreamWriter output = null;  
-	if (output_file.equals("")) {
-             output = new OutputStreamWriter(System.out, "UTF-8");
-	}
-	else{
-            output = new OutputStreamWriter(new FileOutputStream(output_file), "UTF-8"); 
-	}
-       
-        transformer.transform(new StreamSource(new File(xml_file)),new StreamResult(output));
-        
-    }
-
-     static public String replaceAll(String source_string, String match_regexp, String replace_string)
-    {
-	return source_string.replaceAll(match_regexp, replace_string);
-    }
-
-
-    public static void main(String[] args) 
-    { 
-
-        String xml_file="";
-        String xsl_file="";
-        String mapping_file="";
-        String output_file="";
-      
-	// Checking Arguments
-	if(args.length < 1)
-	    {
-	       printUsage(); 
-	    }           
-
-	for (int i=0;i<args.length;i++){
-	    if (args[i].equals("-m") && i+1 < args.length && !args[i+1].startsWith("-")){
-		 mapping_file = args[++i];
-         	 checkFile(mapping_file.replaceAll("file:///",""));
-	    }
-	    else if (args[i].equals("-x") && i+1 < args.length && !args[i+1].startsWith("-")){
-               xml_file = args[++i]; 
-               checkFile(xml_file.replaceAll("file:///",""));
-	    }
-	    else if(args[i].equals("-t") && i+1 < args.length && !args[i+1].startsWith("-")){
-                xsl_file = args[++i];
-	         checkFile( xsl_file.replaceAll("file:///","")); 
-	    }
-            else if(args[i].equals("-o") && i+1 < args.length && !args[i+1].startsWith("-")){
-                output_file = args[++i];
-	    
-	    }
-	    else if(args[i].equals("-h")){
-                printUsage();
-	    }
-	    else{
-                printUsage();
-	    }
-		  
-	}
-
-         
-	ApplyXSLT core = null;
-	
- 	if (xml_file.equals("") && !xsl_file.equals("")){//read from pipe line
-	    if (mapping_file.equals("")){ 
-           	 core = new ApplyXSLT(xsl_file);
-	    }
-	    else{
-                core = new ApplyXSLT(xsl_file,mapping_file);  
-	    }
-            
-	    if (core != null){
-		core.process();
-	    }
-	    else{
-		printUsage(); 
-	    }
- 	}
- 	else if(!xml_file.equals("") && !xsl_file.equals("")){
-	    core = new ApplyXSLT();
-            try {
-		core.translate(xml_file,xsl_file,output_file); 
-	    }  
-	    catch(Exception e){e.printStackTrace();}
- 	} 
-	else{
-	    printUsage();    
-	}
-	
-    }
-
-    private static void checkFile(String filename){
-         File file = new File(filename);
-	 if (!file.exists()){
-	     System.out.println("Error: "+filename+" doesn't exist!");
-	     System.exit(-1);
-	 }
-    }
+    else{
+      printUsage();    
+    }
+	
+  }
+
+  private static void checkFile(String filename){
+    File file = new File(filename);
+    if (!file.exists()){
+      System.out.println("Error: "+filename+" doesn't exist!");
+      System.exit(-1);
+    }
+  }
   
-    private  static void printUsage(){
-	System.out.println("Usage:ApplyXSLT  -x File -t File [-m File] [-o File]");
-        System.out.println("\t-x spacifies the xml file (Note: optional for piped xml data)");
-        System.out.println("\t-t spacifies the xsl file");
-    	System.out.println("\t-m specifies the mapping file (for MARCXMLPlugout.pm only)");
-        System.out.println("\t-o specifies the output file name (output to screen if this option is absent)");
-        System.exit(-1);
-    }
+  private  static void printUsage(){
+    System.out.println("Usage:ApplyXSLT  -x File -t File [-m File] [-o File]");
+    System.out.println("\t-x spacifies the xml file (Note: optional for piped xml data)");
+    System.out.println("\t-t spacifies the xsl file");
+    System.out.println("\t-m specifies the mapping file (for MARCXMLPlugout.pm only)");
+    System.out.println("\t-o specifies the output file name (output to screen if this option is absent)");
+    System.exit(-1);
+  }
 }
 
