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.nio.*;
035: import java.util.*;
036:
037: import de.ug2t.kernel.*;
038: import de.ug2t.model.values.*;
039: import de.ug2t.xmlScript.*;
040:
041: public class MoXmlWriter implements IMoTreeWriter {
042: private static final int IN_NODE = 0;
043: private static final int OUT_NODE = 1;
044:
045: private StringBuffer pem_buffer = null;
046: private OutputStream pem_oStream = null;
047: private int pem_lastAction = MoXmlWriter.OUT_NODE;
048: private Stack pem_actNode = new Stack();
049: private String pem_format = null;
050: private boolean pem_useTplNames = false;
051: private String pem_fileName = null;
052:
053: public MoXmlWriter(boolean xUseTplNames) {
054: this .pem_useTplNames = xUseTplNames;
055: return;
056: }
057:
058: public MoXmlWriter(OutputStream xStream, boolean xUseTplNames) {
059: this (xStream);
060: this .pem_useTplNames = xUseTplNames;
061: }
062:
063: public MoXmlWriter(String xFileName, boolean xUseTplNames) {
064: this (xFileName);
065: this .pem_useTplNames = xUseTplNames;
066: }
067:
068: public MoXmlWriter() {
069: return;
070: }
071:
072: public MoXmlWriter(OutputStream xStream) {
073: this .pem_oStream = xStream;
074: this .pem_buffer = new StringBuffer();
075: this .pem_buffer.append("<?xml version=\"1.0\" encoding=\""
076: + KeEnvironment.pcmf_getCharacterSet() + "\"?>");
077:
078: return;
079: }
080:
081: public MoXmlWriter(String xFileName) {
082: try {
083: this .pem_fileName = xFileName;
084: this .pem_buffer = new StringBuffer();
085: this .pem_buffer.append("<?xml version=\"1.0\" encoding=\""
086: + KeEnvironment.pcmf_getCharacterSet() + "\"?>");
087: } catch (Exception e) {
088: KeLog.pcmf_logException("ug2t", this , e);
089: }
090: ;
091:
092: return;
093: }
094:
095: public void pcmf_format(String xFormat) {
096: this .pem_format = xFormat;
097: }
098:
099: /**
100: * <p>
101: * Does...
102: * </p>
103: * <p>
104: *
105: * @return a Type with
106: * </p>
107: * <p>
108: * @param
109: * </p>
110: */
111: public final boolean pcmf_writeModel(IMoValue xModel) {
112: if (this .pem_useTplNames)
113: this .pcmf_addNode(xModel.pcmf_getMyTemplate(), xModel);
114: else
115: this .pcmf_addNode("model", xModel);
116:
117: this .pcmf_commitModel();
118:
119: return (true);
120: }
121:
122: /**
123: * <p>
124: * Does...
125: * </p>
126: * <p>
127: *
128: * @return a Type with
129: * </p>
130: * <p>
131: * @param
132: * </p>
133: */
134: public final boolean pcmf_writeModel(Object xId, IMoValue xModel) {
135: this .pcmf_addNode(xId.toString(), xModel);
136: this .pcmf_commitModel();
137:
138: return (true);
139: }
140:
141: /**
142: * <p>
143: * Does...
144: * </p>
145: * <p>
146: *
147: * @return a Type with
148: * </p>
149: * <p>
150: * @param
151: * </p>
152: */
153: public final void pcmf_addAttribute(String xName, IMoValue xValue) {
154: if (this .pem_lastAction == MoXmlWriter.OUT_NODE) {
155: KeLog.pcmf_log("ug2t", "wrong writer state, not in node",
156: this , KeLog.FATAL);
157: return;
158: }
159:
160: this .pem_buffer.append(" ");
161: this .pem_buffer.append(ScXmlScript.pcmf_filterSigns(xName));
162: this .pem_buffer.append("=\"");
163: this .pem_buffer.append(ScXmlScript.pcmf_filterSigns(xValue
164: .toString()));
165: this .pem_buffer.append("\"");
166:
167: this .pem_lastAction = MoXmlWriter.IN_NODE;
168:
169: return;
170: }
171:
172: /**
173: * <p>
174: * Does...
175: * </p>
176: * <p>
177: *
178: * @return a Type with
179: * </p>
180: * <p>
181: * @param
182: * </p>
183: */
184: public final void pcmf_addNode(String xName, IMoValue xValue) {
185: if (this .pem_lastAction == MoXmlWriter.IN_NODE)
186: this .pem_buffer.append(">\n");
187:
188: if (this .pem_format != null) {
189: int l_size = this .pem_actNode.size();
190: for (int i = 0; i < l_size; i++) {
191: this .pem_buffer.append(this .pem_format);
192: }
193: }
194:
195: this .pem_buffer.append("<");
196: this .pem_buffer.append(ScXmlScript.pcmf_filterSigns(xName));
197:
198: this .pem_lastAction = MoXmlWriter.IN_NODE;
199: this .pem_actNode.push(xName);
200:
201: // Attribute und Kinder schreiben
202: if (xValue instanceof IMoValueContainer) {
203: IMoValueContainer l_mod = (IMoValueContainer) xValue;
204: Iterator l_itn = l_mod.pcmf_getSubValueNameIt();
205: Iterator l_itv = l_mod.pcmf_getSubValueIt();
206: ArrayList l_subs = new ArrayList();
207: ArrayList l_subv = new ArrayList();
208: IMoValue l_value = null;
209: String l_key = null;
210:
211: while (l_itn.hasNext()) {
212: l_key = (String) l_itn.next();
213: l_value = (IMoValue) l_itv.next();
214: if (l_value instanceof IMoSingleValue) {
215: if (this .pem_useTplNames)
216: this .pcmf_addAttribute(l_value
217: .pcmf_getMyTemplate(), l_value);
218: else
219: this .pcmf_addAttribute(l_key, l_value);
220: } else {
221: l_subs.add(l_key);
222: l_subv.add(l_value);
223: }
224: }
225: ;
226:
227: l_itn = l_subs.iterator();
228: l_itv = l_subv.iterator();
229: while (l_itn.hasNext()) {
230: l_key = (String) l_itn.next();
231: l_value = (IMoValue) l_itv.next();
232: if (l_value instanceof IMoValueContainer) {
233: if (this .pem_useTplNames)
234: this .pcmf_addNode(l_value.pcmf_getMyTemplate(),
235: l_value);
236: else
237: this .pcmf_addNode(l_key, l_value);
238: } else
239: KeLog.pcmf_log("ug2t", "no model interface found",
240: this , KeLog.FATAL);
241: }
242: ;
243: }
244: ;
245:
246: this .pcmf_closeNode();
247:
248: return;
249: }
250:
251: /**
252: * <p>
253: * Does...
254: * </p>
255: * <p>
256: *
257: * @return a Type with
258: * </p>
259: * <p>
260: * @param
261: * </p>
262: */
263: public final void pcmf_closeNode() {
264: if (this .pem_lastAction == MoXmlWriter.IN_NODE)
265: this .pem_buffer.append(">\n");
266:
267: if (this .pem_format != null) {
268: int l_size = this .pem_actNode.size() - 1;
269: for (int i = 0; i < l_size; i++) {
270: this .pem_buffer.append(this .pem_format);
271: }
272: }
273:
274: this .pem_buffer.append("</");
275: this .pem_buffer.append(ScXmlScript
276: .pcmf_filterSigns(this .pem_actNode.pop().toString()));
277: this .pem_buffer.append(">\n");
278:
279: this .pem_lastAction = MoXmlWriter.OUT_NODE;
280: }
281:
282: /**
283: * <p>
284: * Does...
285: * </p>
286: * <p>
287: *
288: * @return a Type with
289: * </p>
290: * <p>
291: * @param
292: * </p>
293: */
294: public final IMoValue pcmf_removeNode() {
295: throw (new UnsupportedOperationException());
296: }
297:
298: /**
299: * <p>
300: * Does...
301: * </p>
302: * <p>
303: *
304: * @return a Type with
305: * </p>
306: * <p>
307: * @param
308: * </p>
309: */
310: public final boolean pcmf_beginModel() {
311: return (this .pcmf_beginModel(this .toString() + this .hashCode()));
312: }
313:
314: /**
315: * <p>
316: * Does...
317: * </p>
318: * <p>
319: *
320: * @return a Type with
321: * </p>
322: * <p>
323: * @param
324: * </p>
325: */
326: public final boolean pcmf_beginModel(Object xId) {
327: try {
328: this .pem_oStream = (new BufferedOutputStream(
329: new FileOutputStream(KeEnvironment
330: .pcmf_getRootDir()
331: + xId.toString())));
332: ;
333: this .pem_buffer = new StringBuffer();
334: this .pem_buffer
335: .append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
336: } catch (Exception e) {
337: KeLog.pcmf_logException("ug2t", this , e);
338: return (false);
339: }
340:
341: this .pem_lastAction = MoXmlWriter.OUT_NODE;
342:
343: return (true);
344: }
345:
346: /**
347: * <p>
348: * Does...
349: * </p>
350: * <p>
351: *
352: * @return a Type with
353: * </p>
354: * <p>
355: * @param
356: * </p>
357: */
358: public final void pcmf_clear() {
359: try {
360: this .pem_buffer = new StringBuffer();
361: this .pem_buffer
362: .append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
363:
364: this .pem_lastAction = MoXmlWriter.OUT_NODE;
365: this .pem_oStream.close();
366: } catch (Exception e) {
367: KeLog.pcmf_logException("ug2t", this , e);
368: }
369:
370: return;
371: }
372:
373: /**
374: * <p>
375: * Does...
376: * </p>
377: * <p>
378: *
379: * @return a Type with
380: * </p>
381: * <p>
382: * @param
383: * </p>
384: */
385: public final boolean pcmf_commitModel() {
386: try {
387: if (this .pem_oStream == null && this .pem_fileName != null)
388: this .pem_oStream = (new BufferedOutputStream(
389: new FileOutputStream(KeEnvironment
390: .pcmf_buildPath(this .pem_fileName))));
391:
392: CharBuffer l_buffer = CharBuffer.wrap(this .pem_buffer
393: .toString().toCharArray());
394: ByteBuffer l_bytes = (KeEnvironment
395: .pcmf_getCharacterSetObject().encode(l_buffer));
396: this .pem_oStream.write(l_bytes.array());
397: this .pem_oStream.flush();
398: } catch (Exception e) {
399: KeLog.pcmf_logException("ug2t", this , e);
400: return (false);
401: } finally {
402: this .pcmf_clear();
403: }
404:
405: return false;
406: }
407:
408: /**
409: * <p>
410: * Does...
411: * </p>
412: * <p>
413: *
414: * @return a Type with
415: * </p>
416: * <p>
417: * @param
418: * </p>
419: */
420: public final boolean pcmf_setCurser(String xLocation) {
421: throw (new UnsupportedOperationException());
422: }
423:
424: protected void finalize() throws Throwable {
425: if (this.pem_oStream != null)
426: this.pem_oStream.close();
427:
428: super.finalize();
429: }
430: }
|