source: gsdl/trunk/build-src/src/java/org/nzdl/gsdl/ApplyXSLT.java@ 20875

Last change on this file since 20875 was 20875, checked in by kjdon, 15 years ago

indented the file before I start modifying it

  • Property svn:keywords set to Author Date Id Revision
File size: 10.7 KB
RevLine 
[12395]1package org.nzdl.gsdl;
2
3import java.io.*;
4
5import javax.xml.transform.Transformer;
6import javax.xml.transform.TransformerConfigurationException;
7import javax.xml.transform.TransformerException;
8import javax.xml.transform.TransformerFactory;
9import javax.xml.transform.stream.StreamResult;
10import javax.xml.transform.stream.StreamSource;
11
[12594]12import javax.xml.parsers.*;
13import javax.xml.transform.dom.*;
14import org.w3c.dom.*;
15
16
17
[12395]18/**
19 * Use the TraX interface to perform a transformation in the simplest manner possible
20 * (3 statements).
21 */
22public class ApplyXSLT
23{
24
[20875]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?>");
[12395]28
[20875]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";
[12594]33
[20875]34 private final int BEFORE_READING = 0;
35 private final int IS_READING = 1;
36 private String xsl_file;
37 private String mapping_file;
[12395]38
[20875]39 public ApplyXSLT(){}
[13470]40
[20875]41 public ApplyXSLT(String xsl_file)
42 {
43 this.xsl_file = xsl_file;
44 }
[12395]45
[20875]46 public ApplyXSLT(String xsl_file,String mapping_file){
47 this.xsl_file = xsl_file;
48 this.mapping_file = mapping_file;
49 }
[12594]50
[20875]51 private boolean process()
52 {
53 try{
[13223]54
[20875]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);
[12395]58
[20875]59 int system_status = BEFORE_READING;
60 String a_doc = new String();
61 String output_file = new String();
[12395]62
[13223]63
[20875]64 while (br.ready()) {
[12594]65
[20875]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 }
[12594]80
[20875]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 }
[12594]91
[20875]92 if (!result){
93 System.err.println("Translation Failed!!");
94 return false;
95 }
[12594]96
[20875]97 system_status = BEFORE_READING;
[12594]98
[20875]99 }
100 else{
101 a_doc = a_doc + this_line + "\n";
[12594]102
[20875]103 }
104 }
105 else{
106 System.err.println ("Undefined system status in ApplyXSLT.java main().");
107 System.exit(-1);
108 }
[12594]109
[20875]110 }
111 }catch (Exception e)
112 {
113 System.err.println("Receiving piped data error!" + e.toString());
114 }
[12395]115
[20875]116 return false;
117 }
[12594]118
119
[20875]120 private boolean translateXML(String full_doc, String output_file)
121 throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException
122 {
[12594]123
[20875]124 StringReader str = new StringReader(full_doc) ;
[12395]125
[20875]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 }
[12395]131
[20875]132 private boolean translateXMLWithMapping(String full_doc, String output_file)
133 throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException
134 {
135 StringReader str = new StringReader(full_doc) ;
[12594]136
[20875]137 try{
138 TransformerFactory tFactory = TransformerFactory.newInstance();
139 Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file));
[12594]140
[20875]141 Document mapping_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(mapping_file);
142 Element mapping =mapping_doc.getDocumentElement();
[12594]143
[20875]144 transformer.setParameter("mapping",mapping);
[12594]145
[20875]146 Document output_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
[12594]147
[20875]148 transformer.transform(new StreamSource(str), new DOMResult(output_doc));
[12594]149
[20875]150 calculateRecordsLength(output_doc);
[12594]151
[20875]152 transformer = tFactory.newTransformer();
[12594]153
[20875]154 transformer.transform(new DOMSource(output_doc), new StreamResult(new FileOutputStream(output_file)));
[12594]155
156 }
[20875]157 catch(Exception e){
158 e.printStackTrace();
159 return false;
160 }
[12594]161
[20875]162 return true;
163 }
[12594]164
[20875]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);
[12594]171 }
[20875]172 }
[12594]173
[20875]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 }
[12594]181
[20875]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 }
[12594]187
[20875]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 }
[12594]201
[20875]202 }
[12594]203
[20875]204 NodeList leaders = record.getElementsByTagName(LEADER_ELEMENT);
[12594]205
[20875]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());
[12594]211 }
212
[20875]213 }
[12594]214
[20875]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 }
[12594]224 }
225
[20875]226 }
[12594]227
[20875]228 private String getElementTextValue(Element element)
229 {
230 String text ="";
[12594]231
[20875]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 }
[12594]239 }
[20875]240
241 return text;
242 }
[12594]243
244
[20875]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");
[12594]253 }
[20875]254 }
255 child = child.getNextSibling();
256 }
[12594]257
[20875]258 }
[12594]259
260
[20875]261 private void translate(String xml_file, String xsl_file, String output_file)throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException, IOException{
[13473]262
[20875]263 TransformerFactory tFactory = TransformerFactory.newInstance();
264 Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file));
[13473]265
[20875]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 }
[13473]273
[20875]274 transformer.transform(new StreamSource(new File(xml_file)),new StreamResult(output));
[13473]275
[20875]276 }
[13470]277
[20875]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 }
[13473]282
283
[20875]284 public static void main(String[] args)
285 {
[13470]286
[20875]287 String xml_file="";
288 String xsl_file="";
289 String mapping_file="";
290 String output_file="";
[13470]291
[20875]292 // Checking Arguments
293 if(args.length < 1)
294 {
295 printUsage();
296 }
[13470]297
[20875]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];
[13470]313
[20875]314 }
315 else if(args[i].equals("-h")){
316 printUsage();
317 }
318 else{
319 printUsage();
320 }
[13470]321
[20875]322 }
[12594]323
[13470]324
[20875]325 ApplyXSLT core = null;
[13470]326
[20875]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 }
[13470]334
[20875]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 }
[13470]352
[20875]353 }
[13470]354
[20875]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);
[13470]360 }
[20875]361 }
[13470]362
[20875]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 }
[12395]371}
372
373
Note: See TracBrowser for help on using the repository browser.