001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * JRXMLDataSource.java
028: *
029: * Created on 22 giugno 2004, 14.55
030: *
031: */
032:
033: package it.businesslogic.ireport.connection;
034:
035: import javax.xml.parsers.*;
036: import org.w3c.dom.*;
037:
038: /**
039: * The XML datasource define a row as a path.
040: * A path can be truncated.
041: * @author Administrator
042: */
043: public class JRXMLDataSource implements
044: net.sf.jasperreports.engine.JRDataSource {
045:
046: private FieldNode rootFieldNode = null;
047: private String rowPath = "/";
048: private FieldNode actualPath = null;
049:
050: public JRXMLDataSource(String uri, String rowPath) {
051: this .rowPath = rowPath;
052: try {
053: DocumentBuilder db = DocumentBuilderFactory.newInstance()
054: .newDocumentBuilder();
055: org.w3c.dom.Document doc = db.parse(uri);
056: build(doc);
057: } catch (Exception ex) {
058: ex.printStackTrace();
059: }
060: }
061:
062: public JRXMLDataSource(java.io.File file, String rowPath) {
063: this .rowPath = rowPath;
064: try {
065: DocumentBuilder db = DocumentBuilderFactory.newInstance()
066: .newDocumentBuilder();
067: org.w3c.dom.Document doc = db.parse(file);
068: build(doc);
069: } catch (Exception ex) {
070: ex.printStackTrace();
071: }
072: }
073:
074: public JRXMLDataSource(java.io.InputStream is, String rowPath) {
075: this .rowPath = rowPath;
076: try {
077: DocumentBuilder db = DocumentBuilderFactory.newInstance()
078: .newDocumentBuilder();
079: org.w3c.dom.Document doc = db.parse(is);
080: build(doc);
081: } catch (Exception ex) {
082: ex.printStackTrace();
083: }
084: }
085:
086: public JRXMLDataSource(FieldNode rootFieldNode, String rowPath) {
087: this .rowPath = rowPath;
088: this .rootFieldNode = rootFieldNode;
089: }
090:
091: private void build(org.w3c.dom.Document doc) throws Exception {
092: Element element = doc.getDocumentElement();
093: rootFieldNode = createNode(element, null);
094: }
095:
096: private FieldNode createNode(Node element, FieldNode parent)
097: throws Exception {
098:
099: FieldNode cn = new FieldNode(element.getNodeName());
100:
101: // Aggiungiamo gli attributi...
102: NamedNodeMap attributes = element.getAttributes();
103: for (int i = 0; i < attributes.getLength(); ++i) {
104: Node node = attributes.item(i);
105: cn.getAttributes().setProperty(node.getNodeName(),
106: node.getNodeValue());
107: }
108: NodeList nl = element.getChildNodes();
109: for (int i = 0; i < nl.getLength(); ++i) {
110: Node node = nl.item(i);
111: if (node.getNodeType() == Node.ELEMENT_NODE) {
112: createNode(node, cn);
113: } else if (node.getNodeType() == Node.CDATA_SECTION_NODE
114: || node.getNodeType() == Node.TEXT_NODE) {
115: cn.setValue(node.getNodeValue());
116: }
117: }
118:
119: if (parent != null) {
120: parent.getChildren().add(cn);
121: } else {
122: parent = cn;
123: }
124:
125: return parent;
126: }
127:
128: public Object getFieldValue(
129: net.sf.jasperreports.engine.JRField jRField)
130: throws net.sf.jasperreports.engine.JRException {
131:
132: String path = jRField.getDescription();
133:
134: Object val = getPathValue(rootFieldNode, path);
135: //if (jRField.getClass() == String.class)
136: {
137: return val;
138: }
139: //return null;
140: }
141:
142: public boolean next()
143: throws net.sf.jasperreports.engine.JRException {
144: // Create the next path.
145: return rootFieldNode.next(this .rowPath);
146: }
147:
148: public String getActualPath() {
149:
150: String childPath = this .rowPath;
151: childPath = rowPath
152: .substring(rootFieldNode.getName().length() + 1);
153: return rootFieldNode.getName() + "::"
154: + rootFieldNode.getChildsPath(rowPath);
155: }
156:
157: public static void main(String[] argv) {
158: JRXMLDataSource ds = new JRXMLDataSource(
159: "C:\\test_ireport.xml", "/addressbook/address/ciccio");
160: try {
161: System.out.println("starting");
162: while (ds.next()) {
163: System.out.println(ds.getActualPath());
164: }
165: System.out.println("finishing");
166: } catch (Exception ex) {
167: ex.printStackTrace();
168: }
169: }
170:
171: private Object getPathValue(FieldNode startingNode, String path) {
172: String tag = "";
173: if (path == null)
174: return startingNode.getValue();
175:
176: if (path.startsWith("/"))
177: path = path.substring(1);
178:
179: String sub_path = "";
180: if (path.indexOf("+") >= 0) {
181: sub_path = path.substring(path.indexOf("+") + 1);
182: path = path.substring(0, path.indexOf("+") + 1);
183: }
184:
185: if (path.indexOf("/") < 0) {
186: if (path.indexOf("@") >= 0) {
187: tag = path.substring(path.indexOf("@") + 1);
188: return startingNode.getAttribute(tag);
189: //path = path.substring(0, path.indexOf("@"));
190: } else if (path.indexOf("*") >= 0) {
191: String childName = path
192: .substring(path.indexOf("*") + 1);
193: // Create a datasource based on this type of chils...
194: FieldNode fn = new FieldNode(startingNode.getName());
195: fn.setAttributes(startingNode.getAttributes());
196: fn.setChildren(startingNode.getChilddren(childName));
197: return new JRXMLDataSource(fn, "/"
198: + startingNode.getName() + "/" + childName);
199: //path = path.substring(0, path.indexOf("@"));
200: } else if (path.indexOf("+") >= 0) {
201: String childToTake = sub_path;
202: childToTake = getNextNodeName(sub_path);
203: return getSubPathValue(startingNode
204: .getChild(childToTake), sub_path);
205: } else {
206: return startingNode.getValue();
207: }
208: }
209:
210: path = path.substring(path.indexOf("/") + 1);
211: path += sub_path;
212: /*
213: if (path.indexOf("/") > 0)
214: {
215: // go to the next path child...
216: String childToTake = path.substring(0,path.indexOf("/"));
217: System.out.println("taking child " + childToTake);
218: return getPathValue(startingNode.getChild(childToTake), path );
219: }
220: */
221:
222: return getPathValue(startingNode.getNextChild(), path);
223:
224: }
225:
226: private Object getSubPathValue(FieldNode startingNode, String path) {
227: String tag = "";
228: if (path == null)
229: return startingNode.getValue() + "[" + path + "]";
230:
231: //System.out.println("Resolving path " + path + " now in " + startingNode.getName());
232: //System.out.flush();
233:
234: if (path.startsWith("/"))
235: path = path.substring(1);
236:
237: if (path.indexOf("/") < 0) {
238: if (path.indexOf("@") >= 0) {
239: tag = path.substring(path.indexOf("@") + 1);
240: return startingNode.getAttribute(tag); //+ "(tag of " + startingNode.getName() + ")";
241: //path = path.substring(0, path.indexOf("@"));
242: } else if (path.indexOf("*") >= 0) {
243: String childName = path
244: .substring(path.indexOf("*") + 1);
245: // Create a datasource based on this type of chils...
246: FieldNode fn = new FieldNode(startingNode.getName());
247: fn.setAttributes(startingNode.getAttributes());
248: fn.setChildren(startingNode.getChilddren(childName));
249: return new JRXMLDataSource(fn, "/"
250: + startingNode.getName() + "/" + childName);
251: //path = path.substring(0, path.indexOf("@"));
252: } else {
253: return startingNode.getValue(); //+" " + startingNode.getAttributes().size() +" ("+startingNode.getName() + ")";
254: }
255: } else {
256: path = path.substring(path.indexOf("/") + 1);
257:
258: String childToTake = path;
259: childToTake = getNextNodeName(path);
260: return getSubPathValue(startingNode.getChild(childToTake),
261: path); //"(child of "+startingNode.getName() + " " + startingNode.getAttributes().size() + ")";
262:
263: }
264:
265: //return getSubPathValue(startingNode.getNextChild(), path );
266:
267: }
268:
269: private static String getNextNodeName(String path) {
270:
271: if (path == null || path.length() == 0)
272: return "";
273: if (path.startsWith("/"))
274: path = path.substring(1);
275:
276: String childToTake = path;
277: if (path.indexOf("/") >= 0) {
278: childToTake = path.substring(0, path.indexOf("/"));
279: }
280:
281: if (childToTake.indexOf("@") >= 0) {
282: childToTake = childToTake.substring(0, path.indexOf("@"));
283: }
284:
285: if (childToTake.indexOf("*") >= 0) {
286: childToTake = childToTake.substring(0, path.indexOf("*"));
287: }
288:
289: return childToTake;
290: }
291: }
|