KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencrx > kernel > layer > application > Xml


1 /*
2  * ====================================================================
3  * Project: opencrx, http://www.opencrx.org/
4  * Name: $Id: Xml.java,v 1.21 2006/03/31 00:15:18 wfro Exp $
5  * Description: Xml
6  * Revision: $Revision: 1.21 $
7  * Owner: CRIXP AG, Switzerland, http://www.crixp.com
8  * Date: $Date: 2006/03/31 00:15:18 $
9  * ====================================================================
10  *
11  * This software is published under the BSD license
12  * as listed below.
13  *
14  * Copyright (c) 2004-2005, CRIXP Corp., Switzerland
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  *
24  * * Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  *
29  * * Neither the name of CRIXP Corp. nor the names of the contributors
30  * to openCRX may be used to endorse or promote products derived
31  * from this software without specific prior written permission
32  *
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * ------------------
49  *
50  * This product includes software developed by the Apache Software
51  * Foundation (http://www.apache.org/).
52  *
53  * This product includes software developed by contributors to
54  * openMDX (http://www.openmdx.org/)
55  */

56 package org.opencrx.kernel.layer.application;
57
58 import java.beans.XMLDecoder JavaDoc;
59 import java.io.ByteArrayInputStream JavaDoc;
60 import java.io.ByteArrayOutputStream JavaDoc;
61 import java.io.IOException JavaDoc;
62 import java.io.InputStream JavaDoc;
63 import java.io.OutputStream JavaDoc;
64 import java.io.OutputStreamWriter JavaDoc;
65 import java.io.PrintStream JavaDoc;
66 import java.io.PrintWriter JavaDoc;
67 import java.util.ArrayList JavaDoc;
68 import java.util.Arrays JavaDoc;
69 import java.util.HashMap JavaDoc;
70 import java.util.HashSet JavaDoc;
71 import java.util.Iterator JavaDoc;
72 import java.util.List JavaDoc;
73 import java.util.Map JavaDoc;
74 import java.util.Set JavaDoc;
75 import java.util.StringTokenizer JavaDoc;
76 import java.util.TreeSet JavaDoc;
77 import java.util.Map.Entry;
78 import java.util.zip.ZipEntry JavaDoc;
79 import java.util.zip.ZipOutputStream JavaDoc;
80
81 import org.openmdx.application.log.AppLog;
82 import org.openmdx.base.exception.ServiceException;
83 import org.openmdx.base.query.Filter;
84 import org.openmdx.base.text.conversion.Base64;
85 import org.openmdx.compatibility.base.dataprovider.cci.DataproviderObject;
86 import org.openmdx.compatibility.base.dataprovider.cci.DataproviderObject_1_0;
87 import org.openmdx.compatibility.base.dataprovider.cci.RequestCollection;
88 import org.openmdx.compatibility.base.dataprovider.cci.ServiceHeader;
89 import org.openmdx.compatibility.base.dataprovider.cci.SystemAttributes;
90 import org.openmdx.compatibility.base.dataprovider.exporter.TraversalHandler;
91 import org.openmdx.compatibility.base.dataprovider.exporter.XMLExportHandler;
92 import org.openmdx.compatibility.base.dataprovider.exporter.XmlContentHandler;
93 import org.openmdx.compatibility.base.dataprovider.exporter.XmlExporter;
94 import org.openmdx.compatibility.base.dataprovider.importer.xml.XmlImporter;
95 import org.openmdx.compatibility.base.naming.Path;
96 import org.openmdx.kernel.exception.BasicException;
97 import org.openmdx.model1.accessor.basic.cci.ModelElement_1_0;
98 import org.openmdx.model1.accessor.basic.cci.Model_1_0;
99 import org.openmdx.model1.code.Multiplicities;
100 import org.xml.sax.SAXException JavaDoc;
101 import org.xml.sax.SAXParseException JavaDoc;
102
103 public class Xml {
104
105     //-------------------------------------------------------------------------
106
class Importer extends XmlImporter {
107       
108         public Importer(
109             RequestCollection target,
110             RequestCollection reader,
111             List JavaDoc errors,
112             List JavaDoc report
113         ) throws ServiceException {
114             super(target, reader, false, true);
115             this.errors = errors;
116             this.report = report;
117         }
118
119         private void addError(
120             String JavaDoc message
121         ) {
122             if(this.errors.size() < 5) {
123                 this.errors.add(message);
124             }
125         }
126             
127         private void addError(
128             SAXParseException JavaDoc e
129         ) {
130             this.addError("[" + this.getLocationString(e) + "]: " + e.getMessage());
131         }
132             
133         public void beginObject(
134             DataproviderObject object,
135             String JavaDoc operation
136         ) throws ServiceException {
137             if(
138                 (object.path().size() > 5) &&
139                 ((this.mainObject == null) || (object.path().size() < this.mainObject.path().size()))
140             ) {
141                 this.mainObject = object;
142             }
143         }
144     
145         public void endObject(
146             DataproviderObject object,
147             String JavaDoc operation
148         ) throws ServiceException {
149             try {
150                 if(!"null".equals(operation)) {
151                     try {
152                         // verify object to be replaced
153
DataproviderObject test = new DataproviderObject(
154                             Xml.this.plugin.retrieveObjectForModification(object.path())
155                         );
156                         test.addClones(
157                             object,
158                             true
159                         );
160                         Xml.this.model.verifyObject(
161                             test,
162                             test.values(SystemAttributes.OBJECT_CLASS).get(0),
163                             Multiplicities.SINGLE_VALUE,
164                             "create".equals(operation)
165                         );
166                         
167                         // replace
168
Xml.this.removeForeignAndDerivedAttributes(object);
169                         Xml.this.plugin.retrieveObjectForModification(object.path()).addClones(
170                             object,
171                             true
172                         );
173                     } catch(Exception JavaDoc e) {
174                         Xml.this.model.verifyObject(
175                             object,
176                             object.values(SystemAttributes.OBJECT_CLASS).get(0),
177                             Multiplicities.SINGLE_VALUE,
178                             "create".equals(operation)
179                         );
180                         Xml.this.removeForeignAndDerivedAttributes(object);
181                         Xml.this.target.addCreateRequest(object);
182                     }
183                     this.report.add(operation);
184                 }
185             }
186             catch(ServiceException e) {
187                 e.log(); // log for administrators
188
this.addError(e.getMessage() + " at object " + object.path());
189             }
190         }
191         
192         public void error(
193           SAXParseException JavaDoc e
194         ) throws SAXException JavaDoc {
195             this.addError(e);
196             super.error(e);
197         }
198
199         public void fatalError(
200             SAXParseException JavaDoc e
201         ) throws SAXException JavaDoc {
202             this.addError(e);
203             super.fatalError(e);
204         }
205
206         public void warning(
207             SAXParseException JavaDoc e
208         ) {
209             this.report.add(e.getMessage());
210             super.warning(e);
211         }
212         
213         public void process(
214             InputStream JavaDoc[] is
215         ) {
216             try {
217                 super.process(is);
218             }
219             catch(ServiceException e) {
220                 BasicException be = e.getStackedException(0);
221                 this.errors.add(be.getDescription() + " (" + Arrays.asList(be.getParameters()) + ")");
222             }
223         }
224         
225         public DataproviderObject getMainObject(
226         ) {
227           return this.mainObject;
228         }
229         
230         private DataproviderObject mainObject = null;
231         private final List JavaDoc errors;
232         private final List JavaDoc report;
233     }
234
235     //-------------------------------------------------------------------------
236
class Exporter extends XmlExporter {
237         
238         //---------------------------------------------------------------------
239
public Exporter(
240             ServiceHeader header,
241             RequestCollection reader,
242             PrintStream JavaDoc exportStream,
243             Model_1_0 model,
244             Map JavaDoc exportedCodes
245         ) {
246             super(
247                 header,
248                 reader,
249                 exportStream,
250                 model,
251                 "UTF-8"
252             );
253             this.referencedObjects = new HashSet JavaDoc();
254             this.exportedObjects = new HashSet JavaDoc();
255             this.exportedCodes = exportedCodes;
256         }
257         
258         public Set JavaDoc getReferencedObjects(
259         ) {
260             return this.referencedObjects;
261         }
262         
263         public Set JavaDoc getExportedObjects(
264         ) {
265             return this.exportedObjects;
266         }
267         
268         void updateReferencedObjects(
269             DataproviderObject_1_0 object
270         ) {
271             Xml.this.plugin.collectReferencedObjects(
272                 object,
273                 Xml.this.referenceFilter,
274                 this.referencedObjects
275             );
276         }
277        
278         protected TraversalHandler setupTraversalHandler(
279         ) throws ServiceException {
280             this.setupContentHandler(
281                 this.exportStream
282             );
283             TaggingXMLExportHandler exportHandler = new TaggingXMLExportHandler(
284                 this.model,
285                 "http://www.w3.org/2001/XMLSchema-instance",
286                 this.schemaString,
287                 this.exportedCodes
288             );
289             exportHandler.setContentHandler(
290                 this.setupContentHandler(this.exportStream)
291             );
292             return exportHandler;
293         }
294         
295         protected XmlContentHandler setupContentHandler(
296             PrintStream JavaDoc target
297         ) throws ServiceException {
298             return new NonClosingXmlContentHandler(
299                 target
300             );
301         }
302
303         //---------------------------------------------------------------------
304
public void export(
305             List JavaDoc startPoints,
306             List JavaDoc referenceFilters,
307             Map JavaDoc attributeFilters,
308             String JavaDoc schemaString,
309             int maxObjects
310         ) throws ServiceException {
311             super.export(
312                 startPoints,
313                 referenceFilters,
314                 attributeFilters,
315                 schemaString
316             );
317         }
318
319         //---------------------------------------------------------------------
320
class TaggingXMLExportHandler
321             extends XMLExportHandler {
322          
323             public TaggingXMLExportHandler(
324                 Model_1_0 model,
325                 String JavaDoc schemaInstance,
326                 String JavaDoc schemaLocation,
327                 Map JavaDoc exportedCodes
328             ) {
329                 super(model, schemaInstance, schemaLocation);
330                 this.exportedCodes = exportedCodes;
331             }
332
333             /**
334              * Test all attributes of object to be a coded value, i.e. configured
335              * as member of a code value container. If yes, add the attributes to
336              * a map of tags which contains a hint of the corresponding code value
337              * container. The hints are streamed as XML comments.
338              */

339             public Map JavaDoc getAttributeTags(
340                 DataproviderObject_1_0 object
341             ) throws ServiceException {
342                 Map JavaDoc attributeTags = new HashMap JavaDoc();
343                 String JavaDoc objectClassName = (String JavaDoc)object.values(SystemAttributes.OBJECT_CLASS).get(0);
344                 if(objectClassName != null) {
345                     ModelElement_1_0 objectClass = this.model.getElement(objectClassName);
346                     if(objectClass != null) {
347                         for(Iterator JavaDoc i = object.attributeNames().iterator(); i.hasNext(); ) {
348                             String JavaDoc attributeName = (String JavaDoc)i.next();
349                             // try to find a code value container for attribute
350
for(Iterator JavaDoc j = objectClass.values("allSupertype").iterator(); j.hasNext(); ) {
351                                 ModelElement_1_0 supertype = this.model.getElement(j.next());
352                                 String JavaDoc qualifiedClassName = (String JavaDoc)supertype.values("qualifiedName").get(0);
353                                 String JavaDoc qualifiedAttributeName = qualifiedClassName + ":" + attributeName;
354                                 DataproviderObject_1_0 codeValueContainer = Xml.this.plugin.getCodes().getCodeValueContainerByName(qualifiedAttributeName);
355                                 // if found add hint with reference to corresponding code value container to tags.
356
if(codeValueContainer != null) {
357                                     String JavaDoc codeValueContainerName = codeValueContainer.path().getBase();
358                                     attributeTags.put(
359                                         attributeName,
360                                         "@see CodeValueContainer name=\"" + codeValueContainerName + "\""
361                                     );
362                                     // remember exported codes
363
Set JavaDoc codes = null;
364                                     if((codes = (Set JavaDoc)this.exportedCodes.get(codeValueContainerName)) == null) {
365                                         this.exportedCodes.put(
366                                             codeValueContainerName,
367                                             codes = new HashSet JavaDoc()
368                                         );
369                                     }
370                                     // add all codes as Integer
371
try {
372                                         for(Iterator JavaDoc k = object.values(attributeName).iterator(); k.hasNext(); ) {
373                                             codes.add(new Integer JavaDoc(((Number JavaDoc)k.next()).intValue()));
374                                         }
375                                     }
376                                     catch(Exception JavaDoc e) {
377                                         AppLog.error("Exception occurred while retrieving codes for attribute", attributeName);
378                                         new ServiceException(e).log();
379                                     }
380                                     break;
381                                 }
382                             }
383                         }
384                     }
385                 }
386                 return attributeTags;
387             }
388        
389             public boolean featureComplete(
390                 DataproviderObject_1_0 object
391             ) throws ServiceException {
392                 Exporter.this.updateReferencedObjects(object);
393                 // skip dummy and egment-level objects
394
if((object.getValues(SystemAttributes.OBJECT_CLASS) != null) && (object.path().size() > 5)) {
395                     Exporter.this.exportedObjects.add(object.path());
396                 }
397                 return super.featureComplete(
398                     object
399                 );
400             }
401             
402             private final Map JavaDoc exportedCodes;
403         }
404         
405         //---------------------------------------------------------------------
406
class NonClosingXmlContentHandler
407             extends XmlContentHandler {
408
409             public NonClosingXmlContentHandler(
410                 PrintStream JavaDoc stream
411             ) {
412                 super(stream);
413             }
414                         
415             public void endDocument(
416             ) throws ServiceException {
417                 super.endDocument(false);
418             }
419         }
420         
421         int currentEntryId = 0;
422         
423         final Set JavaDoc referencedObjects;
424         final Set JavaDoc exportedObjects;
425         // map with CodeValueContainerName as key and List as value
426
// containing the exported codes
427
final Map JavaDoc exportedCodes;
428     }
429     
430     //-------------------------------------------------------------------------
431
static class NullOutputStream
432         extends OutputStream JavaDoc {
433
434         public NullOutputStream() {
435         }
436         public void close() throws IOException JavaDoc {
437         }
438         public void flush() throws IOException JavaDoc {
439         }
440         public void write(byte[] b, int off, int len) throws IOException JavaDoc {
441         }
442         public void write(byte[] b) throws IOException JavaDoc {
443         }
444         public void write(int b) throws IOException JavaDoc {
445         }
446     }
447     
448     //-------------------------------------------------------------------------
449
public Xml(
450         OpenCrxKernel_1 plugin,
451         RequestCollection target,
452         RequestCollection reader,
453         Model_1_0 model
454     ) {
455         this.plugin = plugin;
456         this.target = target;
457         this.reader = reader;
458         this.model = model;
459         this.header = null;
460     }
461
462     //-------------------------------------------------------------------------
463
public Xml(
464         OpenCrxKernel_1 plugin,
465         ServiceHeader header,
466         RequestCollection reader,
467         Model_1_0 model
468     ) {
469         this.plugin = plugin;
470         this.header = header;
471         this.model = model;
472         this.target = null;
473         this.reader = reader;
474     }
475
476     //---------------------------------------------------------------------------
477
void removeForeignAndDerivedAttributes(
478       DataproviderObject object
479     ) throws ServiceException {
480
481       // remove derived attributes but not SystemAttributes
482
ModelElement_1_0 typeDef = this.model.getElement(object.values(SystemAttributes.OBJECT_CLASS).get(0));
483       if(this.model.isClassType(typeDef)) {
484         Map JavaDoc attributeDefs = this.model.getAttributeDefs(
485           typeDef, false, true
486         );
487         for(
488           Iterator JavaDoc i = object.attributeNames().iterator();
489           i.hasNext();
490         ) {
491           boolean isDerived = false;
492           boolean isChangeable = true;
493           boolean isForeign = true;
494           
495           String JavaDoc featureName = (String JavaDoc)i.next();
496           
497           // ignore namespaces
498
if(featureName.indexOf(':') < 0) {
499             ModelElement_1_0 featureDef = (ModelElement_1_0) attributeDefs.get(featureName);
500             
501             if (featureDef != null) {
502                 isDerived =
503                   (featureDef.values("isDerived").size() > 0) &&
504                   ((Boolean JavaDoc)featureDef.values("isDerived").get(0)).booleanValue();
505                 isChangeable =
506                   (featureDef.values("isChangeable").size() > 0) &&
507                   ((Boolean JavaDoc)featureDef.values("isChangeable").get(0)).booleanValue();
508                 isForeign = false;
509             }
510             boolean isSystemAttribute =
511                  SystemAttributes.OBJECT_CLASS.equals(featureName)
512               || SystemAttributes.CREATED_AT.equals(featureName)
513               || SystemAttributes.MODIFIED_AT.equals(featureName)
514               || SystemAttributes.CREATED_BY.equals(featureName)
515               || SystemAttributes.MODIFIED_BY.equals(featureName);
516
517             if((isDerived || !isChangeable || isForeign) && !isSystemAttribute) {
518               i.remove();
519             }
520           }
521         }
522       }
523     }
524     
525     //-------------------------------------------------------------------------
526
private void exportItem(
527         ZipOutputStream JavaDoc zip,
528         PrintStream JavaDoc ps,
529         List JavaDoc startPoints,
530         Set JavaDoc exportedObjects,
531         Map JavaDoc exportedCodes,
532         int level
533     ) throws IOException JavaDoc, ServiceException {
534         if(level >= 0) {
535             List JavaDoc referencedObjects = new ArrayList JavaDoc();
536             for(
537                 Iterator JavaDoc i = startPoints.iterator();
538                 i.hasNext();
539             ) {
540                 Path startPoint = (Path)i.next();
541                 if(!exportedObjects.contains(startPoint)) {
542                     zip.putNextEntry(
543                         new ZipEntry JavaDoc("data-" + (this.currentEntryId++) + ".xml")
544                     );
545                     
546                     // derive schema name from identity
547
String JavaDoc qualifiedModelName = startPoint.get(0);
548                     String JavaDoc namespace = qualifiedModelName.substring(0, qualifiedModelName.lastIndexOf(":"));
549                     String JavaDoc modelName = qualifiedModelName.substring(qualifiedModelName.lastIndexOf(":") + 1);
550                     Exporter exporter = new Exporter(
551                         this.header,
552                         this.reader,
553                         ps,
554                         this.model,
555                         exportedCodes
556                     );
557                     exporter.export(
558                         Arrays.asList(new Path[]{startPoint}),
559                         this.referenceFilter,
560                         this.attributeFilter,
561                         "xri:+resource/" + namespace.replace(':', '/') + "/" + modelName + "/xmi/" + modelName + ".xsd"
562                     );
563                     
564                     // update referenced and exported objects
565
referencedObjects.addAll(
566                         exporter.getReferencedObjects()
567                     );
568                     exportedObjects.addAll(
569                         exporter.getExportedObjects()
570                     );
571                 }
572             }
573             this.exportItem(
574                 zip,
575                 ps,
576                 referencedObjects,
577                 exportedObjects,
578                 exportedCodes,
579                 level-1
580             );
581         }
582     }
583
584     //-------------------------------------------------------------------------
585
public byte[] exportItem(
586         DataproviderObject_1_0 startFrom,
587         DataproviderObject_1_0 param
588     ) throws ServiceException {
589         
590         try {
591             
592             // reference filter
593
String JavaDoc referenceFilter = (String JavaDoc)param.values("referenceFilter").get(0);
594             if(referenceFilter == null) {
595                 referenceFilter = ":*";
596             }
597             this.referenceFilter = new ArrayList JavaDoc();
598             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(referenceFilter, " ;,", false);
599             while(tokenizer.hasMoreTokens()) {
600                 this.referenceFilter.add(
601                     new Path(startFrom.path().toXri() + "/" + tokenizer.nextToken())
602                 );
603             }
604             
605             // attribute filter
606
String JavaDoc attributeFilter = (String JavaDoc)param.values("attributeFilter").get(0);
607             this.attributeFilter = new HashMap JavaDoc();
608             if(attributeFilter != null) {
609                 tokenizer = new StringTokenizer JavaDoc(attributeFilter, " ;,", false);
610                 while(tokenizer.hasMoreTokens()) {
611                     String JavaDoc filterDefinition = tokenizer.nextToken();
612                     int pos = 0;
613                     if((pos = filterDefinition.indexOf("=")) >= 0) {
614                         Path reference = new Path(startFrom.path().toXri() + "/" + filterDefinition.substring(0, pos));
615                         Filter filter = null;
616                         if(!"*".equals(filterDefinition.substring(pos+1))) {
617                             try {
618                                 XMLDecoder JavaDoc decoder = new XMLDecoder JavaDoc(
619                                     new ByteArrayInputStream JavaDoc(Base64.decode(filterDefinition.substring(pos+1)))
620                                 );
621                                 filter = (Filter)decoder.readObject();
622                                 decoder.close();
623                             }
624                             catch(Exception JavaDoc e) {}
625                         }
626                         if(filter != null) {
627                             this.attributeFilter.put(
628                                 reference,
629                                 filter
630                             );
631                         }
632                     }
633                 }
634             }
635             
636             Set JavaDoc exportedObjects = new TreeSet JavaDoc();
637             
638             // 2-pass export
639
// 1) determine transitive closure of objects to export
640
// 2) export
641

642             // pass 1
643
ZipOutputStream JavaDoc zip = new ZipOutputStream JavaDoc(new NullOutputStream());
644             PrintStream JavaDoc ps = new PrintStream JavaDoc(zip);
645             Map JavaDoc exportedCodes = new HashMap JavaDoc();
646             // export at least code table 'locale'
647
exportedCodes.put(
648                 LOCALE_CODE_TABLE_ID,
649                 null
650             );
651             this.currentEntryId = 0;
652             this.exportItem(
653                 zip,
654                 ps,
655                 Arrays.asList(new Path[]{startFrom.path()}
656                 ),
657                 exportedObjects,
658                 exportedCodes,
659                 MAX_LEVELS
660             );
661
662             // Remove all objects which are composite in set of exported objects
663
// NOTE: exported objects are ordered by identity
664
Path last = null;
665             for(
666                 Iterator JavaDoc i = exportedObjects.iterator();
667                 i.hasNext();
668             ) {
669                 Path current = (Path)i.next();
670                 if((last != null) && (current.startsWith(last))) {
671                     i.remove();
672                 }
673                 else {
674                     last = current;
675                 }
676             }
677             
678             // Assert that the object to be exported is not contained
679
// in the file export list. It will be exportered separately
680
// with traverseContent = false
681
List JavaDoc exportedObjectsAsList = new ArrayList JavaDoc(exportedObjects);
682             exportedObjectsAsList.remove(startFrom.path());
683             
684             // pass 2
685
ByteArrayOutputStream JavaDoc bs = new ByteArrayOutputStream JavaDoc();
686             zip = new ZipOutputStream JavaDoc(bs);
687             ps = new PrintStream JavaDoc(zip);
688             exportedCodes = new HashMap JavaDoc();
689             exportedCodes.put(
690                 LOCALE_CODE_TABLE_ID,
691                 null
692             );
693             // export startFrom with traverseContent = false
694
this.currentEntryId = 0;
695             this.exportItem(
696                 zip,
697                 ps,
698                 Arrays.asList(new Path[]{startFrom.path()}),
699                 new TreeSet JavaDoc(),
700                 exportedCodes,
701                 0
702             );
703             // export contained and referenced objects with traverseContent = true
704
this.exportItem(
705                 zip,
706                 ps,
707                 exportedObjectsAsList,
708                 new TreeSet JavaDoc(),
709                 exportedCodes,
710                 0
711             );
712
713             // referenced codes in separate entry
714
zip.putNextEntry(
715                 new ZipEntry JavaDoc("codes.xml")
716             );
717             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(
718                 new OutputStreamWriter JavaDoc(ps, "UTF-8"),
719                 true
720             );
721             writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
722             writer.println("<org.openmdx.base.Authority xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" name=\"org:opencrx:kernel:code1\" xsi:noNamespaceSchemaLocation=\"xri:+resource/org/opencrx/kernel/code1/xmi/code1.xsd\">");
723             writer.println(" <_object/>");
724             writer.println(" <_content>");
725             writer.println(" <provider>");
726             writer.println(" <org.openmdx.base.Provider qualifiedName=\"CRX\" _operation=\"null\">");
727             writer.println(" <_object/>");
728             writer.println(" <_content>");
729             writer.println(" <segment>");
730             writer.println(" <org.opencrx.kernel.code1.Segment qualifiedName=\"Root\" _operation=\"null\">");
731             writer.println(" <_object/>");
732             writer.println(" <_content>");
733             writer.println(" <valueContainer>");
734             for(Iterator JavaDoc j = exportedCodes.entrySet().iterator(); j.hasNext(); ) {
735                 Entry e = (Entry)j.next();
736                 String JavaDoc codeValueContainerId = (String JavaDoc)e.getKey();
737                 Set JavaDoc codes = (Set JavaDoc)e.getValue();
738                 List JavaDoc codeEntries = this.plugin.getCodes().getCodeEntriesById(codeValueContainerId);
739                 if(((codes == null) || (codes.size() > 0)) && (codeEntries != null)) {
740                     writer.println(" <org.opencrx.kernel.code1.CodeValueContainer name=\"" + codeValueContainerId + "\" _operation=\"null\">");
741                     writer.println(" <_object/>");
742                     writer.println(" <_content>");
743                     writer.println(" <entry>");
744                     for(Iterator JavaDoc k = codeEntries.iterator(); k.hasNext(); ) {
745                         DataproviderObject_1_0 codeEntry = (DataproviderObject_1_0)k.next();
746                         Number JavaDoc code = new Integer JavaDoc(codeEntry.path().getBase());
747                         if((codes == null) || (codes.contains(code))) {
748                             writer.println(" <org.opencrx.kernel.code1.CodeValueEntry code=\"" + code + "\" _operation=\"null\">");
749                             writer.println(" <_object>");
750                             
751                             // shortText
752
writer.println(" <shortText>");
753                             int ll = 0;
754                             for(
755                                 Iterator JavaDoc l = codeEntry.values("shortText").iterator();
756                                 l.hasNext();
757                                 ll++
758                              ) {
759                                 String JavaDoc text = (String JavaDoc)l.next();
760                                 if(text == null) {
761                                     writer.println(" <_item />");
762                                 }
763                                 else {
764                                     writer.println(" <_item>" + text + "</_item>");
765                                 }
766                             }
767                             writer.println(" </shortText>");
768
769                             // longText
770
writer.println(" <longText>");
771                             ll = 0;
772                             for(
773                                 Iterator JavaDoc l = codeEntry.values("longText").iterator();
774                                 l.hasNext();
775                                 ll++
776                              ) {
777                                 String JavaDoc text = (String JavaDoc)l.next();
778                                 if(text == null) {
779                                     writer.println(" <_item />");
780                                 }
781                                 else {
782                                     writer.println(" <_item>" + text + "</_item>");
783                                 }
784                              }
785                              writer.println(" </longText>");
786
787                              writer.println(" </_object>");
788                              writer.println(" <_content/>");
789                              writer.println(" </org.opencrx.kernel.code1.CodeValueEntry>");
790                         }
791                     }
792                     writer.println(" </entry>");
793                     writer.println(" </_content>");
794                     writer.println(" </org.opencrx.kernel.code1.CodeValueContainer>");
795                 }
796             }
797             writer.println(" </valueContainer>");
798             writer.println(" </_content>");
799             writer.println(" </org.opencrx.kernel.code1.Segment>");
800             writer.println(" </segment>");
801             writer.println(" </_content>");
802             writer.println(" </org.openmdx.base.Provider>");
803             writer.println(" </provider>");
804             writer.println(" </_content>");
805             writer.println("</org.openmdx.base.Authority>");
806             zip.closeEntry();
807             
808             ps.close();
809             return bs.toByteArray();
810         }
811         catch(IOException JavaDoc e) {
812             throw new ServiceException(e);
813         }
814     }
815
816     //-------------------------------------------------------------------------
817
public DataproviderObject importItem(
818         byte[] item,
819         short locale,
820         List JavaDoc errors,
821         List JavaDoc report
822     ) throws ServiceException {
823         Importer importer = new Importer(
824             this.target,
825             this.reader,
826             errors,
827             report
828         );
829         importer.process(
830             new InputStream JavaDoc[]{new ByteArrayInputStream JavaDoc(item)}
831         );
832         return importer.getMainObject();
833     }
834   
835     //-------------------------------------------------------------------------
836
// Members
837
//-------------------------------------------------------------------------
838
public static final String JavaDoc MIME_TYPE = "text/xml";
839     public static final int MIME_TYPE_CODE = 2;
840     public static final String JavaDoc LOCALE_CODE_TABLE_ID = "locale";
841     protected static final int MAX_LEVELS = 4;
842
843     protected int currentEntryId = 0;
844     protected final OpenCrxKernel_1 plugin;
845     protected final Model_1_0 model;
846     protected final RequestCollection target;
847     protected final RequestCollection reader;
848     protected final ServiceHeader header;
849     protected List JavaDoc referenceFilter = null;
850     protected Map JavaDoc attributeFilter = null;
851     
852 }
853
854 //--- End of File -----------------------------------------------------------
855
Popular Tags