Changeset 12594
- Timestamp:
- 2006-08-30T10:20:19+12:00 (18 years ago)
- Location:
- trunk/gsdl
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl/src/java/org/nzdl/gsdl/ApplyXSLT.java
r12395 r12594 10 10 import javax.xml.transform.stream.StreamSource; 11 11 12 import javax.xml.parsers.*; 13 import javax.xml.transform.dom.*; 14 import org.w3c.dom.*; 15 16 17 12 18 /** 13 19 * Use the TraX interface to perform a transformation in the simplest manner possible … … 21 27 public static final String INPUT_END = new String ("<?Done?>"); 22 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 23 34 private final int BEFORE_READING = 0; 24 35 private final int IS_READING = 1; 25 36 private String xsl_file; 37 private String mapping_file; 26 38 27 39 public ApplyXSLT(String xsl_file) 28 40 { 29 41 this.xsl_file = xsl_file; 42 } 43 44 public ApplyXSLT(String xsl_file,String mapping_file){ 45 this.xsl_file = xsl_file; 46 this.mapping_file = mapping_file; 30 47 } 31 48 … … 41 58 42 59 try{ 43 while (br.ready()) 44 { 45 46 String this_line = br.readLine(); 47 48 49 if(system_status == BEFORE_READING) 50 { 51 52 if(this_line.compareTo(DOC_START) == 0) 53 { 54 output_file = br.readLine(); // read the next line as the output file name 55 system_status = IS_READING; 56 a_doc = ""; 57 } 58 else if(this_line.compareTo(INPUT_END) == 0) 59 { 60 return true; 61 } 62 else 63 { 64 65 System.err.println("Undefined process status:" + this_line); 66 system_status = BEFORE_READING; 67 } 68 60 while (br.ready()) { 61 62 String this_line = br.readLine(); 63 if(system_status == BEFORE_READING){ 64 if(this_line.compareTo(DOC_START) == 0){ 65 output_file = br.readLine(); // read the next line as the output file name 66 system_status = IS_READING; 67 a_doc = ""; 68 } 69 else if(this_line.compareTo(INPUT_END) == 0){ 70 return true; 71 } 72 else{ 73 System.err.println("Undefined process status:" + this_line); 74 system_status = BEFORE_READING; 75 } 76 77 } 78 else if(system_status == IS_READING){ 79 if(this_line.compareTo(DOC_END) == 0){ 80 boolean result = false; 81 if (mapping_file !=null && !mapping_file.equals("")){ 82 result = translateXMLWithMapping(a_doc,output_file); 69 83 } 70 else if(system_status == IS_READING) 71 { 72 if(this_line.compareTo(DOC_END) == 0) 73 { 74 75 if(translateXML(a_doc,xsl_file,output_file) == false) 76 { 77 System.err.println("Translation Failed!!"); 78 return false; 79 } 80 system_status = BEFORE_READING; 81 82 } 83 else 84 { 85 a_doc = a_doc + this_line + "\n"; 86 87 } 84 else{ 85 result = translateXML(a_doc,output_file); 88 86 } 89 else 90 { 91 92 System.err.println ("Undefined system status in ApplyXSLT.java main()."); 93 System.exit(-1); 87 88 if (!result){ 89 System.err.println("Translation Failed!!"); 90 return false; 94 91 } 95 92 93 system_status = BEFORE_READING; 94 95 } 96 else{ 97 a_doc = a_doc + this_line + "\n"; 98 99 } 96 100 } 101 else{ 102 System.err.println ("Undefined system status in ApplyXSLT.java main()."); 103 System.exit(-1); 104 } 105 106 } 97 107 }catch (Exception e) 98 108 { … … 102 112 return false; 103 113 } 104 105 106 private boolean translateXML(String full_doc, String xsl_file, Stringoutput_file)114 115 116 private boolean translateXML(String full_doc, String output_file) 107 117 throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException 108 118 { 109 119 110 120 StringReader str = new StringReader(full_doc) ; 111 121 112 122 TransformerFactory tFactory = TransformerFactory.newInstance(); 113 123 Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file)); 114 124 transformer.transform(new StreamSource(str), new StreamResult(new FileOutputStream(output_file))); … … 116 126 } 117 127 128 private boolean translateXMLWithMapping(String full_doc, String output_file) 129 throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException 130 { 131 StringReader str = new StringReader(full_doc) ; 132 133 try{ 134 TransformerFactory tFactory = TransformerFactory.newInstance(); 135 Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file)); 136 137 Document mapping_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(mapping_file); 138 Element mapping =mapping_doc.getDocumentElement(); 139 140 transformer.setParameter("mapping",mapping); 141 142 Document output_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 143 144 transformer.transform(new StreamSource(str), new DOMResult(output_doc)); 145 146 calculateRecordsLength(output_doc); 147 148 transformer = tFactory.newTransformer(); 149 150 transformer.transform(new DOMSource(output_doc), new StreamResult(new FileOutputStream(output_file))); 151 152 } 153 catch(Exception e){ 154 e.printStackTrace(); 155 return false; 156 } 157 158 return true; 159 } 160 161 private void calculateRecordsLength(Document output_doc){ 162 NodeList records = output_doc.getDocumentElement().getElementsByTagName(RECORD_ELEMENT); 163 164 for(int i=0;i<records.getLength();i++){ 165 Element record = (Element)records.item(i); 166 calculateRecordLength(record); 167 } 168 } 169 170 private void calculateRecordLength(Element record){ 171 int total_length =0; 172 NodeList controlfileds = record.getElementsByTagName(CONTROLFIELD_ELEMENT); 173 for(int i=0;i<controlfileds.getLength();i++){ 174 Element controlfiled = (Element)controlfileds.item(i); 175 total_length +=getElementTextValue(controlfiled).length(); 176 } 177 178 NodeList subfileds = record.getElementsByTagName(SUBFIELD_ELEMENT); 179 for(int i=0;i<subfileds.getLength();i++){ 180 Element subfiled = (Element)subfileds.item(i); 181 total_length +=getElementTextValue(subfiled).length(); 182 } 183 184 String record_length = total_length+""; 185 //fill in a extra digit as record length needs to be five characters long 186 if (total_length < 10000){ 187 record_length = "0"+record_length; 188 if (total_length < 1000){ 189 record_length = "0"+record_length; 190 } 191 if (total_length < 100){ 192 record_length = "0"+record_length; 193 } 194 if (total_length < 10){ 195 record_length = "0"+record_length; 196 } 197 198 } 199 200 NodeList leaders = record.getElementsByTagName(LEADER_ELEMENT); 201 202 //only one leader element 203 if (leaders.getLength() >0){ 204 Element leader_element = (Element)leaders.item(0); 205 removeFirstTextNode(leader_element); 206 leader_element.insertBefore(leader_element.getOwnerDocument().createTextNode(record_length),leader_element.getFirstChild()); 207 } 208 209 } 210 211 private void removeFirstTextNode(Element element){ 212 //remove the first text node 213 NodeList children_nodelist = element.getChildNodes(); 214 for (int i = 0; i < children_nodelist.getLength(); i++) { 215 Node child_node = children_nodelist.item(i); 216 if (child_node.getNodeType() == Node.TEXT_NODE) { 217 element.removeChild(child_node); 218 return; 219 } 220 } 221 222 } 223 224 private String getElementTextValue(Element element) 225 { 226 String text =""; 227 228 // Find the node child 229 NodeList children_nodelist = element.getChildNodes(); 230 for (int i = 0; i < children_nodelist.getLength(); i++) { 231 Node child_node = children_nodelist.item(i); 232 if (child_node.getNodeType() == Node.TEXT_NODE) { 233 text +=child_node.getNodeValue(); 234 } 235 } 236 237 return text; 238 } 239 240 241 private void setMappingVariable(Document style_doc){ 242 Node child = style_doc.getDocumentElement().getFirstChild(); 243 while(child != null) { 244 String name = child.getNodeName(); 245 if (name.equals("xsl:variable")) { 246 Element variable_element = (Element)child; 247 if ( variable_element.getAttribute("name").trim().equals("mapping")){ 248 variable_element.setAttribute("select","document('"+mapping_file+"')/Mapping"); 249 } 250 } 251 child = child.getNextSibling(); 252 } 253 254 } 255 256 118 257 public static void main(String[] args) 119 258 { 120 259 // Checking Arguments 121 if(args.length !=1)260 if(args.length < 1) 122 261 { 123 262 System.err.println("Must provide a XSL file !"); 124 263 System.exit(-1); 125 264 } 126 ApplyXSLT core = new ApplyXSLT(args[0]); 265 ApplyXSLT core = null; 266 267 if (args.length == 1){ 268 core = new ApplyXSLT(args[0]); 269 } 270 else if(args.length == 2){ 271 core = new ApplyXSLT(args[0],args[1]); 272 } 127 273 128 274 if(core.process())
Note:
See TracChangeset
for help on using the changeset viewer.