Source Code Cross Referenced for XmlPersistentObjectOutputStream.java in  » Database-ORM » ODAL » com » completex » objective » components » persistency » io » xml » impl » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » ODAL » com.completex.objective.components.persistency.io.xml.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         *  Objective Database Abstraction Layer (ODAL)
003:         *  Copyright (c) 2004, The ODAL Development Group
004:         *  All rights reserved.
005:         *  For definition of the ODAL Development Group please refer to LICENCE.txt file
006:         *
007:         *  Distributable under LGPL license.
008:         *  See terms of license at gnu.org.
009:         */package com.completex.objective.components.persistency.io.xml.impl;
010:
011:        import com.completex.objective.components.persistency.ColumnType;
012:        import com.completex.objective.components.persistency.Link;
013:        import com.completex.objective.components.persistency.MetaColumn;
014:        import com.completex.objective.components.persistency.PersistentEntry;
015:        import com.completex.objective.components.persistency.PersistentObject;
016:        import com.completex.objective.components.persistency.Record;
017:        import com.completex.objective.components.persistency.core.impl.LinkIterator;
018:        import com.completex.objective.components.persistency.io.PoObjectOutput;
019:        import com.completex.objective.components.persistency.key.SimpleNaturalKey;
020:        import com.completex.objective.components.persistency.key.impl.CompoundNaturalKeyImpl;
021:        import com.completex.objective.components.persistency.key.impl.SimpleNaturalKeyImpl;
022:        import com.completex.objective.components.persistency.type.CollectionTraceable;
023:        import com.completex.objective.components.persistency.type.CollectionTracer;
024:        import com.completex.objective.components.persistency.type.TracingCollection;
025:        import com.completex.objective.components.persistency.type.ValueStringResult;
026:        import com.completex.objective.components.persistency.type.ValueStreamHelper;
027:        import com.completex.objective.tools.generators.NameHelper;
028:        import com.completex.objective.util.TypeUtil;
029:        import com.completex.objective.util.XmlDomHelper;
030:        import org.w3c.dom.Document;
031:        import org.w3c.dom.Element;
032:        import org.w3c.dom.NodeList;
033:        import org.w3c.dom.Text;
034:        import org.xml.sax.SAXException;
035:
036:        import javax.xml.parsers.DocumentBuilder;
037:        import javax.xml.transform.OutputKeys;
038:        import javax.xml.transform.Transformer;
039:        import javax.xml.transform.TransformerException;
040:        import javax.xml.transform.TransformerFactory;
041:        import javax.xml.transform.dom.DOMSource;
042:        import javax.xml.transform.stream.StreamResult;
043:        import java.io.IOException;
044:        import java.io.OutputStream;
045:        import java.util.Collection;
046:        import java.util.Iterator;
047:
048:        /**
049:         * Implementation of PoObjectoutput interface. The stream is not thread safe. <br/>
050:         * Only <t>PersistentObject</t>s, their <t>Collection</t>s and basic Java types including the primitives,
051:         *  which are assumed "known", are serialized in readable format. All other ones are serialized as "binaries".
052:         * If the generated persistent object implements java.util.Extenalizable interface then all
053:         * extras in the inherited objects are to be written in <t>PersistentObject.writeExternalDescendant(ObjectOutput out)</t>
054:         * and read reapecitivey in <t>PersistentObject.readExternalDescendant(ObjectInput in)</t>
055:         * method. 
056:         * <br/>
057:         * References are guaranteed to be preserved for all "known" objects (see above) and not for "binaries".
058:         * Even though for the binary objects of the 1st level the references will be preserved, for the "internal" objects (
059:         * binaries inside binaries) references from/to external object will not be preserved. 
060:         * <p/>
061:         *
062:         * @author Gennady Krizhevsky
063:         */
064:        public class XmlPersistentObjectOutputStream extends XmlDomStream
065:                implements  PoObjectOutput, XmlStreamTags {
066:
067:            private OutputStream outputStream;
068:
069:            /**
070:             * Return true if persistent object column names are to be shown
071:             * 
072:             * @return true if persistent object column names are to be shown
073:             */
074:            public boolean isUseNames() {
075:                return useNames;
076:            }
077:
078:            /**
079:             * Set true if persistent object column names are to be shown
080:             * 
081:             * @param useNames true if persistent object column names are to be shown
082:             */
083:            public void setUseNames(boolean useNames) {
084:                this .useNames = useNames;
085:            }
086:
087:            /**
088:             * Set true if persistent object column names are to be shown in JavaBeans style
089:             * 
090:             * @param useNamesJavaStyle true if persistent object column names are to be shown in JavaBeans style
091:             */
092:            public void setUseNamesJavaStyle(boolean useNamesJavaStyle) {
093:                this .useNamesJavaStyle = useNamesJavaStyle;
094:            }
095:
096:            public boolean isUseReferences() {
097:                return useReferences;
098:            }
099:
100:            public void setUseReferences(boolean useReferences) {
101:                this .useReferences = useReferences;
102:            }
103:
104:            /**
105:             * @see PoObjectOutput#setPreserveOriginalValues(boolean)
106:             */
107:            public void setPreserveOriginalValues(boolean preserveOriginalValues) {
108:                this .preserveOriginalValues = preserveOriginalValues;
109:            }
110:
111:            public XmlPersistentObjectOutputStream(OutputStream outputStream)
112:                    throws IOException {
113:                this .outputStream = outputStream;
114:                setup();
115:            }
116:
117:            protected void setupRootElement() {
118:                rootElement = document.createElement(TAG_ODAL);
119:                rootElement.setAttribute(ATTR_VERSION, version);
120:            }
121:
122:            protected void init() {
123:                ensureOpen();
124:                if (!initialized) {
125:                    rootElement.setAttribute(
126:                            XmlStreamTags.TAG_PRESERVE_ORIGINAL_VALUES, String
127:                                    .valueOf(preserveOriginalValues));
128:                    initialized = true;
129:                }
130:            }
131:
132:            protected Document createDocument(DocumentBuilder builder)
133:                    throws IOException, SAXException {
134:                return builder.newDocument();
135:            }
136:
137:            /**
138:             * Only PersistentObjects, their Collections and basic Java types including the primitives are serialized
139:             * in readable format. All other ones are serialized as "binaries".
140:             * If the generated persistent object implements java.util.Extenalizable interface then all
141:             * extras in the inherited objects are to be written in PersistentObject.writeExternalDescendant(ObjectOutput out)
142:             * method. 
143:             * 
144:             * @see java.io.ObjectOutput#writeObject(Object)
145:             * @see PersistentObject#writeExternalDescendant(java.io.ObjectOutput out)
146:             */
147:            public void writeObject(Object obj) throws IOException {
148:                Element parentElement = resolveParentElement();
149:                init();
150:                writeObject(parentElement, obj);
151:            }
152:
153:            protected void writeObject(Element parentElement, Object obj)
154:                    throws IOException {
155:                if (obj instanceof  PersistentObject) {
156:                    writePersistentObject(parentElement, (PersistentObject) obj);
157:                } else if (obj instanceof  Collection) {
158:                    writeCollection(parentElement, ((Collection) obj));
159:                } else if (obj != null) {
160:                    writeExtType(parentElement, obj, null);
161:                } else {
162:                    // Object is null
163:                    addNull(parentElement);
164:                }
165:            }
166:
167:            protected void writeExtType(Element parentElement, Object obj,
168:                    Class primitiveClass) throws IOException {
169:                init();
170:                if (obj == null) {
171:                    addNull(parentElement);
172:                } else {
173:                    Element extTypeNode = createElement(parentElement,
174:                            TAG_EXT_TYPE);
175:                    boolean isReference = primitiveClass == null
176:                            && writeReferenceAttrs(extTypeNode, obj);
177:                    if (!isReference) {
178:                        String className = primitiveClass != null ? primitiveClass
179:                                .getName()
180:                                : obj.getClass().getName();
181:                        extTypeNode.setAttribute(ATTR_CLASS, className);
182:                        ValueStringResult result = value2string(obj);
183:                        addTextValue(extTypeNode, result.getValueString());
184:                    }
185:                }
186:            }
187:
188:            protected void addNull(Element extTypeNode) {
189:                xmlDomHelper.createElement(extTypeNode, TAG_NULL);
190:            }
191:
192:            protected Element createElement(Element parentElement,
193:                    String tagName) {
194:                return xmlDomHelper.createElement(parentElement, tagName);
195:            }
196:
197:            protected Element createElement(String tagName) {
198:                return xmlDomHelper.createElement(tagName);
199:            }
200:
201:            protected void writePersistentObject(Element parentElement,
202:                    PersistentObject persistent) throws IOException {
203:                if (persistent == null) {
204:                    return;
205:                }
206:                persistent.record2().setPreserveOriginalValues(
207:                        preserveOriginalValues);
208:                Element poNode = createElement(parentElement, TAG_PO);
209:                boolean isReference = writeReferenceAttrs(poNode, persistent);
210:                if (!isReference) {
211:                    poNode.setAttribute(ATTR_CLASS, persistent.getClass()
212:                            .getName());
213:                    writeRecord(poNode, persistent);
214:                    writeExternalDescendant(poNode, persistent);
215:                    writeCompoundChildren(poNode, persistent);
216:                    writeComplexChildren(poNode, persistent);
217:                }
218:            }
219:
220:            protected void writeExternalDescendant(Element poNode,
221:                    PersistentObject persistent) throws IOException {
222:                Element descNode = createElement(TAG_DESCENDANT);
223:                currentElement = descNode;
224:                persistent.writeExternalDescendant(this );
225:                NodeList nodeList = descNode.getChildNodes();
226:                if (nodeList.getLength() > 0) {
227:                    poNode.appendChild(descNode);
228:                }
229:                currentElement = null;
230:            }
231:
232:            protected void writeComplexChildren(Element pElement,
233:                    PersistentObject persistent) throws IOException {
234:                if (persistent.complex() && persistent.hasChildren()) {
235:                    Element complexNode = document
236:                            .createElement(TAG_COMPLEX_CHILDREN);
237:
238:                    boolean hasNonNullLinks = false;
239:                    Link parentLink = persistent.toLink();
240:                    for (LinkIterator it = parentLink.linkIterator(); it
241:                            .hasNext();) {
242:                        Link link = it.nextLink();
243:                        hasNonNullLinks = hasNonNullLinks
244:                                || writeLink(link, complexNode);
245:                    }
246:
247:                    if (hasNonNullLinks) {
248:                        pElement.appendChild(complexNode);
249:                    }
250:                }
251:            }
252:
253:            protected boolean writeLink(Link link, Element cpxElements)
254:                    throws IOException {
255:                Object result = link.getResult();
256:                if (result == null) {
257:                    return false;
258:                }
259:
260:                Element cpxChildNode = createElement(cpxElements, TAG_LINK);
261:                cpxChildNode.setAttribute(ATTR_NAME, link.getName());
262:                cpxChildNode.setAttribute(ATTR_PATH, link.getPathString());
263:                cpxChildNode.setAttribute(ATTR_END_OF_CHAIN, String
264:                        .valueOf(link.isEndOfChain()));
265:
266:                if (result instanceof  Collection) {
267:                    writeCollection(cpxChildNode, ((Collection) result));
268:                } else if (result instanceof  PersistentObject) {
269:                    writePersistentObject(cpxChildNode,
270:                            ((PersistentObject) result));
271:                } else {
272:                    writeExtType(cpxChildNode, result, null);
273:                }
274:                return true;
275:            }
276:
277:            protected void writeCollection(Element complexElement,
278:                    Collection collection) throws IOException {
279:                init();
280:                if (collection.size() > 0) {
281:                    Element collectionNode = createElement(complexElement,
282:                            TAG_COLLECTION);
283:                    boolean isReference = writeReferenceAttrs(collectionNode,
284:                            collection);
285:                    if (!isReference) {
286:                        if (collection instanceof  TracingCollection) {
287:                            TracingCollection tracingCollection = (TracingCollection) collection;
288:                            collectionNode.setAttribute(ATTR_SAVE_TRACED,
289:                                    XmlDomHelper.b2S(tracingCollection
290:                                            .isSaveTraced()));
291:                        }
292:                        collectionNode.setAttribute(ATTR_CLASS, collection
293:                                .getClass().getName());
294:                        Element collectionElements = createElement(
295:                                collectionNode, TAG_ELEMENTS);
296:                        writeCollectionElements(collection, collectionElements);
297:                        writeTracer(collectionNode, collection);
298:                    }
299:                }
300:            }
301:
302:            protected void writeTracer(Element collectionNode,
303:                    Collection collection) throws IOException {
304:                if (collection instanceof  CollectionTraceable) {
305:                    CollectionTraceable traceable = (CollectionTraceable) collection;
306:                    CollectionTracer tracer = traceable.getCollectionTracer();
307:                    if (tracer.isTrace()) {
308:                        Element tracerNode = createElement(collectionNode,
309:                                TAG_TRACER);
310:                        tracerNode.setAttribute(ATTR_TRACE, XmlDomHelper
311:                                .b2S(tracer.isTrace()));
312:                        if (tracer.dirty()) {
313:                            CollectionTracer.ModifiedEntry[] modifiedEntries = tracer
314:                                    .getModifiedEntries();
315:                            for (int i = 0; i < modifiedEntries.length; i++) {
316:                                CollectionTracer.ModifiedEntry modifiedEntry = modifiedEntries[i];
317:                                writeTracerEntry(tracerNode, modifiedEntry);
318:                            }
319:                        }
320:                    }
321:                }
322:            }
323:
324:            protected void writeTracerEntry(Element tracerNode,
325:                    CollectionTracer.ModifiedEntry modifiedEntry)
326:                    throws IOException {
327:                if (modifiedEntry != null) {
328:                    Element modifiedEntryNode = createElement(tracerNode,
329:                            TAG_MODIFIED_ENTRY);
330:                    modifiedEntryNode.setAttribute(ATTR_OP, modifiedEntry
331:                            .getOperation().getType());
332:                    writeObject(modifiedEntryNode, modifiedEntry.getObject());
333:                }
334:            }
335:
336:            protected void writeKeyValue(Element keyNode, Object key)
337:                    throws IOException {
338:                boolean isReference = writeReferenceAttrs(keyNode, key);
339:                if (!isReference) {
340:                    if (key instanceof  SimpleNaturalKeyImpl) {
341:                        keyNode.setAttribute(ATTR_CLASS, key.getClass()
342:                                .getName());
343:                        SimpleNaturalKeyImpl naturalKey = (SimpleNaturalKeyImpl) key;
344:                        writeSimpleKey(keyNode, naturalKey);
345:                    } else if (key instanceof  CompoundNaturalKeyImpl) {
346:                        keyNode.setAttribute(ATTR_CLASS, key.getClass()
347:                                .getName());
348:                        CompoundNaturalKeyImpl naturalKey = (CompoundNaturalKeyImpl) key;
349:                        writeCompoundKey(keyNode, naturalKey);
350:                    } else {
351:                        writeExtType(keyNode, key, null);
352:                    }
353:                }
354:            }
355:
356:            protected void writeCompoundKey(Element keyNode,
357:                    CompoundNaturalKeyImpl naturalKey) throws IOException {
358:                keyNode.setAttribute(ATTR_OWNER, naturalKey.getClassName());
359:                SimpleNaturalKey[] naturalKeys = naturalKey.getNaturalKeys();
360:                for (int i = 0; i < naturalKeys.length; i++) {
361:                    Element keyEntryNode = createElement(keyNode, TAG_KEY_ENTRY);
362:                    SimpleNaturalKey key = naturalKeys[i];
363:                    writeKeyValue(keyEntryNode, key);
364:                }
365:            }
366:
367:            protected void writeSimpleKey(Element keyNode,
368:                    SimpleNaturalKeyImpl naturalKey) throws IOException {
369:                keyNode.setAttribute(ATTR_OWNER, naturalKey.getClassName());
370:                Object[] values = naturalKey.getValues();
371:                for (int i = 0; i < values.length; i++) {
372:                    Object value = values[i];
373:                    writeSimpleKeyValue(keyNode, value);
374:                }
375:            }
376:
377:            protected void writeSimpleKeyValue(Element valuesNode, Object value)
378:                    throws IOException {
379:                Element element = createElement(valuesNode, TAG_VALUE);
380:                element.setAttribute(ATTR_CLASS, value.getClass().getName());
381:                ValueStringResult valueStringResult = value2string(value);
382:                addTextValue(element, valueStringResult.getValueString());
383:            }
384:
385:            protected void writeCollectionElements(Object result,
386:                    Element collectionElements) throws IOException {
387:                Collection collection = (Collection) result;
388:                for (Iterator iterator = collection.iterator(); iterator
389:                        .hasNext();) {
390:                    Object obj = iterator.next();
391:                    writeObject(collectionElements, obj);
392:                }
393:            }
394:
395:            protected void writeCompoundChildren(Element pElement,
396:                    PersistentObject persistent) throws IOException {
397:                if (persistent.compound()) {
398:                    Element compoundElement = createElement(pElement,
399:                            TAG_COMPOUND_CHILDREN);
400:                    //
401:                    // Start from 1 to skip the PO itself:
402:                    //
403:                    for (int i = 1; i < persistent.compoundSize(); i++) {
404:                        PersistentObject persistentEntry = persistent
405:                                .compoundEntry(i);
406:                        writePersistentObject(compoundElement, persistentEntry);
407:                    }
408:                }
409:            }
410:
411:            protected void writeRecord(Element pElement,
412:                    PersistentObject persistent) {
413:                Element rElement = createElement(pElement, TAG_RECORD);
414:
415:                rElement.setAttribute(TAG_PRESERVE_ORIGINAL_VALUES, String
416:                        .valueOf(preserveOriginalValues));
417:                if (preserveOriginalValues) {
418:                    rElement.setAttribute(ATTR_STATE, persistent.record()
419:                            .getState().getName());
420:                }
421:
422:                Record record = persistent.record2();
423:                for (int i = 0; i < record.getTable().size(); i++) {
424:                    writeField(i, record, rElement);
425:                }
426:            }
427:
428:            protected void writeField(int index, Record record, Element rElement) {
429:                MetaColumn column = record.getColumn(index);
430:                ColumnType type = column.getType();
431:
432:                Object value = record.getObject(index);
433:                Object origValue = record.getOriginalObject(index);
434:                ValueStringResult result = value2string(rElement, type, value);
435:                ValueStringResult origResult = value2string(rElement, type,
436:                        origValue);
437:
438:                XmlValueStruct valueStruct = new XmlValueStruct(value,
439:                        origValue, result, origResult);
440:
441:                boolean skipField = skipField(record, result.getValueString(),
442:                        origResult.getValueString());
443:
444:                if (!skipField) {
445:                    writeField(index, record, rElement, valueStruct);
446:                }
447:            }
448:
449:            protected void writeField(int index, Record record,
450:                    Element rElement, XmlValueStruct valueStruct) {
451:                MetaColumn column = record.getColumn(index);
452:
453:                PersistentEntry entry = record.getEntry(index);
454:                Element fElement = document.createElement(TAG_FIELD);
455:                fElement.setAttribute(ATTR_IDX, String.valueOf(index));
456:                if (useNames || useNamesJavaStyle) {
457:                    String columnAlias = column.getColumnAlias();
458:                    if (useNamesJavaStyle) {
459:                        columnAlias = NameHelper.javaName(columnAlias, null,
460:                                null, false);
461:                        fElement.setAttribute(ATTR_NAME, columnAlias);
462:                    } else {
463:                        fElement.setAttribute(ATTR_NAME, columnAlias);
464:                    }
465:                }
466:
467:                writeDirty(entry, fElement);
468:                writeValue(valueStruct, fElement);
469:                writeOrigValue(record, valueStruct, fElement);
470:
471:                rElement.appendChild(fElement);
472:            }
473:
474:            protected void writeDirty(PersistentEntry entry, Element fElement) {
475:                if (entry.isDirty()) {
476:                    fElement.setAttribute(ATTR_DIRTY, TypeUtil.b2S(entry
477:                            .isDirty(), TypeUtil.TRUE_FALSE));
478:                }
479:            }
480:
481:            protected boolean skipField(Record record, String stringValue,
482:                    String stringOrigValue) {
483:                boolean skip1 = preserveOriginalValues
484:                        && (stringValue == null && stringOrigValue == null);
485:                boolean skip2 = (!preserveOriginalValues && stringValue == null);
486:                boolean skip3 = !record.isInitialized() && stringValue == null;
487:                return skip1 || skip2 || skip3;
488:            }
489:
490:            protected ValueStringResult value2string(Element element,
491:                    ColumnType type, Object value) {
492:                return ValueStreamHelper.value2string(type, value);
493:            }
494:
495:            protected ValueStringResult value2string(Object value)
496:                    throws IOException {
497:                return ValueStreamHelper.value2string(value);
498:            }
499:
500:            protected void writeValue(XmlValueStruct valueStruct,
501:                    Element fElement) {
502:                writeValue(TAG_VALUE, valueStruct.value,
503:                        valueStruct.valueStringResult, fElement);
504:            }
505:
506:            private void writeOrigValue(Record record,
507:                    XmlValueStruct valueStruct, Element fElement) {
508:                if (preserveOriginalValues
509:                        && record.isInitialized()
510:                        && PersistentEntry.different(valueStruct.origValue,
511:                                valueStruct.value)) {
512:                    writeValue(TAG_ORIG_VALUE, valueStruct.origValue,
513:                            valueStruct.origValueStringResult, fElement);
514:                }
515:            }
516:
517:            protected void writeValue(String tagName, Object value,
518:                    ValueStringResult result, Element fElement) {
519:                Element vElement;
520:                if (result.getValueString() != null) {
521:                    vElement = document.createElement(tagName);
522:                    boolean isReference = writeReferenceAttrs(vElement, value);
523:                    if (!isReference) {
524:                        addTextValue(vElement, result.getValueString());
525:                        if (result.isBinary()) {
526:                            vElement.setAttribute(ATTR_BINARY, BASE64);
527:                        }
528:                    }
529:                } else {
530:                    vElement = document.createElement(TAG_NULL);
531:                }
532:                fElement.appendChild(vElement);
533:            }
534:
535:            protected boolean writeReferenceAttrs(Element vElement, Object value) {
536:                boolean isReference = false;
537:                if (useReferences) {
538:                    //
539:                    // Write reference: 
540:                    //
541:                    Integer index = (Integer) references.get(value);
542:                    if (index == null) {
543:                        index = new Integer(references.size() + 1);
544:                        references.put(value, index);
545:                        vElement.setAttribute(ATTR_ID, index.toString());
546:                    } else {
547:                        vElement.setAttribute(ATTR_REFID, index.toString());
548:                        isReference = true;
549:                    }
550:                }
551:                return isReference;
552:            }
553:
554:            protected void addTextValue(Element vElement, String stringValue) {
555:                vElement.setNodeValue(stringValue);
556:                Text text = document.createTextNode(TAG_TEXT);
557:                text.setNodeValue(stringValue);
558:                vElement.appendChild(text);
559:            }
560:
561:            public void write(int b) throws IOException {
562:                writeByte(b);
563:            }
564:
565:            public void write(byte b[]) throws IOException {
566:                throw new UnsupportedOperationException(
567:                        "write(byte b[]) - use writeObject(...) instead");
568:            }
569:
570:            public void write(byte b[], int off, int len) throws IOException {
571:                throw new UnsupportedOperationException(
572:                        "write(byte b[]) - use writeObject(...) instead");
573:            }
574:
575:            public void flush() throws IOException {
576:                println("flush()");
577:                try {
578:                    flush(this .outputStream);
579:                } catch (TransformerException e) {
580:                    throw new IOException(e.toString());
581:                }
582:            }
583:
584:            public void flush(OutputStream outputStream)
585:                    throws TransformerException, IOException {
586:                TransformerFactory transformerFactory = TransformerFactory
587:                        .newInstance();
588:                Transformer tr = transformerFactory.newTransformer();
589:                tr.setOutputProperty(OutputKeys.METHOD, "xml");
590:                tr.setOutputProperty(OutputKeys.INDENT, "yes");
591:                tr.setOutputProperty(
592:                        "{http://xml.apache.org/xslt}indent-amount", "3");
593:                tr.transform(new DOMSource(rootElement), new StreamResult(
594:                        outputStream));
595:                outputStream.flush();
596:            }
597:
598:            public void close() throws IOException {
599:                super .close();
600:                outputStream.close();
601:            }
602:
603:            public void writeBoolean(boolean v) throws IOException {
604:                println("writeBoolean(boolean v)");
605:                Element parentElement = resolveParentElement();
606:                writeExtType(parentElement, Boolean.valueOf(v), boolean.class);
607:            }
608:
609:            public void writeByte(int v) throws IOException {
610:                println("writeByte()");
611:                Element parentElement = resolveParentElement();
612:                writeExtType(parentElement, new Byte(((byte) v)), byte.class);
613:            }
614:
615:            public void writeShort(int v) throws IOException {
616:                println("writeShort()");
617:                Element parentElement = resolveParentElement();
618:                writeExtType(parentElement, new Short(((short) v)), short.class);
619:            }
620:
621:            public void writeChar(int v) throws IOException {
622:                println("writeChar()");
623:                writeUTF(new String(new char[] { ((char) v) }));
624:            }
625:
626:            public void writeInt(int v) throws IOException {
627:                println("writeInt()");
628:                Element parentElement = resolveParentElement();
629:                writeExtType(parentElement, new Integer(v), int.class);
630:            }
631:
632:            public void writeLong(long v) throws IOException {
633:                println("writeLong()");
634:                Element parentElement = resolveParentElement();
635:                writeExtType(parentElement, new Long(v), long.class);
636:            }
637:
638:            public void writeFloat(float v) throws IOException {
639:                println("writeFloat()");
640:                Element parentElement = resolveParentElement();
641:                writeExtType(parentElement, new Float(v), float.class);
642:            }
643:
644:            public void writeDouble(double v) throws IOException {
645:                println("writeLong()");
646:                Element parentElement = resolveParentElement();
647:                writeExtType(parentElement, new Double(v), double.class);
648:            }
649:
650:            public void writeBytes(String s) throws IOException {
651:                println("writeBytes()");
652:                byte[] bytes = s == null ? null : s.getBytes();
653:                Element parentElement = resolveParentElement();
654:                writeExtType(parentElement, bytes, null);
655:            }
656:
657:            public void writeChars(String s) throws IOException {
658:                println("writeChars()");
659:                writeUTF(s);
660:            }
661:
662:            public void writeUTF(String str) throws IOException {
663:                println("writeUTF()");
664:                Element parentElement = resolveParentElement();
665:                writeExtType(parentElement, str, null);
666:            }
667:
668:            /**
669:             * Utility class
670:             */
671:            class XmlValueStruct {
672:                public Object value;
673:                public Object origValue;
674:                public ValueStringResult valueStringResult;
675:                public ValueStringResult origValueStringResult;
676:
677:                public XmlValueStruct() {
678:                }
679:
680:                public XmlValueStruct(Object value, Object origValue,
681:                        ValueStringResult valueStringResult,
682:                        ValueStringResult origValueStringResult) {
683:                    this .value = value;
684:                    this .origValue = origValue;
685:                    this .valueStringResult = valueStringResult;
686:                    this .origValueStringResult = origValueStringResult;
687:                }
688:
689:                public Object getValue() {
690:                    return value;
691:                }
692:
693:                public void setValue(Object value) {
694:                    this .value = value;
695:                }
696:
697:                public Object getOrigValue() {
698:                    return origValue;
699:                }
700:
701:                public void setOrigValue(Object origValue) {
702:                    this .origValue = origValue;
703:                }
704:
705:                public ValueStringResult getValueStringResult() {
706:                    return valueStringResult;
707:                }
708:
709:                public void setValueStringResult(
710:                        ValueStringResult valueStringResult) {
711:                    this .valueStringResult = valueStringResult;
712:                }
713:
714:                public ValueStringResult getOrigValueStringResult() {
715:                    return origValueStringResult;
716:                }
717:
718:                public void setOrigValueStringResult(
719:                        ValueStringResult origValueStringResult) {
720:                    this.origValueStringResult = origValueStringResult;
721:                }
722:            }
723:
724:        }
w__w_w_.__j___a__va__2_s.c_o_m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.