1 package org.exoplatform.services.xml.querying.impl.xtas; 2 3 import org.exoplatform.services.xml.querying.UniFormTransformationException; 4 import org.xml.sax.InputSource ; 5 import java.io.SequenceInputStream ; 6 import java.io.ByteArrayInputStream ; 7 import java.util.Enumeration ; 8 import java.util.Properties ; 9 10 13 public class UniFormConverter { 14 15 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 (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 39 public static WellFormedUniFormTree toWellForm(UniFormTree tree, String rootName) throws UniFormTransformationException 40 { 41 42 if ( (tree instanceof WellFormedUniFormTree) || (tree instanceof UniFormTreeFragment) ) { 43 44 WellFormedUniFormTree wfTree = new WellFormedUniFormTree(); 45 SequenceInputStream s = new SequenceInputStream ( 46 new ByteArrayInputStream ( ("<"+rootName+">").getBytes() ), tree.getAsInputStream() ); 47 s = new SequenceInputStream ( 48 s, new ByteArrayInputStream ( ("</"+rootName+">").getBytes() ) ); 49 50 wfTree.init( new InputSource (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 63 public static WellFormedUniFormTree toWellForm(UniFormTree tree, String rootName, String namespaceURI, Properties attrs) throws UniFormTransformationException 64 { 65 if ( (tree instanceof WellFormedUniFormTree) || (tree instanceof UniFormTreeFragment) ) { 66 67 WellFormedUniFormTree wfTree = new WellFormedUniFormTree(); 68 String attributes = ""; 69 for (Enumeration e = attrs.propertyNames(); e.hasMoreElements() ;) { 70 String name = (String )e.nextElement(); 71 attributes+=name+"=\""+attrs.getProperty(name)+"\" "; 72 } 73 String str = "<"+rootName+" "+namespaceURI+" "+attributes+">"; 74 SequenceInputStream s = new SequenceInputStream ( 75 new ByteArrayInputStream ( str.getBytes() ), tree.getAsInputStream() ); 76 s = new SequenceInputStream ( 77 s, new ByteArrayInputStream ( ("</"+rootName+">").getBytes() ) ); 78 79 wfTree.init( new InputSource (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 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 |