Dom4j
Dom4j
http://baike.baidu.com/link?url=2XOnr06saKUd-9By1GyPxIolXMQhf_C-CnMFll_yhtR4m00i27zphbkI5-dGpwEVjuKbkdosTYmNMXc4OA31ba
1
1 package ec.export.checksheet; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.util.ArrayList; 7 import java.util.HashMap; 8 import java.util.List; 9 import java.util.Map; 10 11 import net.sf.jasperreports.engine.JREmptyDataSource; 12 import net.sf.jasperreports.engine.JRException; 13 import net.sf.jasperreports.engine.JRPrintPage; 14 import net.sf.jasperreports.engine.JasperFillManager; 15 import net.sf.jasperreports.engine.JasperPrint; 16 import net.sf.jasperreports.engine.JasperReport; 17 import net.sf.jasperreports.engine.util.JRLoader; 18 19 import org.dom4j.Document; 20 import org.dom4j.DocumentException; 21 import org.dom4j.io.SAXReader; 22 23 import ec.export.ECReportHelper; 24 import ec.utils.Dom4jXml; 25 26 public class AddCheckSheet { 27 28 public Dom4jXml dj = new Dom4jXml(); 29 String TEMPLATES_PATH = "ec/export/resources/"; 30 31 public JasperPrint FormTotalPage(JasperPrint jp,org.w3c.dom.Document document,Map mSelectCheckSheet,Map mCheckSheetAutoFill){ 32 List l=new ArrayList(); 33 try { 34 l=CheckSheetReadXml(document,mCheckSheetAutoFill); 35 } catch (Exception e1) { 36 // TODO Auto-generated catch block 37 e1.printStackTrace(); 38 } 39 40 41 // String jasperpatha=getClass().getResource("")+"checksheeta.jasper"; 42 // jasperpatha=jasperpatha.replaceAll("file:/", ""); 43 // String jasperpathb=getClass().getResource("")+"checksheetb.jasper"; 44 // jasperpathb=jasperpathb.replaceAll("file:/", ""); 45 46 47 //ECReportHelper.locateResource(""); 48 49 50 51 String jasperpatha=TEMPLATES_PATH+"checksheeta.jasper"; 52 53 String jasperpathb=TEMPLATES_PATH+"checksheetb.jasper"; 54 55 56 for(int i=0;i<l.size();i++){ 57 58 // String ec.export.ECJobReporter.buildStylesFile(); 59 60 61 // ========for checksheeta.jasper======= 62 JasperPrint jasperprint = new JasperPrint(); 63 try { 64 //JasperPrint jasperPrint = JasperFillManager.fillReport(getClass().getResourceAsStream("periodo.jasper"), parameters, con); 65 //jasperprint = JasperFillManager.fillReport(JRLoader.getResourceInputStream(jasperpathb), (Map)l.get(i)); 66 jasperprint = JasperFillManager.fillReport(JRLoader.getResourceInputStream(jasperpatha), (Map)l.get(i), new JREmptyDataSource()); 67 //jasperprint = JasperFillManager.fillReport(ClassLoader.getSystemResourceAsStream(jasperpathb), (Map)l.get(i), new JREmptyDataSource()); 68 //jasperprint = JasperFillManager.fillReport(getClass().getResourceAsStream(jasperpathb), (Map)l.get(i), new JREmptyDataSource()); 69 //jasperprint = JasperFillManager.fillReport(jasperpathb, (Map)l.get(i), new JREmptyDataSource()); 70 71 } catch (Exception e) { 72 e.printStackTrace(); 73 } 74 //System.out.println(" before jp: "+jp.getPages().size()); 75 multipageLinking(jp,jasperprint); 76 //System.out.println(" after jp: "+jp.getPages().size()); 77 78 79 // =======for checksheetb.jasper======= 80 jasperprint = new JasperPrint(); 81 try { 82 //jasperprint = JasperFillManager.fillReport(JRLoader.getResourceInputStream(jasperpathb), (Map)l.get(i)); 83 jasperprint = JasperFillManager.fillReport(JRLoader.getResourceInputStream(jasperpathb), (Map)l.get(i), new JREmptyDataSource()); 84 //jasperprint = JasperFillManager.fillReport(ClassLoader.getSystemResourceAsStream(jasperpathb), (Map)l.get(i), new JREmptyDataSource()); 85 //jasperprint = JasperFillManager.fillReport(getClass().getResourceAsStream(jasperpathb), (Map)l.get(i), new JREmptyDataSource()); 86 //jasperprint = JasperFillManager.fillReport(jasperpathb, (Map)l.get(i), new JREmptyDataSource()); 87 } catch (Exception e) { 88 e.printStackTrace(); 89 } 90 //System.out.println(" before jp: "+jp.getPages().size()); 91 multipageLinking(jp,jasperprint); 92 //System.out.println(" after jp: "+jp.getPages().size()); 93 94 95 96 97 // JasperPrint result = JasperFillManager.fillReport( 98 // ECReportHelper.locateResource(JOB_TEMPLATE), 99 // params 100 // ); 101 /* 102 // ========for checksheeta.jasper======= 103 JasperPrint jasperprint = new JasperPrint(); 104 try { 105 jasperprint = JasperFillManager.fillReport(jasperpatha, (Map)l.get(i), new JREmptyDataSource()); 106 } catch (Exception e) { 107 e.printStackTrace(); 108 } 109 //System.out.println(" before jp: "+jp.getPages().size()); 110 multipageLinking(jp,jasperprint); 111 //System.out.println(" after jp: "+jp.getPages().size()); 112 113 // =======for checksheetb.jasper======= 114 jasperprint = new JasperPrint(); 115 try { 116 jasperprint = JasperFillManager.fillReport(jasperpathb, (Map)l.get(i), new JREmptyDataSource()); 117 } catch (Exception e) { 118 e.printStackTrace(); 119 } 120 //System.out.println(" before jp: "+jp.getPages().size()); 121 multipageLinking(jp,jasperprint); 122 //System.out.println(" after jp: "+jp.getPages().size()); 123 */ 124 } 125 return jp; 126 } 127 128 private JasperPrint multipageLinking(JasperPrint page1, JasperPrint page2) { 129 List pages = page2.getPages(); 130 for (int count = 0; count <pages.size(); count++) { 131 page1.addPage((JRPrintPage) pages.get(count)); 132 } 133 return page1; 134 } 135 136 137 // for checkSheet Print 138 public List CheckSheetReadXml(org.w3c.dom.Document doc,Map mCheckSheetAutoFill) throws Exception{ 139 Document document=dj.parse(doc); 140 List pl=new ArrayList(); 141 142 //count product num 143 List list = document.selectNodes("/report/job/product"); 144 int num = list.size(); 145 146 for(int i=1;i<=num;i++) 147 { 148 Map mproduct=new HashMap(); 149 //0 select_date 150 String xpathstr="/report/job/@select_date"; 151 if(!dj.getContentString(document,xpathstr).equals("")){ 152 mproduct.put("cs_Date", dj.getContentString(document,xpathstr)); 153 } 154 155 xpathstr="/report/job/createdDt"; 156 if(!dj.getContentString(document,xpathstr).equals("")){ 157 mproduct.put("cs_Date", dj.getContentString(document,xpathstr)); 158 } 159 160 //model 1 AT3ARF1R AT5CRF4R 161 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Screw Compressor Air Cooled Package Chiller']/row/field[name='Model #']/value"; 162 String s1=dj.getContentString(document,xpathstr); 163 //for acds in spec 164 if(s1.equals("")){ 165 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Scroll Compressor Air Cooled Package Chiller']/row/field[name='Model #']/value"; 166 s1=dj.getContentString(document,xpathstr); 167 } 168 169 170 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Screw Compressor Air Cooled Package Chiller']/row/field[name='Component code']/value"; 171 String tmep=dj.getContentString(document,xpathstr); 172 if(tmep==""){}else{ 173 tmep=tmep.substring(2); 174 String tempbefore=tmep.substring(3); 175 String tempafter=tmep.substring(0,3); 176 tmep=tempbefore+tempafter; 177 } 178 mproduct.put("cs_Model", s1+tmep); 179 180 xpathstr="/report/job/product["+i+"]/qty"; 181 mproduct.put("cs_Qty", dj.getContentString(document,xpathstr)); 182 183 184 185 //evap 2 186 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator']/col1/field[contains(name,'Entering fluid temp')]/value"; 187 mproduct.put("cs_Evap_Entering_fluid_temp", dj.getContentString(document,xpathstr)); 188 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator']/col1/field[contains(name,'Fluid flow rate')]/value"; 189 mproduct.put("cs_Evap_Fluid_flow_rate", dj.getContentString(document,xpathstr)); 190 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator']/col2/field[contains(name,'Fouling factor')]/value"; 191 mproduct.put("cs_Evap_Fouling_factor", dj.getContentString(document,xpathstr)); 192 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator']/col2/field[contains(name,'Leaving fluid temp')]/value"; 193 mproduct.put("cs_Evap_Leaving_fluid_temp", dj.getContentString(document,xpathstr)); 194 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator']/col1/field[contains(name,'Fluid')]/value"; 195 196 String fluidstr=dj.getContentString(document,xpathstr); 197 if(fluidstr!=""&& !fluidstr.equals("")&& fluidstr.contains("%")){ 198 fluidstr=fluidstr.substring(fluidstr.indexOf("%")+1)+" / "+fluidstr.substring(0, fluidstr.indexOf("%")+1); 199 } 200 mproduct.put("cs_Evap_Fluid",fluidstr ); 201 202 //mproduct.put("cs_Evap_Fluid", dj.getContentString(document,xpathstr)); 203 204 205 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator']/col1/field[contains(name,'Number of passes')]/value"; 206 mproduct.put("cs_Evap_Number_of_passes", dj.getContentString(document,xpathstr)); 207 208 //cond 3 209 210 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Condenser']/col1/field[contains(name,'Design ambient')]/value"; 211 mproduct.put("cs_Cond_Design_ambient", dj.getContentString(document,xpathstr)); 212 213 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Condenser']/col2/field[contains(name,'Entering fluid temp')]/value"; 214 mproduct.put("cs_Cond_Entering_fluid_temp", dj.getContentString(document,xpathstr)); 215 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Condenser']/col2/field[contains(name,'Fluid flow rate')]/value"; 216 mproduct.put("cs_Cond_Fluid_flow_rate", dj.getContentString(document,xpathstr)); 217 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Condenser']/col2/field[contains(name,'Fouling factor')]/value"; 218 mproduct.put("cs_Cond_Fouling_factor", dj.getContentString(document,xpathstr)); 219 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Condenser']/col2/field[contains(name,'Leaving fluid temp')]/value"; 220 mproduct.put("cs_Cond_Leaving_fluid_temp", dj.getContentString(document,xpathstr)); 221 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Condenser']/col2/field[contains(name,'Fluid')]/value"; 222 mproduct.put("cs_Cond_Fluid", dj.getContentString(document,xpathstr)); 223 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Condenser']/col2/field[contains(name,'Number of passes')]/value"; 224 mproduct.put("cs_Cond_Number_of_passes", dj.getContentString(document,xpathstr)); 225 226 //4 Cooling Conditions of service 227 //<name>Capacity (Tons)</name> 228 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Conditions of service']/col1/field[contains(name,'Capacity')]/value"; 229 mproduct.put("cs_Capacity", dj.getContentString(document,xpathstr)); 230 //<name>Energy efficiency (kW/Ton)</name> 231 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Conditions of service']/col2/field[contains(name,'Compressor power')]/value"; 232 mproduct.put("cs_Energy_efficiency", dj.getContentString(document,xpathstr)); 233 234 //5<name>Unit power supply</name> 235 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Electrical characteristics']/col1/field[contains(name,'Unit power supply')]/value"; 236 mproduct.put("cs_Unit_power_supply", dj.getContentString(document,xpathstr)); 237 238 //6<name>Refrigerant</name>//<value>R134a</value> 239 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Physical Specifications']/col2/field[contains(name,'Refrigerant')]/value"; 240 mproduct.put("cs_Refrigerant", dj.getContentString(document,xpathstr)); 241 242 //add pictureleftheader 243 String pic_left_header=TEMPLATES_PATH+"leftheader-cs.JPG"; 244 //pic_left_header=pic_left_header.replaceAll("file:/", ""); 245 mproduct.put("cs_pic_left_header", pic_left_header); 246 247 mproduct.putAll((Map) mCheckSheetAutoFill.get(i)); 248 pl.add(mproduct); 249 } 250 return pl; 251 } 252 253 // public static void main(String[] args) throws Exception { 254 // Dom4jXml dj = new Dom4jXml(); 255 // AddCheckSheet acs=new AddCheckSheet(); 256 // SAXReader saxReader = new SAXReader(); 257 // Document document; 258 // try { 259 // document = saxReader.read(new File("D:/Task/draw_cs3/20141218/wcfx36_more.xml")); 260 // 261 // acs.CheckSheetReadXml(dj.parse(document)); 262 // } catch (DocumentException e) { 263 // // TODO Auto-generated catch block 264 // e.printStackTrace(); 265 // } 266 // } 267 }
2
1 package ec.utils; 2 3 import java.io.File; 4 import java.util.ArrayList; 5 import java.util.HashMap; 6 import java.util.Iterator; 7 import java.util.List; 8 import java.util.Map; 9 10 import org.dom4j.Attribute; 11 import org.dom4j.Document; 12 import org.dom4j.DocumentException; 13 import org.dom4j.Element; 14 import org.dom4j.io.SAXReader; 15 16 public class Dom4jXml { 17 18 static boolean debugf=true; 19 20 21 public static void main(String[] args) throws Exception { 22 Dom4jXml dj = new Dom4jXml(); 23 SAXReader saxReader = new SAXReader(); 24 Document document; 25 try { 26 document = saxReader.read(new File("D:/Task/draw_cs3/20141218/wcfx36_more.xml")); 27 28 dj.ReadXml(parse(document)); 29 } catch (DocumentException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } 33 } 34 35 //for checkSheet Print 36 public List ReadXml(org.w3c.dom.Document doc) throws Exception{ 37 Document document=parse(doc); 38 List pl=new ArrayList(); 39 40 //count product num 41 List list = document.selectNodes("/report/job/product"); 42 int num = list.size(); 43 44 for(int i=1;i<=num;i++) 45 { 46 Map mproduct=new HashMap(); 47 //0 select_date 48 String xpathstr="/report/job/@select_date"; 49 mproduct.put("cs_Date", getContentString(document,xpathstr)); 50 //model 1 AT3ARF1R AT5CRF4R 51 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Screw Compressor Water Cooled Package Chiller']/row/field[name='Model #']/value"; 52 String s1=getContentString(document,xpathstr); 53 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Screw Compressor Water Cooled Package Chiller']/row/field[name='Component code']/value"; 54 mproduct.put("cs_Model", s1+getContentString(document,xpathstr)); 55 56 //evap 2 57 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col1/field[contains(name,'Entering fluid temp')]/value"; 58 mproduct.put("cs_Evap_Entering_fluid_temp", getContentString(document,xpathstr)); 59 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col1/field[contains(name,'Fluid flow rate')]/value"; 60 mproduct.put("cs_Evap_Fluid_flow_rate", getContentString(document,xpathstr)); 61 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col1/field[contains(name,'Fouling factor')]/value"; 62 mproduct.put("cs_Evap_Fouling_factor", getContentString(document,xpathstr)); 63 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col1/field[contains(name,'Leaving fluid temp')]/value"; 64 mproduct.put("cs_Evap_Leaving_fluid_temp", getContentString(document,xpathstr)); 65 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col1/field[contains(name,'Fluid')]/value"; 66 mproduct.put("cs_Evap_Fluid", getContentString(document,xpathstr)); 67 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col1/field[contains(name,'Number of passes')]/value"; 68 mproduct.put("cs_Evap_Number_of_passes", getContentString(document,xpathstr)); 69 70 //cond 3 71 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col2/field[contains(name,'Entering fluid temp')]/value"; 72 mproduct.put("cs_Cond_Entering_fluid_temp", getContentString(document,xpathstr)); 73 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col2/field[contains(name,'Fluid flow rate')]/value"; 74 mproduct.put("cs_Cond_Fluid_flow_rate", getContentString(document,xpathstr)); 75 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col2/field[contains(name,'Fouling factor')]/value"; 76 mproduct.put("cs_Cond_Fouling_factor", getContentString(document,xpathstr)); 77 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col2/field[contains(name,'Leaving fluid temp')]/value"; 78 mproduct.put("cs_Cond_Leaving_fluid_temp", getContentString(document,xpathstr)); 79 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col2/field[contains(name,'Fluid')]/value"; 80 mproduct.put("cs_Cond_Fluid", getContentString(document,xpathstr)); 81 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Evaporator_Condenser']/col2/field[contains(name,'Number of passes')]/value"; 82 mproduct.put("cs_Cond_Number_of_passes", getContentString(document,xpathstr)); 83 84 //4 Cooling Conditions of service 85 //<name>Capacity (Tons)</name> 86 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Cooling Conditions of service']/col1/field[contains(name,'Capacity')]/value"; 87 mproduct.put("cs_Capacity", getContentString(document,xpathstr)); 88 //<name>Energy efficiency (kW/Ton)</name> 89 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Cooling Conditions of service']/col2/field[contains(name,'Energy efficiency')]/value"; 90 mproduct.put("cs_Energy_efficiency", getContentString(document,xpathstr)); 91 92 //5<name>Unit power supply</name> 93 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Electrical characteristics']/col1/field[contains(name,'Unit power supply')]/value"; 94 mproduct.put("cs_Unit_power_supply", getContentString(document,xpathstr)); 95 96 //6<name>Refrigerant</name>//<value>R134a</value> 97 xpathstr="/report/job/product["+i+"]/performanceData/group[name='Physical Specifications']/col2/field[contains(name,'Refrigerant')]/value"; 98 mproduct.put("cs_Refrigerant", getContentString(document,xpathstr)); 99 100 //getContent(document,xpathstr); 101 102 pl.add(mproduct); 103 } 104 return pl; 105 } 106 107 public static Document parse(org.w3c.dom.Document doc) throws Exception { 108 if (doc == null) { 109 return (null); 110 } 111 org.dom4j.io.DOMReader xmlReader = new org.dom4j.io.DOMReader(); 112 return (xmlReader.read(doc)); 113 } 114 115 public static org.w3c.dom.Document parse(Document doc) throws Exception { 116 if (doc == null) { 117 return (null); 118 } 119 java.io.StringReader reader = new java.io.StringReader(doc.asXML()); 120 org.xml.sax.InputSource source = new org.xml.sax.InputSource(reader); 121 javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory = javax.xml.parsers.DocumentBuilderFactory 122 .newInstance(); 123 javax.xml.parsers.DocumentBuilder documentBuilder = documentBuilderFactory 124 .newDocumentBuilder(); 125 return (documentBuilder.parse(source)); 126 } 127 128 public String getContentString(Document document,String xpathstr){ 129 130 List list = document.selectNodes(xpathstr); 131 String result=""; 132 133 Iterator iter = list.iterator(); 134 iter = list.iterator(); 135 if (iter.hasNext()) { 136 137 Object o=iter.next(); 138 if(o instanceof Attribute){ 139 Attribute attribute = (Attribute) o; 140 //hm.put(attribute.getName(),attribute.getValue()); 141 result=attribute.getValue(); 142 if(debugf){ 143 System.out.println(attribute.getName()+":"+attribute.getValue()); 144 } 145 146 } 147 if(o instanceof Element){ 148 Element element = (Element) o; 149 String name = element.getName(); 150 String value = element.getText(); 151 //hm.put(name, value); 152 result=value; 153 if(debugf){ 154 System.out.println(name+":"+value); 155 } 156 } 157 } else { 158 return result; 159 } 160 return result; 161 } 162 public Map<String,String> getContent(Document document,String xpathstr){ 163 164 List list = document.selectNodes(xpathstr); 165 166 Map<String,String> hm=new HashMap(); 167 Iterator iter = list.iterator(); 168 iter = list.iterator(); 169 if (iter.hasNext()) { 170 171 Object o=iter.next(); 172 if(o instanceof Attribute){ 173 Attribute attribute = (Attribute) o; 174 hm.put(attribute.getName(),attribute.getValue()); 175 if(debugf){ 176 System.out.println(attribute.getName()+":"+attribute.getValue()); 177 } 178 179 } 180 if(o instanceof Element){ 181 Element element = (Element) o; 182 String name = element.getName(); 183 String value = element.getText(); 184 hm.put(name, value); 185 if(debugf){ 186 System.out.println(name+":"+value); 187 } 188 } 189 } else { 190 191 } 192 return hm; 193 } 194 195 }