KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > querying > impl > xtas > UniFormConverter


1 package org.exoplatform.services.xml.querying.impl.xtas;
2
3 import org.exoplatform.services.xml.querying.UniFormTransformationException;
4 import org.xml.sax.InputSource JavaDoc;
5 import java.io.SequenceInputStream JavaDoc;
6 import java.io.ByteArrayInputStream JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.util.Properties JavaDoc;
9
10 /**
11  * Helper to UniFormTree subclasses transformation
12  */

13 public class UniFormConverter {
14
15     /**
16      * Converts UniFormTreeFragment to WellFormedUniFormTree
17      */

18     public static WellFormedUniFormTree toWellForm(UniFormTree tree) throws UniFormTransformationException
19     {
20
21         if (tree instanceof WellFormedUniFormTree)
22             return (WellFormedUniFormTree)tree;
23         else if (tree instanceof UniFormTreeFragment) {
24
25             WellFormedUniFormTree wfTree = new WellFormedUniFormTree();
26             wfTree.init( new InputSource JavaDoc(tree.getAsInputStream()) );
27             return wfTree;
28
29         }
30         else
31             throw new UniFormTransformationException( "The type "+tree.getClass().getName()+
32                       " is not transformable to WellFormedUniFormTree! ");
33     }
34
35     /**
36      * Converts UniFormTree to WellFormedUniFormTree
37      * and inserts Element named rootName as root
38      */

39     public static WellFormedUniFormTree toWellForm(UniFormTree tree, String JavaDoc rootName) throws UniFormTransformationException
40     {
41
42         if ( (tree instanceof WellFormedUniFormTree) || (tree instanceof UniFormTreeFragment) ) {
43
44             WellFormedUniFormTree wfTree = new WellFormedUniFormTree();
45             SequenceInputStream JavaDoc s = new SequenceInputStream JavaDoc(
46                  new ByteArrayInputStream JavaDoc( ("<"+rootName+">").getBytes() ), tree.getAsInputStream() );
47             s = new SequenceInputStream JavaDoc(
48                  s, new ByteArrayInputStream JavaDoc( ("</"+rootName+">").getBytes() ) );
49
50             wfTree.init( new InputSource JavaDoc(s) );
51             return wfTree;
52         }
53         else
54             throw new UniFormTransformationException( "The type "+tree.getClass().getName()+
55                       " is not transformable to WellFormedUniFormTree! ");
56
57     }
58
59     /**
60      * Converts UniFormTree to WellFormedUniFormTree
61      * and inserts root Element
62      */

63     public static WellFormedUniFormTree toWellForm(UniFormTree tree, String JavaDoc rootName, String JavaDoc namespaceURI, Properties JavaDoc attrs) throws UniFormTransformationException
64     {
65         if ( (tree instanceof WellFormedUniFormTree) || (tree instanceof UniFormTreeFragment) ) {
66
67             WellFormedUniFormTree wfTree = new WellFormedUniFormTree();
68             String JavaDoc attributes = "";
69             for (Enumeration JavaDoc e = attrs.propertyNames(); e.hasMoreElements() ;) {
70                String JavaDoc name = (String JavaDoc)e.nextElement();
71                attributes+=name+"=\""+attrs.getProperty(name)+"\" ";
72             }
73             String JavaDoc str = "<"+rootName+" "+namespaceURI+" "+attributes+">";
74             SequenceInputStream JavaDoc s = new SequenceInputStream JavaDoc(
75                  new ByteArrayInputStream JavaDoc( str.getBytes() ), tree.getAsInputStream() );
76             s = new SequenceInputStream JavaDoc(
77                  s, new ByteArrayInputStream JavaDoc( ("</"+rootName+">").getBytes() ) );
78
79             wfTree.init( new InputSource JavaDoc(s) );
80             return wfTree;
81         }
82         else
83             throw new UniFormTransformationException( "The type "+tree.getClass().getName()+
84                       " is not transformable to WellFormedUniFormTree! ");
85
86     }
87
88     /**
89      * Converts WellformedUniFormTree to UniFormTreeFragment
90      */

91     public static UniFormTreeFragment toFragment(UniFormTree tree) throws UniFormTransformationException
92     {
93         if (tree instanceof UniFormTreeFragment)
94             return (UniFormTreeFragment)tree;
95         else if (tree instanceof WellFormedUniFormTree) {
96
97             UniFormTreeFragment fragment = new UniFormTreeFragment();
98             fragment.init( tree.getAsInputStream() );
99             return fragment;
100
101         }
102         else
103             throw new UniFormTransformationException( "The type "+tree.getClass().getName()+
104                       " is not transformable to UniFormTreeFragment! ");
105
106     }
107 }
108
Popular Tags