001/* 002 * Copyright (c) 2003-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: barseghian $' 006 * '$Date: 2010-07-13 23:03:43 +0000 (Tue, 13 Jul 2010) $' 007 * '$Revision: 25099 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.kepler.reporting.rio.fop; 031 032import javax.xml.transform.TransformerException; 033 034import org.apache.commons.logging.Log; 035import org.apache.commons.logging.LogFactory; 036import org.apache.xpath.XPathAPI; 037import org.kepler.objectmanager.lsid.KeplerLSID; 038import org.kepler.reporting.rio.Item; 039import org.kepler.reporting.rio.ItemLabel; 040import org.kepler.reporting.rio.ItemProperties; 041import org.kepler.reporting.rio.ItemValue; 042import org.kepler.reporting.rio.ReportInstance; 043import org.kepler.reporting.rio.StaticReportItem; 044import org.kepler.reporting.rio.util.TokenUtil; 045import org.w3c.dom.Node; 046import org.w3c.dom.NodeList; 047 048import ptolemy.data.Token; 049 050/** 051 * This class is used to parse XML into a Report class. 052 */ 053public class ReportParser { 054 055 public static Log log = LogFactory.getLog(ReportParser.class); 056 057 public static ReportInstance parse(Node rootNode) throws TransformerException, InstantiationException, IllegalAccessException, ClassNotFoundException { 058 059 ReportInstance report = new ReportInstance(); 060 061 Node titleNode = XPathAPI.selectSingleNode(rootNode, "/report/title"); 062 if (titleNode != null) { 063 report.setTitle(titleNode.getTextContent()); 064 } 065 Node headerNode = XPathAPI.selectSingleNode(rootNode, "/report/headerGraphic"); 066 if (headerNode != null) { 067 report.setHeaderGraphic(headerNode.getTextContent()); 068 } 069 070 //look up the LSID from the XML 071 Node lsidNode = XPathAPI.selectSingleNode(rootNode, "/report/lsid"); 072 if (lsidNode != null) { 073 KeplerLSID lsid = null; 074 try { 075 lsid = new KeplerLSID(lsidNode.getTextContent()); 076 } catch (Exception e) { 077 log.error("Error setting report layout LSID: " + e.getMessage()); 078 e.printStackTrace(); 079 } 080 report.setLsid(lsid); 081 } 082 083 //look up the workflow reference 084 Node wfLsidNode = XPathAPI.selectSingleNode(rootNode, "/report/workflow"); 085 if (wfLsidNode != null) { 086 KeplerLSID wfLsid = null; 087 //NamedObj workflow = null; 088 try { 089 wfLsid = new KeplerLSID(wfLsidNode.getTextContent()); 090 // why was this here? - 11.13.09 derik 091 //workflow = ObjectManager.getInstance().getHighestObjectRevision(wfLsid); 092 } catch (Exception e) { 093 log.error("could not find workflow for LSID: " + wfLsid); 094 e.printStackTrace(); 095 } 096 report.setWorkflowLSID(wfLsid); 097 } 098 099 //items 100 NodeList itemNodeList = XPathAPI.selectNodeList(rootNode, "/report/item"); 101 102 for (int i = 0; i < itemNodeList.getLength(); i++) { 103 Node itemNode = itemNodeList.item(i); 104 105 Node classNode = XPathAPI.selectSingleNode(itemNode, "@class"); 106 String classStr = null; 107 Node nameNode = XPathAPI.selectSingleNode(itemNode, "@name"); 108 String nameStr = null; 109 Node typeNode = XPathAPI.selectSingleNode(itemNode, "@type"); 110 String typeStr = null; 111 112 Item item = new StaticReportItem(); 113 if (classNode != null) { 114 classStr = classNode.getTextContent(); 115 item = (Item) Class.forName(classStr).newInstance(); 116 } 117 if (nameNode != null) { 118 nameStr = nameNode.getTextContent(); 119 } 120 if (typeNode != null) { 121 typeStr = typeNode.getTextContent(); 122 } 123 item.setName(nameStr); 124 item.setType(typeStr); 125 126 //properties 127 ItemProperties itemProperties = new ItemProperties(); 128 NodeList propertyNodeList = XPathAPI.selectNodeList(itemNode, "properties/property"); 129 for (int j = 0; j < propertyNodeList.getLength(); j++) { 130 Node propertyNode = propertyNodeList.item(j); 131 Node propertyNameNode = XPathAPI.selectSingleNode(propertyNode, "@name"); 132 String key = propertyNameNode.getTextContent(); 133 String value = propertyNode.getTextContent(); 134 itemProperties.setProperty(key, value); 135 } 136 item.setItemProperties(itemProperties); 137 138 //labels 139 NodeList labelNodeList = XPathAPI.selectNodeList(itemNode, "label"); 140 for (int j = 0; j < labelNodeList.getLength(); j++) { 141 Node labelNode = labelNodeList.item(j); 142 Node labelPositionNode = XPathAPI.selectSingleNode(labelNode, "@position"); 143 ItemLabel label = new ItemLabel(labelNode.getTextContent()); 144 label.setPosition(labelPositionNode.getTextContent()); 145 item.addLabel(label); 146 } 147 148 //values 149 NodeList valuesNodeList = XPathAPI.selectNodeList(itemNode, "value"); 150 for (int j = 0; j < valuesNodeList.getLength(); j++) { 151 ItemValue itemValue = new ItemValue(); 152 Node valueNode = valuesNodeList.item(j); 153 // Handle token node 154 Node tokenNode = XPathAPI.selectSingleNode(valueNode, "token"); 155 if (tokenNode != null) { 156 Node tokenTypeNode = XPathAPI.selectSingleNode(tokenNode, "@type"); 157 if (tokenTypeNode != null) { 158 String tokenType = tokenTypeNode.getTextContent(); 159 String tokenValue = tokenNode.getTextContent(); 160 Token token = TokenUtil.getToken(tokenType, tokenValue); 161 itemValue.setValue(token); 162 } 163 } 164 // Handle base64encoded node 165 Node base64dataNode = XPathAPI.selectSingleNode(valueNode, "base64data"); 166 if (base64dataNode != null) { 167 String base64data = base64dataNode.getTextContent(); 168 itemValue.setBase64data(base64data); 169 } 170 item.getValues().add(itemValue); 171 172 } 173 174 //add it 175 report.getSections().get(0).getColumns().get(0).getItems().add(item); 176 } 177 return report; 178 } 179 180}