001: /*
002: * Copyright (C) 2004 Giuseppe MANNA
003: *
004: * This file is part of FreeReportBuilder
005: *
006: * FreeReportBuilder is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * 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, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package it.frb;
023:
024: import java.io.InputStream;
025: import java.util.Vector;
026:
027: import org.w3c.dom.NamedNodeMap;
028: import org.w3c.dom.Node;
029: import org.w3c.dom.NodeList;
030:
031: public class XmlFileReader {
032: private String xmlFilePath;
033: private InputStream xmlStream;
034: private Vector vectorOfObjects;
035: private javax.xml.parsers.DocumentBuilder dBuilder;
036: private java.awt.event.ActionListener actionListener;
037: private java.io.File xml;
038: private org.w3c.dom.Document dom;
039:
040: public XmlFileReader(String xmlFilePath) {
041: this (xmlFilePath, true);
042: }
043:
044: public XmlFileReader(InputStream xmlStream) {
045: this (xmlStream, true);
046: }
047:
048: public XmlFileReader(InputStream xmlStream, boolean addDgnController) {
049: this .xmlStream = xmlStream;
050:
051: XMLDocument xmlDoc = new XMLDocument();
052:
053: try {
054: dBuilder = xmlDoc.newDocumentBuilder();
055: dom = dBuilder.parse(xmlStream);
056:
057: } catch (javax.xml.parsers.ParserConfigurationException e) {
058: javax.swing.JOptionPane
059: .showMessageDialog(
060: null,
061: "Impossible to load the report character not recognized. To replace the character with the suitable line error. -"
062: + e.getMessage());
063:
064: } catch (org.xml.sax.SAXException saxE) {
065: javax.swing.JOptionPane
066: .showMessageDialog(
067: null,
068: "Impossible to load the report character not recognized. To replace the character with the suitable line error. -"
069: + saxE.getMessage());
070:
071: } catch (java.io.IOException IoE) {
072: javax.swing.JOptionPane.showMessageDialog(null, IoE
073: .getMessage());
074: }
075: }
076:
077: public XmlFileReader(String xmlFilePath, boolean addDgnController) {
078: this .xmlFilePath = xmlFilePath;
079:
080: XMLDocument xmlDoc = new XMLDocument();
081:
082: try {
083: dBuilder = xmlDoc.newDocumentBuilder();
084: xml = new java.io.File(xmlFilePath);
085: dom = dBuilder.parse(xml);
086:
087: } catch (javax.xml.parsers.ParserConfigurationException e) {
088: javax.swing.JOptionPane
089: .showMessageDialog(
090: null,
091: "Impossible to load the report character not recognized. To replace the character with the suitable line error. -"
092: + e.getMessage());
093:
094: } catch (org.xml.sax.SAXException saxE) {
095: javax.swing.JOptionPane
096: .showMessageDialog(
097: null,
098: "Impossible to load the report character not recognized. To replace the character with the suitable line error. -"
099: + saxE.getMessage());
100:
101: } catch (java.io.IOException IoE) {
102: //System.out.println(IoE);
103: javax.swing.JOptionPane.showMessageDialog(null, IoE
104: .getMessage());
105: //throw new Exception("Errore in fase di caricamento.");
106: }
107: }
108:
109: public Vector getObjects(String bandName) {
110: if (!bandName.equals("SQL")) {
111: vectorOfObjects = new Vector();
112:
113: NodeList nd = dom.getElementsByTagName(bandName);
114: Node n = nd.item(0);
115: NamedNodeMap nnMM = n.getAttributes();
116:
117: if (nnMM.item(0) != null) {
118: vectorOfObjects.add(nnMM.item(0).getNodeValue());
119: Node nlObj = n.getFirstChild().getNextSibling();
120: NodeList nl = nlObj.getChildNodes();
121:
122: return readObject(nl);
123:
124: } else {
125: return readObject(n.getChildNodes());
126: }
127:
128: } else {
129: vectorOfObjects = new Vector();
130: vectorOfObjects.add(getSQLText());
131: return vectorOfObjects;
132: }
133: }
134:
135: public Vector getObjects() {
136: vectorOfObjects = new Vector();
137:
138: NodeList nd = dom.getElementsByTagName("Objects");
139:
140: for (int i = 0; i < nd.getLength(); i++) {
141: Node n = nd.item(i);
142: NodeList nl = n.getChildNodes();
143:
144: vectorOfObjects = readObject(nl);
145: }
146:
147: return vectorOfObjects;
148: }
149:
150: public int getTotalPage() {
151: String nodeValue;
152: NodeList nd = dom.getElementsByTagName("TotalPage");
153:
154: try {
155: nodeValue = nd.item(0).getFirstChild().getNodeValue()
156: .trim();
157:
158: } catch (NullPointerException npe) {
159: javax.swing.JOptionPane
160: .showMessageDialog(
161: null,
162: "Probably the file that he is' loading not and' a report saved with the application or and' corrupt!");
163: return 0;
164: }
165:
166: return new Integer(nodeValue).intValue();
167: }
168:
169: public String getSQLText() {
170: NodeList nd = dom.getElementsByTagName("SQL");
171: String nodeValue = nd.item(0).getFirstChild().getNodeValue();
172:
173: if (nodeValue != null) {
174: nodeValue = replaceString(nodeValue, "<", "<");
175: nodeValue = replaceString(nodeValue, ">", ">");
176: }
177:
178: return nodeValue;
179: }
180:
181: public int getGroupCount() {
182: org.w3c.dom.Element dataElem = dom.getDocumentElement();
183: String groupCount = dataElem.getAttribute("groupCount");
184:
185: return Integer.parseInt(groupCount);
186: }
187:
188: private Vector readObject(NodeList nl) {
189: for (int j = 0; j < nl.getLength(); j++) {
190: Node chl = nl.item(j);
191:
192: if (!((String) chl.getNodeName()).equals("#text")) {
193: Object obj = null;
194: try {
195: obj = Class.forName(chl.getNodeName())
196: .newInstance();
197: } catch (ClassNotFoundException e) {
198: System.out.println(e);
199:
200: } catch (InstantiationException a) {
201: System.out.println(a);
202:
203: } catch (IllegalAccessException b) {
204: System.out.println(b);
205: }
206:
207: NodeList lastNode = chl.getChildNodes();
208:
209: int x = 0;
210: int y = 0;
211:
212: for (int z = 0; z < lastNode.getLength(); z++) {
213: if (!((String) lastNode.item(z).getNodeName())
214: .equals("#text")) {
215: if (lastNode.item(z).getNodeName()
216: .equalsIgnoreCase("location")) {
217: NamedNodeMap nnM = lastNode.item(z)
218: .getAttributes();
219: for (int k = 0; k < nnM.getLength(); k++) {
220: if (!((String) nnM.item(k)
221: .getNodeName()).equals("#text")) {
222: if ((nnM.item(k).getNodeName()
223: .equals("x"))) {
224: Integer Ix = new Integer(nnM
225: .item(k).getNodeValue());
226: x = Ix.intValue();
227:
228: } else if ((nnM.item(k)
229: .getNodeName().equals("y"))) {
230: Integer Iy = new Integer(nnM
231: .item(k).getNodeValue());
232: y = Iy.intValue();
233:
234: }
235: }
236: }
237:
238: } else if (lastNode.item(z).getNodeName()
239: .equalsIgnoreCase("setForeground")
240: || lastNode.item(z).getNodeName()
241: .equalsIgnoreCase(
242: "setBackground")) {
243: String sMethodName = lastNode.item(z)
244: .getNodeName();
245: NodeList nParam = lastNode.item(z)
246: .getChildNodes();
247:
248: Vector vargs_class = new Vector();
249: Vector vargs_value = new Vector();
250:
251: for (int w = 0; w < nParam.getLength(); w++) {
252: if (!((String) nParam.item(w)
253: .getNodeName()).equals("#text")) {
254: NamedNodeMap nnM = nParam.item(w)
255: .getAttributes();
256: for (int k = 0; k < nnM.getLength(); k++) {
257: if (!((String) nnM.item(k)
258: .getNodeName())
259: .equals("#text")) {
260: if ((nnM.item(k)
261: .getNodeName()
262: .equals("value"))) {
263: vargs_value
264: .addElement(new java.awt.Color(
265: Integer
266: .parseInt(nnM
267: .item(
268: k)
269: .getNodeValue())));
270: } else if ((nnM.item(k)
271: .getNodeName()
272: .equals("class"))) {
273: vargs_class
274: .addElement(ParamClassPrimitiveType
275: .getPrimitiveTypeClass(nnM
276: .item(
277: k)
278: .getNodeValue()));
279: }
280: }
281: }
282: }
283: }
284:
285: int counter = vargs_class.size();
286:
287: Class[] args_class = new Class[counter];
288: Object[] args_value = new Object[counter];
289:
290: for (int s = 0; s < counter; s++) {
291: args_class[s] = (Class) vargs_class
292: .get(s);
293: args_value[s] = vargs_value.get(s);
294: }
295: this .startMethod(obj, sMethodName,
296: args_class, args_value);
297:
298: } else if (lastNode.item(z).getNodeName()
299: .equalsIgnoreCase("setIcon")) {
300: String sMethodName = lastNode.item(z)
301: .getNodeName();
302: NodeList nParam = lastNode.item(z)
303: .getChildNodes();
304:
305: Vector vargs_class = new Vector();
306: Vector vargs_value = new Vector();
307:
308: for (int w = 0; w < nParam.getLength(); w++) {
309: if (!((String) nParam.item(w)
310: .getNodeName()).equals("#text")) {
311: NamedNodeMap nnM = nParam.item(w)
312: .getAttributes();
313: for (int k = 0; k < nnM.getLength(); k++) {
314: if (!((String) nnM.item(k)
315: .getNodeName())
316: .equals("#text")) {
317: if ((nnM.item(k)
318: .getNodeName()
319: .equals("value"))) {
320: vargs_value
321: .addElement(new javax.swing.ImageIcon(
322: nnM
323: .item(
324: k)
325: .getNodeValue()));
326: } else if ((nnM.item(k)
327: .getNodeName()
328: .equals("class"))) {
329: vargs_class
330: .addElement(ParamClassPrimitiveType
331: .getPrimitiveTypeClass(nnM
332: .item(
333: k)
334: .getNodeValue()));
335: }
336: }
337: }
338: }
339: }
340:
341: int counter = vargs_class.size();
342:
343: Class[] args_class = new Class[counter];
344: Object[] args_value = new Object[counter];
345:
346: for (int s = 0; s < counter; s++) {
347: args_class[s] = (Class) vargs_class
348: .get(s);
349: args_value[s] = vargs_value.get(s);
350: }
351:
352: this .startMethod(obj, sMethodName,
353: args_class, args_value);
354:
355: } else if (lastNode.item(z).getNodeName()
356: .equalsIgnoreCase("setFont")) {
357: String sMethodName = lastNode.item(z)
358: .getNodeName();
359: NodeList nParam = lastNode.item(z)
360: .getChildNodes();
361:
362: Vector vargs_class = new Vector();
363: Vector vargs_value = new Vector();
364:
365: for (int w = 0; w < nParam.getLength(); w++) {
366: if (!((String) nParam.item(w)
367: .getNodeName()).equals("#text")) {
368: NamedNodeMap nnM = nParam.item(w)
369: .getAttributes();
370: for (int k = 0; k < nnM.getLength(); k++) {
371: if (!((String) nnM.item(k)
372: .getNodeName())
373: .equals("#text")) {
374: if ((nnM.item(k)
375: .getNodeName()
376: .equals("value"))) {
377: vargs_value
378: .addElement(nnM
379: .item(k)
380: .getNodeValue());
381: } else if ((nnM.item(k)
382: .getNodeName()
383: .equals("class"))) {
384: vargs_class
385: .addElement(ParamClassPrimitiveType
386: .getPrimitiveTypeClass(nnM
387: .item(
388: k)
389: .getNodeValue()));
390: }
391: }
392: }
393: }
394: }
395:
396: String fontName = (String) vargs_value
397: .get(0);
398:
399: Integer integ = new Integer(
400: (String) vargs_value.get(1));
401: int style = integ.intValue();
402:
403: integ = new Integer((String) vargs_value
404: .get(2));
405: int size = integ.intValue();
406:
407: java.awt.Font font = new java.awt.Font(
408: fontName, style, size);
409:
410: vargs_class.clear();
411: vargs_value.clear();
412:
413: vargs_class
414: .addElement(ParamClassPrimitiveType
415: .getPrimitiveTypeClass("java.awt.Font"));
416: vargs_value.addElement(font);
417:
418: int counter = vargs_class.size();
419:
420: Class[] args_class = new Class[counter];
421: Object[] args_value = new Object[counter];
422:
423: for (int s = 0; s < counter; s++) {
424: args_class[s] = (Class) vargs_class
425: .get(s);
426: args_value[s] = vargs_value.get(s);
427: }
428:
429: this .startMethod(obj, sMethodName,
430: args_class, args_value);
431:
432: } else {
433: String sMethodName = lastNode.item(z)
434: .getNodeName();
435: NodeList nParam = lastNode.item(z)
436: .getChildNodes();
437:
438: Vector vargs_class = new Vector();
439: Vector vargs_value = new Vector();
440:
441: for (int w = 0; w < nParam.getLength(); w++) {
442: if (!((String) nParam.item(w)
443: .getNodeName()).equals("#text")) {
444: NamedNodeMap nnM = nParam.item(w)
445: .getAttributes();
446: for (int k = 0; k < nnM.getLength(); k++) {
447: if (!((String) nnM.item(k)
448: .getNodeName())
449: .equals("#text")) {
450: if ((nnM.item(k)
451: .getNodeName()
452: .equals("value"))) {
453: //Questo if serve perche' lengthtextArea devono
454: //ricordarsi la formattazione delle linee
455: if (!chl
456: .getNodeName()
457: .equals(
458: "javax.swing.JTextArea")) {
459: vargs_value
460: .addElement(ParamClassPrimitiveType
461: .getWrapperPrimitiveValue(
462: nnM
463: .getNamedItem(
464: "class")
465: .getNodeValue(),
466: nnM
467: .item(
468: k)
469: .getNodeValue()));
470: } else {
471: if (sMethodName
472: .equals("setText")) {
473: Node node = nParam
474: .item(w)
475: .getFirstChild();
476: String value = "";
477:
478: if (node != null) {
479: value = node
480: .getNodeValue();
481: }
482:
483: vargs_value
484: .addElement(ParamClassPrimitiveType
485: .getWrapperPrimitiveValue(
486: nnM
487: .getNamedItem(
488: "class")
489: .getNodeValue(),
490: value));
491: } else {
492: vargs_value
493: .addElement(ParamClassPrimitiveType
494: .getWrapperPrimitiveValue(
495: nnM
496: .getNamedItem(
497: "class")
498: .getNodeValue(),
499: nnM
500: .item(
501: k)
502: .getNodeValue()));
503: }
504: }
505: } else if ((nnM.item(k)
506: .getNodeName()
507: .equals("class"))) {
508: vargs_class
509: .addElement(ParamClassPrimitiveType
510: .getPrimitiveTypeClass(nnM
511: .item(
512: k)
513: .getNodeValue()));
514: }
515: }
516: }
517: }
518: }
519:
520: int counter = vargs_class.size();
521:
522: Class[] args_class = new Class[counter];
523: Object[] args_value = new Object[counter];
524:
525: for (int s = 0; s < counter; s++) {
526: args_class[s] = (Class) vargs_class
527: .get(s);
528: args_value[s] = vargs_value.get(s);
529: }
530:
531: this .startMethod(obj, sMethodName,
532: args_class, args_value);
533: }
534: }
535: }
536:
537: ((javax.swing.JComponent) obj).setLocation(x, y);
538:
539: vectorOfObjects.add(obj);
540: }
541: }
542:
543: return vectorOfObjects;
544: }
545:
546: private void startMethod(Object obj, String sMethod,
547: Class[] args_class, Object[] args_value) {
548: Class cObj = obj.getClass();
549: if (args_class != null) {
550: try {
551: java.lang.reflect.Method met = cObj.getMethod(sMethod,
552: args_class);
553: met.invoke(obj, args_value);
554:
555: } catch (NoSuchMethodException nSm) {
556: System.out.println("NoSuchMethodException: "
557: + nSm.getMessage());
558: } catch (java.lang.reflect.InvocationTargetException iTe) {
559: System.out.println("InvocationTargetException: "
560: + iTe.getMessage());
561: } catch (IllegalAccessException b) {
562: System.out.println("IllegalAccessException: " + b);
563: }
564: }
565: }
566:
567: public static String replace(StringBuffer sbSource, int nStart,
568: int nEnd, String sStr) {
569: String sHead = sbSource.toString().substring(0, nStart);
570: String sFoot = sbSource.toString().substring(nEnd);
571:
572: StringBuffer sbRet = new StringBuffer(sHead);
573: sbRet.append(sStr);
574: sbRet.append(sFoot);
575:
576: return sbRet.toString();
577: }
578:
579: private String replaceString(String strSource, String strFind,
580: String strReplace) {
581: int indice = strSource.indexOf(strFind);
582:
583: if (indice >= 0) {
584: String s = replace(new StringBuffer(strSource), indice,
585: indice + strFind.length(), strReplace);
586: strSource = this.replaceString(s, strFind, strReplace);
587: }
588:
589: return strSource;
590: }
591: }
|