001: // @@
002: // @@
003: /*
004: * Wi.Ser Framework
005: *
006: * LGPL Version: 1.8.1, 20-September-2007
007: * Copyright (C) 2005-2006 Dirk von der Weiden <dvdw@imail.de>
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library located in LGPL.txt in the
021: * license directory; if not, write to the
022: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
023: * Boston, MA 02111-1307, USA.
024: *
025: * If this agreement does not cover your requirements, please contact us
026: * via email to get detailed information about the commercial license
027: * or our service offerings!
028: *
029: */
030: // @@
031: package de.ug2t.connector;
032:
033: import java.io.*;
034: import java.net.*;
035: import java.util.*;
036:
037: import org.xml.sax.*;
038: import org.xml.sax.helpers.*;
039:
040: import com.bluecast.xml.*;
041:
042: import de.ug2t.kernel.*;
043:
044: class xml_saxEventhandler extends DefaultHandler {
045: private Map pem_aktSource = null;
046: private Map pem_aktCluster = null;
047: private CoXmlParameterGetter pem_myGetter = null;
048:
049: public final void pcmf_setSourceName(String xName,
050: CoXmlParameterGetter xGetter) {
051: pem_myGetter = xGetter;
052:
053: this .pem_aktSource = (Map) this .pem_myGetter.pdm_sources
054: .get(xName);
055:
056: return;
057: };
058:
059: public final void startElement(String uri, String localName,
060: String qName, Attributes attributes) throws SAXException {
061: while (true) {
062: if (qName.equals("Parameter_List"))
063: break;
064:
065: if (qName.equals("include")) {
066: String l_file = KeTools.pcmf_deRef(attributes
067: .getValue("file"));
068: this .pem_myGetter.pdmf_includeSource(l_file);
069:
070: break;
071: }
072: ;
073:
074: if (qName.equals("Cluster")) {
075: String name = KeTools.pcmf_deRef(attributes
076: .getValue("name"));
077: Map cluster = new LinkedHashMap();
078: this .pem_aktSource.put(name, cluster);
079:
080: this .pem_aktCluster = cluster;
081: // Debug
082: // System.out.print("\nCluster: " + name);
083:
084: break;
085: }
086: ;
087:
088: if (qName.equals("Parameter")) {
089: String name = KeTools.pcmf_deRef(attributes
090: .getValue("name"));
091: String value = KeTools.pcmf_deRef(attributes
092: .getValue("value"));
093: String obj = KeTools.pcmf_deRef(attributes
094: .getValue("obj"));
095: String regL = KeTools.pcmf_deRef(attributes
096: .getValue("registerL"));
097: String regG = KeTools.pcmf_deRef(attributes
098: .getValue("registerG"));
099:
100: if (obj != null && obj.equals("true"))
101: this .pem_aktCluster.put(name, KeRegisteredObject
102: .pcmf_getObjByName(value));
103: else
104: this .pem_aktCluster.put(name, value);
105:
106: if (regL != null && regL.equals("true")
107: && value != null)
108: KeRegisteredObject.pcmf_register(name, value);
109: if (regG != null && regG.equals("true")
110: && value != null)
111: KeRegisteredObject.pcmf_registerGlobal(name, value);
112:
113: // System.out.print("\nParamter: " + name + ":" + value);
114:
115: break;
116: }
117: ;
118:
119: break;
120: }
121: ;
122:
123: return;
124: };
125: };
126:
127: public class CoXmlParameterGetter implements ICoParameterGetter,
128: ICoXmlParameterGetter {
129: protected Map pdm_sources = new LinkedHashMap();
130: protected Map pdm_activeSource = null;
131: protected Map pdm_activeCluster = null;
132: static XMLReader pem_xmlReader = new Piccolo();
133: static xml_saxEventhandler pem_handler = new xml_saxEventhandler();
134:
135: static {
136: pem_xmlReader.setContentHandler(pem_handler);
137: };
138:
139: public CoXmlParameterGetter() {
140: return;
141: };
142:
143: protected final void pdmf_includeSource(String xSource) {
144: synchronized (pem_handler) {
145: try {
146: String l_fName = KeEnvironment.pcmf_buildPath(xSource);
147: if (new File(l_fName).exists()) {
148: KeLog
149: .pcmf_log("ug2t",
150: "source exsits, load from file: "
151: + xSource, this ,
152: KeLog.DEBUG);
153:
154: FileReader l_fr = new FileReader(l_fName);
155: InputSource l_source = new InputSource(l_fr);
156: l_source.setEncoding(KeEnvironment
157: .pcmf_getCharacterSet());
158:
159: XMLReader l_xmlReader = new Piccolo();
160: l_xmlReader.setContentHandler(pem_handler);
161: l_xmlReader.parse(l_source);
162:
163: l_fr.close();
164: }
165: } catch (SAXParseException se) {
166: KeLog.pcmf_log("ug2t",
167: "SAX parser Exception at [line: "
168: + se.getLineNumber() + "] [col: "
169: + se.getColumnNumber() + "] [file: "
170: + KeEnvironment.pcmf_buildPath(xSource)
171: + "]", this , KeLog.FATAL);
172: } catch (Exception e) {
173: KeLog.pcmf_logException("ug2t", this , e);
174: }
175: }
176:
177: return;
178: };
179:
180: /*
181: * (non-Javadoc)
182: *
183: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_addSource(java.lang.String)
184: */
185: public final void pcmf_addSource(String xSource) throws Exception {
186: synchronized (pem_handler) {
187: if (pdm_sources.get(xSource) != null) {
188: KeLog.pcmf_log("ug2t",
189: "parameter source already loaded: " + xSource,
190: this , KeLog.DEBUG);
191: return;
192: }
193: ;
194: pdm_sources.put(xSource, new LinkedHashMap());
195: pem_handler.pcmf_setSourceName(xSource, this );
196:
197: try {
198: String l_fName = KeEnvironment.pcmf_buildPath(xSource);
199: if (new File(l_fName).exists()) {
200: KeLog
201: .pcmf_log("ug2t",
202: "source exsits, load from file: "
203: + xSource, this ,
204: KeLog.DEBUG);
205:
206: FileReader l_fr = new FileReader(l_fName);
207: InputSource l_source = new InputSource(l_fr);
208: l_source.setEncoding(KeEnvironment
209: .pcmf_getCharacterSet());
210: pem_xmlReader.parse(l_source);
211: l_fr.close();
212: }
213: } catch (SAXParseException se) {
214: KeLog.pcmf_log("ug2t",
215: "SAX parser Exception at [line: "
216: + se.getLineNumber() + "] [col: "
217: + se.getColumnNumber() + "] [file: "
218: + KeEnvironment.pcmf_buildPath(xSource)
219: + "]", this , KeLog.FATAL);
220:
221: throw (se);
222: }
223: }
224:
225: return;
226: };
227:
228: /*
229: * (non-Javadoc)
230: *
231: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_setSource(java.lang.String)
232: */
233: public final void pcmf_setSource(String xSource) throws Exception {
234: synchronized (pem_handler) {
235: if (pdm_sources.get(xSource) != null)
236: return;
237:
238: pdm_sources.put(xSource, new LinkedHashMap());
239: pem_handler.pcmf_setSourceName(xSource, this );
240:
241: try {
242: FileReader l_fr = new FileReader(KeEnvironment
243: .pcmf_buildPath(xSource));
244: InputSource l_source = new InputSource(l_fr);
245: l_source.setEncoding(KeEnvironment
246: .pcmf_getCharacterSet());
247: pem_xmlReader.parse(l_source);
248: l_fr.close();
249: } catch (SAXParseException se) {
250: KeLog.pcmf_log("ug2t",
251: "SAX parser Exception at [line: "
252: + se.getLineNumber() + "] [col: "
253: + se.getColumnNumber() + "] [file: "
254: + KeEnvironment.pcmf_buildPath(xSource)
255: + "]", this , KeLog.FATAL);
256:
257: throw (se);
258: }
259: }
260:
261: return;
262: };
263:
264: public final void pcmf_setSourceURI(String xSource)
265: throws Exception {
266: synchronized (pem_handler) {
267: if (pdm_sources.get(xSource) != null)
268: return;
269:
270: pdm_sources.put(xSource, new LinkedHashMap());
271: pem_handler.pcmf_setSourceName(xSource, this );
272:
273: try {
274: URL l_fName = new URL(xSource);
275: InputStream l_stream = l_fName.openStream();
276:
277: InputSource l_source = new InputSource(l_stream);
278: l_source.setEncoding(KeEnvironment
279: .pcmf_getCharacterSet());
280: pem_xmlReader.parse(l_source);
281: l_stream.close();
282: } catch (SAXParseException se) {
283: KeLog.pcmf_log("ug2t",
284: "SAX parser Exception at [line: "
285: + se.getLineNumber() + "] [col: "
286: + se.getColumnNumber() + "] [file: "
287: + KeEnvironment.pcmf_buildPath(xSource)
288: + "]", this , KeLog.FATAL);
289:
290: throw (se);
291: }
292: }
293:
294: return;
295: };
296:
297: /*
298: * (non-Javadoc)
299: *
300: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_addSourceReNew(java.lang.String)
301: */
302: public final void pcmf_addSourceReNew(String xSource)
303: throws Exception {
304: synchronized (pem_handler) {
305: if (pdm_sources.get(xSource) != null) {
306: KeLog.pcmf_log("ug2t", "parameter source reloaded: "
307: + xSource, this , KeLog.MESSAGE);
308: if (this .pdm_sources.remove(xSource) == this .pdm_activeSource) {
309: this .pdm_activeCluster = null;
310: this .pdm_activeSource = null;
311: }
312: ;
313: }
314: ;
315: pdm_sources.put(xSource, new LinkedHashMap());
316: pem_handler.pcmf_setSourceName(xSource, this );
317:
318: try {
319: FileReader l_fr = new FileReader(KeEnvironment
320: .pcmf_buildPath(xSource));
321: InputSource l_source = new InputSource(l_fr);
322: l_source.setEncoding(KeEnvironment
323: .pcmf_getCharacterSet());
324: pem_xmlReader.parse(l_source);
325: l_fr.close();
326: } catch (SAXParseException se) {
327: KeLog.pcmf_log("ug2t",
328: "SAX parser Exception at [line: "
329: + se.getLineNumber() + "] [col: "
330: + se.getColumnNumber() + "] [file: "
331: + KeEnvironment.pcmf_buildPath(xSource)
332: + "]", this , KeLog.FATAL);
333:
334: throw (se);
335: }
336: }
337:
338: return;
339: };
340:
341: public final void pcmf_addSourceURIReNew(String xSource)
342: throws Exception {
343: synchronized (pem_handler) {
344: if (pdm_sources.get(xSource) != null) {
345: KeLog.pcmf_log("ug2t", "parameter source reloaded: "
346: + xSource, this , KeLog.MESSAGE);
347: if (this .pdm_sources.remove(xSource) == this .pdm_activeSource) {
348: this .pdm_activeCluster = null;
349: this .pdm_activeSource = null;
350: }
351: ;
352: }
353: ;
354: pdm_sources.put(xSource, new LinkedHashMap());
355: pem_handler.pcmf_setSourceName(xSource, this );
356:
357: try {
358: URL l_fName = new URL(xSource);
359: InputStream l_stream = l_fName.openStream();
360:
361: InputSource l_source = new InputSource(l_stream);
362: l_source.setEncoding(KeEnvironment
363: .pcmf_getCharacterSet());
364:
365: pem_xmlReader.parse(l_source);
366: l_stream.close();
367: } catch (SAXParseException se) {
368: KeLog.pcmf_log("ug2t",
369: "SAX parser Exception at [line: "
370: + se.getLineNumber() + "] [col: "
371: + se.getColumnNumber() + "] [file: "
372: + KeEnvironment.pcmf_buildPath(xSource)
373: + "]", this , KeLog.FATAL);
374:
375: throw (se);
376: }
377: }
378:
379: return;
380: };
381:
382: /*
383: * (non-Javadoc)
384: *
385: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_remSource(java.lang.String)
386: */
387: public final void pcmf_removeSource(String xSource) {
388: if (this .pdm_sources.remove(xSource) == this .pdm_activeSource) {
389: this .pdm_activeCluster = null;
390: this .pdm_activeSource = null;
391: }
392: ;
393:
394: return;
395: };
396:
397: /*
398: * (non-Javadoc)
399: *
400: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_getParameterString(java.lang.String,
401: * java.lang.String, java.lang.String)
402: */
403: public final String pcmf_getParameterString(String xSource,
404: String xCluster, String xKey) {
405: this .pdm_activeSource = (Map) this .pdm_sources.get(xSource);
406: if (this .pdm_activeSource == null)
407: return (null);
408: this .pdm_activeCluster = (Map) this .pdm_activeSource
409: .get(xCluster);
410: if (this .pdm_activeCluster == null)
411: return (null);
412:
413: return ((String) this .pdm_activeCluster.get(xKey));
414: };
415:
416: /*
417: * (non-Javadoc)
418: *
419: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_getParameterString(java.lang.String,
420: * java.lang.String, java.lang.String)
421: */
422: public final void pcmf_setActiveSource(String xSource) {
423: this .pdm_activeSource = (Map) this .pdm_sources.get(xSource);
424: if (this .pdm_activeSource == null)
425: KeLog.pcmf_log("ug2t", "Error setting parameter source: "
426: + xSource, this , KeLog.ERROR);
427: };
428:
429: /*
430: * (non-Javadoc)
431: *
432: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_getParameterString(java.lang.String,
433: * java.lang.String)
434: */
435: public final String pcmf_getParameterString(String xCluster,
436: String xKey) {
437: this .pdm_activeCluster = (Map) this .pdm_activeSource
438: .get(xCluster);
439: if (this .pdm_activeCluster == null)
440: return (null);
441:
442: return ((String) this .pdm_activeCluster.get(xKey));
443: };
444:
445: /*
446: * (non-Javadoc)
447: *
448: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_getParameterString(java.lang.String)
449: */
450: public final String pcmf_getParameterString(String xKey) {
451: return ((String) this .pdm_activeCluster.get(xKey));
452: };
453:
454: /*
455: * (non-Javadoc)
456: *
457: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_getParameter(java.lang.String,
458: * java.lang.String, java.lang.String)
459: */
460: public final Object pcmf_getParameter(String xSource,
461: String xCluster, String xKey) {
462: this .pdm_activeSource = (Map) this .pdm_sources.get(xSource);
463: if (this .pdm_activeSource == null)
464: return (null);
465: this .pdm_activeCluster = (Map) this .pdm_activeSource
466: .get(xCluster);
467: if (this .pdm_activeCluster == null)
468: return (null);
469:
470: return (this .pdm_activeCluster.get(xKey));
471: };
472:
473: /*
474: * (non-Javadoc)
475: *
476: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_removeParameter(java.lang.String,
477: * java.lang.String, java.lang.String)
478: */
479: public final Object pcmf_removeParameter(String xSource,
480: String xCluster, String xKey) {
481: this .pdm_activeSource = (Map) this .pdm_sources.get(xSource);
482: if (this .pdm_activeSource == null)
483: return (null);
484: this .pdm_activeCluster = (Map) this .pdm_activeSource
485: .get(xCluster);
486: if (this .pdm_activeCluster == null)
487: return (null);
488:
489: return (this .pdm_activeCluster.remove(xKey));
490: };
491:
492: /*
493: * (non-Javadoc)
494: *
495: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_setParameter(java.lang.String,
496: * java.lang.String, java.lang.String, java.lang.Object)
497: */
498: public final Object pcmf_setParameter(String xSource,
499: String xCluster, String xKey, Object xValue) {
500: this .pdm_activeSource = (Map) this .pdm_sources.get(xSource);
501: this .pdm_activeCluster = (Map) this .pdm_activeSource
502: .get(xCluster);
503:
504: return (this .pdm_activeCluster.put(xKey, xValue));
505: };
506:
507: /*
508: * (non-Javadoc)
509: *
510: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_removeCluster(java.lang.String,
511: * java.lang.String)
512: */
513: public final Object pcmf_removeCluster(String xSource,
514: String xCluster) {
515: this .pdm_activeSource = (Map) this .pdm_sources.get(xSource);
516: Object l_rem = (Map) this .pdm_activeSource.remove(xCluster);
517: if (l_rem == this .pdm_activeCluster)
518: this .pdm_activeCluster = null;
519:
520: return (l_rem);
521: };
522:
523: /*
524: * (non-Javadoc)
525: *
526: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_addCluster(java.lang.String,
527: * java.lang.String)
528: */
529: public final Object pcmf_addCluster(String xSource, String xCluster) {
530: this .pdm_activeSource = (Map) this .pdm_sources.get(xSource);
531: this .pdm_activeCluster = (Map) this .pdm_activeSource
532: .get(xCluster);
533: if (this .pdm_activeCluster == null) {
534: this .pdm_activeCluster = new LinkedHashMap();
535: this .pdm_activeSource.put(xCluster, this .pdm_activeCluster);
536: }
537:
538: return (this .pdm_activeCluster);
539: };
540:
541: public final void pcmf_cleanCluster(String xSource, String xCluster) {
542: this .pdm_activeSource = (Map) this .pdm_sources.get(xSource);
543: this .pdm_activeCluster = (Map) this .pdm_activeSource.put(
544: xCluster, new LinkedHashMap());
545:
546: return;
547: };
548:
549: public final boolean pcmf_existsCluster(String xSource,
550: String xCluster) {
551: this .pdm_activeSource = (Map) this .pdm_sources.get(xSource);
552: if (this .pdm_activeSource.get(xCluster) == null)
553: return (false);
554: else
555: return (true);
556: };
557:
558: /*
559: * (non-Javadoc)
560: *
561: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_getParameter(java.lang.String,
562: * java.lang.String)
563: */
564: public final Object pcmf_getParameter(String xCluster, String xKey) {
565: this .pdm_activeCluster = (Map) this .pdm_activeSource
566: .get(xCluster);
567: if (this .pdm_activeCluster == null)
568: return (null);
569:
570: return (this .pdm_activeCluster.get(xKey));
571: };
572:
573: /*
574: * (non-Javadoc)
575: *
576: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_setParameter(java.lang.String,
577: * java.lang.String, java.lang.Object)
578: */
579: public final Object pcmf_setParameter(String xCluster, String xKey,
580: Object xValue) {
581: this .pdm_activeCluster = (Map) this .pdm_activeSource
582: .get(xCluster);
583:
584: return (this .pdm_activeCluster.put(xKey, xValue));
585: };
586:
587: /*
588: * (non-Javadoc)
589: *
590: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_getParameter(java.lang.String)
591: */
592: public final Object pcmf_getParameter(String xKey) {
593: return (this .pdm_activeCluster.get(xKey));
594: };
595:
596: /*
597: * (non-Javadoc)
598: *
599: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_setParameter(java.lang.String,
600: * java.lang.Object)
601: */
602: public final Object pcmf_setParameter(String xKey, Object xValue) {
603: return (this .pdm_activeCluster.put(xKey, xValue));
604: };
605:
606: /*
607: * (non-Javadoc)
608: *
609: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_setRoot(java.lang.String,
610: * java.lang.String)
611: */
612: public final int pcmf_setRoot(String xSource, String xCluster) {
613: int l_ret = 0;
614:
615: this .pdm_activeSource = (Map) this .pdm_sources.get(xSource);
616:
617: if (xCluster != null) {
618: this .pdm_activeCluster = (Map) this .pdm_activeSource
619: .get(xCluster);
620: if (this .pdm_activeCluster == null)
621: l_ret = -1;
622: } else
623: l_ret = -1;
624:
625: return (l_ret);
626: };
627:
628: /*
629: * (non-Javadoc)
630: *
631: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_getKeys(java.lang.String)
632: */
633: public final Iterator pcmf_getKeys(String xCluster) {
634: return (new ArrayList(((Map) this .pdm_activeSource
635: .get(xCluster)).keySet()).iterator());
636: };
637:
638: /*
639: * (non-Javadoc)
640: *
641: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_getKeys(java.lang.String,
642: * java.lang.String)
643: */
644: public final Iterator pcmf_getKeys(String xSource, String xCluster) {
645: this .pdm_activeSource = (Map) this .pdm_sources.get(xSource);
646: return (new ArrayList(((Map) this .pdm_activeSource
647: .get(xCluster)).keySet()).iterator());
648: };
649:
650: /*
651: * (non-Javadoc)
652: *
653: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_getClusterKeys()
654: */
655: public final Iterator pcmf_getClusterKeys() {
656: return (new ArrayList(this .pdm_activeSource.keySet())
657: .iterator());
658: };
659:
660: /*
661: * (non-Javadoc)
662: *
663: * @see de.ug2t.connector.ICoXmlParameterGetter#pcmf_getClusterKeys(java.lang.String)
664: */
665: public final Iterator pcmf_getClusterKeys(String xSource) {
666: this .pdm_activeSource = (Map) this .pdm_sources.get(xSource);
667: return (new ArrayList(this.pdm_activeSource.keySet())
668: .iterator());
669: };
670: }
|