- Timestamp:
- 2009-11-02T13:02:31+13:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gsdl/trunk/build-src/src/java/org/nzdl/gsdl/ApplyXSLT.java
r16561 r20875 23 23 { 24 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 25 public static final String DOC_START = new String ("<?DocStart?>"); 26 public static final String DOC_END = new String ("<?DocEnd?>"); 27 public static final String INPUT_END = new String ("<?Done?>"); 28 29 private static final String RECORD_ELEMENT = "record"; 30 private static final String CONTROLFIELD_ELEMENT = "controlfield"; 31 private static final String SUBFIELD_ELEMENT = "subfield"; 32 private static final String LEADER_ELEMENT = "leader"; 33 34 private final int BEFORE_READING = 0; 35 private final int IS_READING = 1; 36 private String xsl_file; 37 private String mapping_file; 38 39 public ApplyXSLT(){} 40 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 41 public ApplyXSLT(String xsl_file) 42 { 43 this.xsl_file = xsl_file; 44 } 45 46 public ApplyXSLT(String xsl_file,String mapping_file){ 47 this.xsl_file = xsl_file; 48 this.mapping_file = mapping_file; 49 } 50 51 private boolean process() 52 { 53 try{ 54 55 // Use System InputStream to receive piped data from the perl program 56 InputStreamReader ir = new InputStreamReader(System.in, "UTF8"); 57 BufferedReader br = new BufferedReader(ir); 58 59 int system_status = BEFORE_READING; 60 String a_doc = new String(); 61 String output_file = new String(); 62 63 64 while (br.ready()) { 65 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 66 String this_line = br.readLine(); 67 if(system_status == BEFORE_READING){ 68 if(this_line.compareTo(DOC_START) == 0){ 69 output_file = br.readLine(); // read the next line as the output file name 70 system_status = IS_READING; 71 a_doc = ""; 72 } 73 else if(this_line.compareTo(INPUT_END) == 0){ 74 return true; 75 } 76 else{ 77 System.err.println("Undefined process status:" + this_line); 78 system_status = BEFORE_READING; 79 } 80 80 81 82 83 84 85 86 87 88 89 90 81 } 82 else if(system_status == IS_READING){ 83 if(this_line.compareTo(DOC_END) == 0){ 84 boolean result = false; 85 if (mapping_file !=null && !mapping_file.equals("")){ 86 result = translateXMLWithMapping(a_doc,output_file); 87 } 88 else{ 89 result = translateXML(a_doc,output_file); 90 } 91 91 92 93 94 95 92 if (!result){ 93 System.err.println("Translation Failed!!"); 94 return false; 95 } 96 96 97 97 system_status = BEFORE_READING; 98 98 99 100 101 99 } 100 else{ 101 a_doc = a_doc + this_line + "\n"; 102 102 103 104 105 106 107 108 103 } 104 } 105 else{ 106 System.err.println ("Undefined system status in ApplyXSLT.java main()."); 107 System.exit(-1); 108 } 109 109 110 111 112 113 114 110 } 111 }catch (Exception e) 112 { 113 System.err.println("Receiving piped data error!" + e.toString()); 114 } 115 115 116 117 116 return false; 117 } 118 118 119 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 120 private boolean translateXML(String full_doc, String output_file) 121 throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException 122 { 123 124 StringReader str = new StringReader(full_doc) ; 125 126 TransformerFactory tFactory = TransformerFactory.newInstance(); 127 Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file)); 128 transformer.transform(new StreamSource(str), new StreamResult(new FileOutputStream(output_file))); 129 return true; 130 } 131 132 private boolean translateXMLWithMapping(String full_doc, String output_file) 133 throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException 134 { 135 StringReader str = new StringReader(full_doc) ; 136 137 try{ 138 TransformerFactory tFactory = TransformerFactory.newInstance(); 139 Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file)); 140 141 Document mapping_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(mapping_file); 142 Element mapping =mapping_doc.getDocumentElement(); 143 144 transformer.setParameter("mapping",mapping); 145 146 Document output_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 147 148 transformer.transform(new StreamSource(str), new DOMResult(output_doc)); 149 150 calculateRecordsLength(output_doc); 151 151 152 152 transformer = tFactory.newTransformer(); 153 153 154 transformer.transform(new DOMSource(output_doc), new StreamResult(new FileOutputStream(output_file))); 155 154 transformer.transform(new DOMSource(output_doc), new StreamResult(new FileOutputStream(output_file))); 155 156 } 157 catch(Exception e){ 158 e.printStackTrace(); 159 return false; 160 } 161 162 return true; 163 } 164 165 private void calculateRecordsLength(Document output_doc){ 166 NodeList records = output_doc.getDocumentElement().getElementsByTagName(RECORD_ELEMENT); 167 168 for(int i=0;i<records.getLength();i++){ 169 Element record = (Element)records.item(i); 170 calculateRecordLength(record); 171 } 172 } 173 174 private void calculateRecordLength(Element record){ 175 int total_length =0; 176 NodeList controlfileds = record.getElementsByTagName(CONTROLFIELD_ELEMENT); 177 for(int i=0;i<controlfileds.getLength();i++){ 178 Element controlfiled = (Element)controlfileds.item(i); 179 total_length +=getElementTextValue(controlfiled).length(); 180 } 181 182 NodeList subfileds = record.getElementsByTagName(SUBFIELD_ELEMENT); 183 for(int i=0;i<subfileds.getLength();i++){ 184 Element subfiled = (Element)subfileds.item(i); 185 total_length +=getElementTextValue(subfiled).length(); 186 } 187 188 String record_length = total_length+""; 189 //fill in a extra digit as record length needs to be five characters long 190 if (total_length < 10000){ 191 record_length = "0"+record_length; 192 if (total_length < 1000){ 193 record_length = "0"+record_length; 194 } 195 if (total_length < 100){ 196 record_length = "0"+record_length; 197 } 198 if (total_length < 10){ 199 record_length = "0"+record_length; 200 } 201 202 } 203 204 NodeList leaders = record.getElementsByTagName(LEADER_ELEMENT); 205 206 //only one leader element 207 if (leaders.getLength() >0){ 208 Element leader_element = (Element)leaders.item(0); 209 removeFirstTextNode(leader_element); 210 leader_element.insertBefore(leader_element.getOwnerDocument().createTextNode(record_length),leader_element.getFirstChild()); 211 } 212 213 } 214 215 private void removeFirstTextNode(Element element){ 216 //remove the first text node 217 NodeList children_nodelist = element.getChildNodes(); 218 for (int i = 0; i < children_nodelist.getLength(); i++) { 219 Node child_node = children_nodelist.item(i); 220 if (child_node.getNodeType() == Node.TEXT_NODE) { 221 element.removeChild(child_node); 222 return; 223 } 224 } 225 226 } 227 228 private String getElementTextValue(Element element) 229 { 230 String text =""; 231 232 // Find the node child 233 NodeList children_nodelist = element.getChildNodes(); 234 for (int i = 0; i < children_nodelist.getLength(); i++) { 235 Node child_node = children_nodelist.item(i); 236 if (child_node.getNodeType() == Node.TEXT_NODE) { 237 text +=child_node.getNodeValue(); 238 } 239 } 240 241 return text; 242 } 243 244 245 private void setMappingVariable(Document style_doc){ 246 Node child = style_doc.getDocumentElement().getFirstChild(); 247 while(child != null) { 248 String name = child.getNodeName(); 249 if (name.equals("xsl:variable")) { 250 Element variable_element = (Element)child; 251 if ( variable_element.getAttribute("name").trim().equals("mapping")){ 252 variable_element.setAttribute("select","document('"+mapping_file+"')/Mapping"); 156 253 } 157 catch(Exception e){ 158 e.printStackTrace(); 159 return false; 160 } 161 162 return true; 163 } 164 165 private void calculateRecordsLength(Document output_doc){ 166 NodeList records = output_doc.getDocumentElement().getElementsByTagName(RECORD_ELEMENT); 167 168 for(int i=0;i<records.getLength();i++){ 169 Element record = (Element)records.item(i); 170 calculateRecordLength(record); 171 } 172 } 173 174 private void calculateRecordLength(Element record){ 175 int total_length =0; 176 NodeList controlfileds = record.getElementsByTagName(CONTROLFIELD_ELEMENT); 177 for(int i=0;i<controlfileds.getLength();i++){ 178 Element controlfiled = (Element)controlfileds.item(i); 179 total_length +=getElementTextValue(controlfiled).length(); 180 } 181 182 NodeList subfileds = record.getElementsByTagName(SUBFIELD_ELEMENT); 183 for(int i=0;i<subfileds.getLength();i++){ 184 Element subfiled = (Element)subfileds.item(i); 185 total_length +=getElementTextValue(subfiled).length(); 186 } 187 188 String record_length = total_length+""; 189 //fill in a extra digit as record length needs to be five characters long 190 if (total_length < 10000){ 191 record_length = "0"+record_length; 192 if (total_length < 1000){ 193 record_length = "0"+record_length; 194 } 195 if (total_length < 100){ 196 record_length = "0"+record_length; 197 } 198 if (total_length < 10){ 199 record_length = "0"+record_length; 200 } 201 202 } 203 204 NodeList leaders = record.getElementsByTagName(LEADER_ELEMENT); 205 206 //only one leader element 207 if (leaders.getLength() >0){ 208 Element leader_element = (Element)leaders.item(0); 209 removeFirstTextNode(leader_element); 210 leader_element.insertBefore(leader_element.getOwnerDocument().createTextNode(record_length),leader_element.getFirstChild()); 211 } 212 213 } 214 215 private void removeFirstTextNode(Element element){ 216 //remove the first text node 217 NodeList children_nodelist = element.getChildNodes(); 218 for (int i = 0; i < children_nodelist.getLength(); i++) { 219 Node child_node = children_nodelist.item(i); 220 if (child_node.getNodeType() == Node.TEXT_NODE) { 221 element.removeChild(child_node); 222 return; 223 } 224 } 225 226 } 227 228 private String getElementTextValue(Element element) 229 { 230 String text =""; 231 232 // Find the node child 233 NodeList children_nodelist = element.getChildNodes(); 234 for (int i = 0; i < children_nodelist.getLength(); i++) { 235 Node child_node = children_nodelist.item(i); 236 if (child_node.getNodeType() == Node.TEXT_NODE) { 237 text +=child_node.getNodeValue(); 238 } 239 } 240 241 return text; 242 } 243 244 245 private void setMappingVariable(Document style_doc){ 246 Node child = style_doc.getDocumentElement().getFirstChild(); 247 while(child != null) { 248 String name = child.getNodeName(); 249 if (name.equals("xsl:variable")) { 250 Element variable_element = (Element)child; 251 if ( variable_element.getAttribute("name").trim().equals("mapping")){ 252 variable_element.setAttribute("select","document('"+mapping_file+"')/Mapping"); 253 } 254 } 255 child = child.getNextSibling(); 256 } 257 254 } 255 child = child.getNextSibling(); 256 } 257 258 } 259 260 261 private void translate(String xml_file, String xsl_file, String output_file)throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException, IOException{ 262 263 TransformerFactory tFactory = TransformerFactory.newInstance(); 264 Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file)); 265 266 OutputStreamWriter output = null; 267 if (output_file.equals("")) { 268 output = new OutputStreamWriter(System.out, "UTF-8"); 269 } 270 else{ 271 output = new OutputStreamWriter(new FileOutputStream(output_file), "UTF-8"); 272 } 273 274 transformer.transform(new StreamSource(new File(xml_file)),new StreamResult(output)); 275 276 } 277 278 static public String replaceAll(String source_string, String match_regexp, String replace_string) 279 { 280 return source_string.replaceAll(match_regexp, replace_string); 281 } 282 283 284 public static void main(String[] args) 285 { 286 287 String xml_file=""; 288 String xsl_file=""; 289 String mapping_file=""; 290 String output_file=""; 291 292 // Checking Arguments 293 if(args.length < 1) 294 { 295 printUsage(); 296 } 297 298 for (int i=0;i<args.length;i++){ 299 if (args[i].equals("-m") && i+1 < args.length && !args[i+1].startsWith("-")){ 300 mapping_file = args[++i]; 301 checkFile(mapping_file.replaceAll("file:///","")); 302 } 303 else if (args[i].equals("-x") && i+1 < args.length && !args[i+1].startsWith("-")){ 304 xml_file = args[++i]; 305 checkFile(xml_file.replaceAll("file:///","")); 306 } 307 else if(args[i].equals("-t") && i+1 < args.length && !args[i+1].startsWith("-")){ 308 xsl_file = args[++i]; 309 checkFile( xsl_file.replaceAll("file:///","")); 310 } 311 else if(args[i].equals("-o") && i+1 < args.length && !args[i+1].startsWith("-")){ 312 output_file = args[++i]; 313 314 } 315 else if(args[i].equals("-h")){ 316 printUsage(); 317 } 318 else{ 319 printUsage(); 320 } 321 322 } 323 324 325 ApplyXSLT core = null; 326 327 if (xml_file.equals("") && !xsl_file.equals("")){//read from pipe line 328 if (mapping_file.equals("")){ 329 core = new ApplyXSLT(xsl_file); 330 } 331 else{ 332 core = new ApplyXSLT(xsl_file,mapping_file); 333 } 334 335 if (core != null){ 336 core.process(); 337 } 338 else{ 339 printUsage(); 340 } 341 } 342 else if(!xml_file.equals("") && !xsl_file.equals("")){ 343 core = new ApplyXSLT(); 344 try { 345 core.translate(xml_file,xsl_file,output_file); 346 } 347 catch(Exception e){e.printStackTrace();} 258 348 } 259 260 261 private void translate(String xml_file, String xsl_file, String output_file)throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException, IOException{ 262 263 TransformerFactory tFactory = TransformerFactory.newInstance(); 264 Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file)); 265 266 OutputStreamWriter output = null; 267 if (output_file.equals("")) { 268 output = new OutputStreamWriter(System.out, "UTF-8"); 269 } 270 else{ 271 output = new OutputStreamWriter(new FileOutputStream(output_file), "UTF-8"); 272 } 273 274 transformer.transform(new StreamSource(new File(xml_file)),new StreamResult(output)); 275 276 } 277 278 static public String replaceAll(String source_string, String match_regexp, String replace_string) 279 { 280 return source_string.replaceAll(match_regexp, replace_string); 281 } 282 283 284 public static void main(String[] args) 285 { 286 287 String xml_file=""; 288 String xsl_file=""; 289 String mapping_file=""; 290 String output_file=""; 291 292 // Checking Arguments 293 if(args.length < 1) 294 { 295 printUsage(); 296 } 297 298 for (int i=0;i<args.length;i++){ 299 if (args[i].equals("-m") && i+1 < args.length && !args[i+1].startsWith("-")){ 300 mapping_file = args[++i]; 301 checkFile(mapping_file.replaceAll("file:///","")); 302 } 303 else if (args[i].equals("-x") && i+1 < args.length && !args[i+1].startsWith("-")){ 304 xml_file = args[++i]; 305 checkFile(xml_file.replaceAll("file:///","")); 306 } 307 else if(args[i].equals("-t") && i+1 < args.length && !args[i+1].startsWith("-")){ 308 xsl_file = args[++i]; 309 checkFile( xsl_file.replaceAll("file:///","")); 310 } 311 else if(args[i].equals("-o") && i+1 < args.length && !args[i+1].startsWith("-")){ 312 output_file = args[++i]; 313 314 } 315 else if(args[i].equals("-h")){ 316 printUsage(); 317 } 318 else{ 319 printUsage(); 320 } 321 322 } 323 324 325 ApplyXSLT core = null; 326 327 if (xml_file.equals("") && !xsl_file.equals("")){//read from pipe line 328 if (mapping_file.equals("")){ 329 core = new ApplyXSLT(xsl_file); 330 } 331 else{ 332 core = new ApplyXSLT(xsl_file,mapping_file); 333 } 334 335 if (core != null){ 336 core.process(); 337 } 338 else{ 339 printUsage(); 340 } 341 } 342 else if(!xml_file.equals("") && !xsl_file.equals("")){ 343 core = new ApplyXSLT(); 344 try { 345 core.translate(xml_file,xsl_file,output_file); 346 } 347 catch(Exception e){e.printStackTrace();} 348 } 349 else{ 350 printUsage(); 351 } 352 353 } 354 355 private static void checkFile(String filename){ 356 File file = new File(filename); 357 if (!file.exists()){ 358 System.out.println("Error: "+filename+" doesn't exist!"); 359 System.exit(-1); 360 } 361 } 349 else{ 350 printUsage(); 351 } 352 353 } 354 355 private static void checkFile(String filename){ 356 File file = new File(filename); 357 if (!file.exists()){ 358 System.out.println("Error: "+filename+" doesn't exist!"); 359 System.exit(-1); 360 } 361 } 362 362 363 364 365 366 367 368 369 370 363 private static void printUsage(){ 364 System.out.println("Usage:ApplyXSLT -x File -t File [-m File] [-o File]"); 365 System.out.println("\t-x spacifies the xml file (Note: optional for piped xml data)"); 366 System.out.println("\t-t spacifies the xsl file"); 367 System.out.println("\t-m specifies the mapping file (for MARCXMLPlugout.pm only)"); 368 System.out.println("\t-o specifies the output file name (output to screen if this option is absent)"); 369 System.exit(-1); 370 } 371 371 } 372 372
Note:
See TracChangeset
for help on using the changeset viewer.