001: // @@
002: // @@
003: /*
004: * Wi.Ser Framework
005: *
006: * Version: 1.8.1, 20-September-2007
007: * Copyright (C) 2005 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.model.persistence;
032:
033: import java.io.*;
034: import java.util.*;
035:
036: import org.xml.sax.*;
037: import org.xml.sax.helpers.*;
038:
039: import com.bluecast.xml.*;
040:
041: import de.ug2t.kernel.*;
042: import de.ug2t.model.values.*;
043:
044: class MoDxrSAXEventhandler extends DefaultHandler {
045: private IMoValueContainer pem_tpl = null;
046: private IMoValueContainer pem_target = null;
047: private IMoValueContainer pem_default = null;
048:
049: private Stack pem_tplStack = new Stack();
050: private Stack pem_modStack = new Stack();
051:
052: private boolean pem_strict = true;
053: private boolean pem_cmpRoot = false;
054: private boolean pem_useLw = false;
055: private IMoReaderFilter pem_filter = null;
056:
057: public MoDxrSAXEventhandler(IMoValueContainer xTpl,
058: IMoValueContainer xTarget, boolean xStrict) {
059: this .pem_target = xTarget;
060: this .pem_tpl = xTpl;
061: this .pem_strict = xStrict;
062:
063: return;
064: };
065:
066: public void pcmf_useLw(boolean xUse) {
067: this .pem_useLw = xUse;
068: }
069:
070: public boolean pcmf_usesLw() {
071: return (this .pem_useLw);
072: }
073:
074: public void pcmf_setDefaultTpl(IMoValue xDef) {
075: this .pem_default = (IMoValueContainer) xDef;
076: }
077:
078: public void pcmf_setCmpRoot(boolean xCmpRoot) {
079: this .pem_cmpRoot = xCmpRoot;
080: }
081:
082: public MoDxrSAXEventhandler() {
083: return;
084: };
085:
086: public MoDxrSAXEventhandler(boolean xStrict) {
087: this .pem_strict = xStrict;
088:
089: return;
090: };
091:
092: protected void pcmf_setStrict(boolean xStrict) {
093: this .pem_strict = xStrict;
094: }
095:
096: public final void pcmf_init(IMoValueContainer xTpl,
097: IMoValueContainer xTarget, IMoReaderFilter xFilter) {
098: this .pem_tplStack.clear();
099: this .pem_modStack.clear();
100:
101: this .pem_target = xTarget;
102: this .pem_tpl = xTpl;
103: this .pem_filter = xFilter;
104:
105: return;
106: };
107:
108: public final void pcmf_init(IMoValueContainer xTarget,
109: IMoReaderFilter xFilter) {
110: this .pem_tplStack.clear();
111: this .pem_modStack.clear();
112:
113: this .pem_target = xTarget;
114: this .pem_filter = xFilter;
115:
116: return;
117: };
118:
119: public final void startElementStrict(String uri, String localName,
120: String qName, Attributes attributes) throws SAXException {
121: try {
122: IMoValue l_new = null;
123:
124: this .pem_tplStack.push(this .pem_tpl);
125: this .pem_modStack.push(this .pem_target);
126:
127: if (this .pem_cmpRoot == false
128: || this .pem_tplStack.size() != 1) {
129: l_new = this .pem_tpl
130: .pcmf_getNewModelValue(qName, false);
131:
132: this .pem_target = (IMoValueContainer) l_new;
133: this .pem_tpl = (IMoValueContainer) this .pem_tpl
134: .pcmf_getModelValue(qName);
135: } else
136: l_new = this .pem_target;
137:
138: if (l_new != null) {
139: int l_len = attributes.getLength();
140: String l_attname = null;
141: String l_attval = null;
142: for (int i = 0; i < l_len; i++) {
143: l_attname = attributes.getQName(i);
144: l_attval = attributes.getValue(l_attname);
145: l_new = this .pem_tpl.pcmf_getNewModelValue(
146: l_attname, false);
147: if (l_new != null) {
148: ((IMoSingleValue) l_new).pcmf_setValue(KeTools
149: .pcmf_deRef(l_attval));
150: ((IMoSingleValue) l_new).pcmf_validate();
151:
152: if (this .pem_filter != null
153: && this .pem_filter.pcmf_doInsert(
154: l_attname, l_new))
155: this .pem_target.pcmf_addValue(l_attname,
156: l_new);
157: else if (this .pem_filter == null)
158: this .pem_target.pcmf_addValue(l_attname,
159: l_new);
160: } else
161: KeLog.pcmf_log("ug2t",
162: "no matchting model-entry (single) found: "
163: + l_attname, this , KeLog.FATAL);
164: }
165: } else
166: KeLog.pcmf_log("ug2t",
167: "no matchting model-entry (multiple) found: "
168: + qName, this , KeLog.FATAL);
169: } catch (Exception e) {
170: KeLog.pcmf_log("ug2t",
171: "error during construction of model from template near node: "
172: + qName, this , KeLog.FATAL);
173: }
174: return;
175: };
176:
177: public final void startElementNonStrict(String uri,
178: String localName, String qName, Attributes attributes)
179: throws SAXException {
180: try {
181: IMoValue l_new = null;
182:
183: this .pem_tplStack.push(this .pem_tpl);
184: this .pem_modStack.push(this .pem_target);
185:
186: if (this .pem_cmpRoot == false
187: || this .pem_tplStack.size() != 1) {
188: l_new = this .pem_tpl
189: .pcmf_getNewModelValue(qName, false);
190: if (l_new == null) {
191: if (this .pem_default == null)
192: l_new = new MoRemotedValueTree(qName, null);
193: else {
194: l_new = this .pem_default
195: .pcmf_cloneModelValue(false);
196: this .pem_tpl = (IMoValueContainer) l_new;
197: }
198:
199: KeLog.pcmf_log("ug2t",
200: "no matchting model-entry (multiple) found: "
201: + qName, this , KeLog.MESSAGE);
202: } else
203: this .pem_tpl = (IMoValueContainer) this .pem_tpl
204: .pcmf_getModelValue(qName);
205:
206: this .pem_target = (IMoValueContainer) l_new;
207: } else
208: l_new = this .pem_target;
209:
210: int l_len = attributes.getLength();
211: String l_attname = null;
212: String l_attval = null;
213: for (int i = 0; i < l_len; i++) {
214: l_attname = attributes.getQName(i);
215: l_attval = attributes.getValue(l_attname);
216: l_new = this .pem_tpl.pcmf_getNewModelValue(l_attname,
217: false);
218: if (l_new == null) {
219: if (this .pem_useLw)
220: l_new = new MoLwSingleValue();
221: else
222: l_new = new MoSingleValue();
223:
224: ((IMoSingleValue) l_new).pcmf_setValue(KeTools
225: .pcmf_deRef(l_attval));
226: ((IMoSingleValue) l_new).pcmf_validate();
227:
228: if (this .pem_filter != null
229: && this .pem_filter.pcmf_doInsert(l_attname,
230: l_new))
231: this .pem_target.pcmf_addValue(l_attname, l_new);
232: else if (this .pem_filter == null)
233: this .pem_target.pcmf_addValue(l_attname, l_new);
234:
235: KeLog.pcmf_log("ug2t",
236: "no matchting model-entry (single) found: "
237: + l_attname, this , KeLog.MESSAGE);
238: } else {
239: ((IMoSingleValue) l_new).pcmf_setValue(KeTools
240: .pcmf_deRef(l_attval));
241: ((IMoSingleValue) l_new).pcmf_validate();
242:
243: if (this .pem_filter != null
244: && this .pem_filter.pcmf_doInsert(l_attname,
245: l_new))
246: this .pem_target.pcmf_addValue(l_attname, l_new);
247: else if (this .pem_filter == null)
248: this .pem_target.pcmf_addValue(l_attname, l_new);
249:
250: }
251: }
252: } catch (Exception e) {
253: KeLog.pcmf_logException("ug2t", this , e);
254: KeLog.pcmf_log("ug2t",
255: "error during construction of model from template near node: "
256: + qName, this , KeLog.ERROR);
257: }
258: return;
259: };
260:
261: public final void startElement(String uri, String localName,
262: String qName, Attributes attributes) throws SAXException {
263: if (this .pem_strict)
264: this .startElementStrict(uri, localName, qName, attributes);
265: else
266: this .startElementNonStrict(uri, localName, qName,
267: attributes);
268: }
269:
270: public final void endElement(String uri, String localName,
271: String qName) throws SAXException {
272: this .pem_tpl = (IMoValueContainer) this .pem_tplStack.pop();
273:
274: IMoValueContainer l_new = this .pem_target;
275: this .pem_target = (IMoValueContainer) this .pem_modStack.pop();
276:
277: if (l_new != this .pem_target) {
278: if (this .pem_filter != null
279: && this .pem_filter.pcmf_doInsert(qName, l_new))
280: this .pem_target.pcmf_addValue(qName, l_new);
281: else if (this .pem_filter == null)
282: this .pem_target.pcmf_addValue(qName, l_new);
283: }
284: return;
285: };
286: };
287:
288: /**
289: * @author Dirk
290: *
291: * date: 22.07.2003 project: JmcFrame_all
292: *
293: * <p>
294: * ...
295: * </p>
296: */
297: public final class MoXmlTemplateReader implements
298: IMoNonTransactionalReader {
299: private XMLReader pem_xmlReader = new Piccolo();
300: private MoDxrSAXEventhandler pem_handler = new MoDxrSAXEventhandler();
301: private String pem_file = null;
302: private byte[] pem_string = null;
303:
304: public void pcmf_useLw(boolean xUse) {
305: this .pem_handler.pcmf_useLw(xUse);
306: }
307:
308: public MoXmlTemplateReader(String xFile, boolean xStrict) {
309: this .pem_file = xFile;
310: pem_handler.pcmf_setStrict(xStrict);
311:
312: pem_xmlReader.setContentHandler(pem_handler);
313:
314: return;
315: };
316:
317: public MoXmlTemplateReader(byte[] xString, boolean xStrict) {
318: this .pem_string = xString;
319: pem_handler.pcmf_setStrict(xStrict);
320:
321: pem_xmlReader.setContentHandler(pem_handler);
322:
323: return;
324: };
325:
326: public MoXmlTemplateReader(String xFile, boolean xStrict,
327: boolean xCmpRoot) {
328: this .pem_file = xFile;
329: pem_handler.pcmf_setStrict(xStrict);
330: pem_handler.pcmf_setCmpRoot(xCmpRoot);
331:
332: pem_xmlReader.setContentHandler(pem_handler);
333:
334: return;
335: };
336:
337: public MoXmlTemplateReader(byte[] xString, boolean xStrict,
338: boolean xCmpRoot) {
339: this .pem_string = xString;
340: pem_handler.pcmf_setStrict(xStrict);
341: pem_handler.pcmf_setCmpRoot(xCmpRoot);
342:
343: pem_xmlReader.setContentHandler(pem_handler);
344:
345: return;
346: };
347:
348: public void pcmf_setDefaultTpl(IMoValue xDef) {
349: this .pem_handler.pcmf_setDefaultTpl(xDef);
350: ;
351: }
352:
353: public boolean pcmf_tplReadFiltered(IMoValue xModelTpl,
354: IMoValue xTarget, IMoReaderFilter xFilter) {
355: try {
356: pem_handler.pcmf_init((IMoValueContainer) xModelTpl,
357: (IMoValueContainer) xTarget, xFilter);
358: if (this .pem_file != null) {
359: BufferedInputStream l_stream = new BufferedInputStream(
360: new FileInputStream(KeEnvironment
361: .pcmf_buildPath(this .pem_file)));
362: InputSource l_source = new InputSource(l_stream);
363: l_source.setEncoding(KeEnvironment
364: .pcmf_getCharacterSet());
365: pem_xmlReader.parse(l_source);
366: l_stream.close();
367: } else if (this .pem_string != null) {
368: ByteArrayInputStream l_stream = new ByteArrayInputStream(
369: this .pem_string);
370: InputSource l_source = new InputSource(l_stream);
371: l_source.setEncoding(KeEnvironment
372: .pcmf_getCharacterSet());
373: pem_xmlReader.parse(l_source);
374: l_stream.close();
375: }
376: } catch (Exception e) {
377: KeLog.pcmf_log("ug2t", "error reading file: "
378: + this .pem_file, this , KeLog.FATAL);
379: KeLog.pcmf_logException("ug2t", this , e);
380:
381: return (false);
382: }
383: ;
384:
385: return (true);
386: }
387:
388: public final boolean pcmf_tplRead(IMoValue xModelTpl,
389: IMoValue xTarget) {
390: return (this .pcmf_tplReadFiltered(xModelTpl, xTarget, null));
391: }
392:
393: public boolean pcmf_tplReadFiltered(Object xId, IMoValue xModelTpl,
394: IMoValue xTarget, IMoReaderFilter xFilter) {
395: try {
396: pem_handler.pcmf_init((IMoValueContainer) xModelTpl,
397: (IMoValueContainer) xTarget, xFilter);
398:
399: BufferedInputStream l_stream = new BufferedInputStream(
400: new FileInputStream(KeEnvironment
401: .pcmf_buildPath(xId.toString())));
402: InputSource l_source = new InputSource(l_stream);
403: l_source.setEncoding(KeEnvironment.pcmf_getCharacterSet());
404: pem_xmlReader.parse(l_source);
405: l_stream.close();
406: } catch (Exception e) {
407: KeLog.pcmf_log("ug2t", "error reading file: "
408: + this .pem_file, this , KeLog.FATAL);
409: KeLog.pcmf_logException("ug2t", this , e);
410:
411: return (false);
412: }
413: ;
414:
415: return (true);
416: }
417:
418: public final boolean pcmf_tplRead(Object xId, IMoValue xModelTpl,
419: IMoValue xTarget) {
420: return (this
421: .pcmf_tplReadFiltered(xId, xModelTpl, xTarget, null));
422: }
423:
424: public boolean pcmf_readFiltered(IMoValue xTarget,
425: IMoReaderFilter xFilter) {
426: try {
427: pem_handler.pcmf_init((IMoValueContainer) xTarget, xFilter);
428: if (this .pem_file != null) {
429: BufferedInputStream l_stream = new BufferedInputStream(
430: new FileInputStream(KeEnvironment
431: .pcmf_buildPath(this .pem_file)));
432: InputSource l_source = new InputSource(l_stream);
433: l_source.setEncoding(KeEnvironment
434: .pcmf_getCharacterSet());
435: pem_xmlReader.parse(l_source);
436: l_stream.close();
437: } else if (this .pem_string != null) {
438: ByteArrayInputStream l_stream = new ByteArrayInputStream(
439: this .pem_string);
440: InputSource l_source = new InputSource(l_stream);
441: l_source.setEncoding(KeEnvironment
442: .pcmf_getCharacterSet());
443: pem_xmlReader.parse(l_source);
444: l_stream.close();
445: }
446: } catch (Exception e) {
447: KeLog.pcmf_log("ug2t", "error reading file: "
448: + this .pem_file, this , KeLog.FATAL);
449: KeLog.pcmf_logException("ug2t", this , e);
450:
451: return (false);
452: }
453: ;
454:
455: return (true);
456: }
457:
458: public final boolean pcmf_read(IMoValue xTarget) {
459: return (this .pcmf_readFiltered(xTarget, null));
460: }
461:
462: public IMoValue pcmf_tplReadFiltered(IMoValue xTpl,
463: IMoReaderFilter xFilter) {
464: try {
465: IMoValueContainer l_root = null;
466:
467: if (this .pem_handler.pcmf_usesLw())
468: l_root = new MoLwValueTree();
469: else
470: l_root = new MoValueMap();
471:
472: this .pcmf_tplReadFiltered(xTpl, l_root, xFilter);
473:
474: Iterator l_it = l_root.pcmf_getSubValueIt();
475: IMoValue l_ret = (IMoValue) l_it.next();
476:
477: l_it = l_root.pcmf_getSubValueNameIt();
478: l_root.pcmf_removeValue(l_it.next().toString());
479:
480: return (l_ret);
481: } catch (Exception e) {
482: KeLog.pcmf_log("ug2t", "error reading file: "
483: + this .pem_file, this , KeLog.FATAL);
484: KeLog.pcmf_logException("ug2t", this , e);
485: return (null);
486: }
487: }
488:
489: public final IMoValue pcmf_tplRead(IMoValue xTpl) {
490: return (this .pcmf_tplReadFiltered(xTpl, null));
491: }
492:
493: public IMoValue pcmf_readFiltered(IMoReaderFilter xFilter) {
494: try {
495: IMoValueContainer l_root = null;
496:
497: if (this .pem_handler.pcmf_usesLw())
498: l_root = new MoLwValueTree();
499: else
500: l_root = new MoValueMap();
501:
502: this .pcmf_tplReadFiltered(l_root, xFilter);
503:
504: Iterator l_it = l_root.pcmf_getSubValueIt();
505: IMoValue l_ret = (IMoValue) l_it.next();
506:
507: l_it = l_root.pcmf_getSubValueNameIt();
508: l_root.pcmf_removeValue(l_it.next().toString());
509:
510: return (l_ret);
511: } catch (Exception e) {
512: KeLog.pcmf_log("ug2t", "error reading file: "
513: + this .pem_file, this , KeLog.FATAL);
514: KeLog.pcmf_logException("ug2t", this , e);
515:
516: return (null);
517: }
518: }
519:
520: public final IMoValue pcmf_read() {
521: return (this.pcmf_readFiltered(null));
522: }
523: }
|