Save This Page
Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » store » [javadoc | source]
    1   /*   Copyright 2004 The Apache Software Foundation
    2    *
    3    *   Licensed under the Apache License, Version 2.0 (the "License");
    4    *   you may not use this file except in compliance with the License.
    5    *   You may obtain a copy of the License at
    6    *
    7    *       http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    *   Unless required by applicable law or agreed to in writing, software
   10    *   distributed under the License is distributed on an "AS IS" BASIS,
   11    *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    *   See the License for the specific language governing permissions and
   13    *  limitations under the License.
   14    */
   15   
   16   package org.apache.xmlbeans.impl.store;
   17   
   18   import javax.xml.stream.XMLStreamReader;
   19   
   20   import java.io.InputStream;
   21   import java.io.OutputStream;
   22   import java.io.IOException;
   23   
   24   import org.w3c.dom.DOMImplementation;
   25   import org.w3c.dom.Node;
   26   import org.w3c.dom.Document;
   27   
   28   import org.apache.xmlbeans.XmlBeans;
   29   import org.apache.xmlbeans.XmlCursor;
   30   import org.apache.xmlbeans.XmlOptions;
   31   import org.apache.xmlbeans.XmlObject;
   32   import org.apache.xmlbeans.XmlException;
   33   
   34   import java.io.PrintStream;
   35   
   36   import org.apache.xmlbeans.impl.store.DomImpl.Dom;
   37   
   38   import org.apache.xmlbeans.impl.store.Saver.TextSaver;
   39   
   40   import org.apache.xmlbeans.impl.values.TypeStore;
   41   import org.apache.xmlbeans.impl.values.TypeStoreUser;
   42   import org.apache.xmlbeans.impl.values.TypeStoreVisitor;
   43   import org.apache.xmlbeans.impl.values.TypeStoreUserFactory;
   44   
   45   import org.apache.xmlbeans.SchemaType;
   46   
   47   import org.apache.xmlbeans.impl.values.NamespaceManager;
   48   
   49   import javax.xml.namespace.QName;
   50   
   51   import org.apache.xmlbeans.SchemaField;
   52   
   53   import org.apache.xmlbeans.QNameSet;
   54   
   55   public final class Public2
   56   {
   57       private static Locale newLocale ( Saaj saaj )
   58       {
   59           XmlOptions options = null;
   60   
   61           if (saaj != null)
   62           {
   63               options = new XmlOptions();
   64               options.put( org.apache.xmlbeans.impl.store.Saaj.SAAJ_IMPL, saaj );
   65           }
   66   
   67           return Locale.getLocale( null, options );
   68       }
   69   
   70       private static Locale newLocale ( )
   71       {
   72           return Locale.getLocale( null, null );
   73       }
   74   
   75       public static void setSync ( Document doc, boolean sync )
   76       {
   77           assert doc instanceof Dom;
   78   
   79           Locale l = ((Dom) doc).locale();
   80   
   81           l._noSync = ! sync;
   82       }
   83   
   84       public static String compilePath ( String path, XmlOptions options )
   85       {
   86           return Path.compilePath( path, options );
   87       }
   88   
   89       public static DOMImplementation getDomImplementation ( )
   90       {
   91           return newLocale( );
   92       }
   93   
   94       public static DOMImplementation getDomImplementation ( Saaj saaj )
   95       {
   96           return newLocale( saaj );
   97       }
   98   
   99       public static Document parse ( String s )
  100           throws XmlException
  101       {
  102           Locale l = newLocale();
  103   
  104           Dom d;
  105   
  106           if (l.noSync())         { l.enter(); try { d = l.load( s ); } finally { l.exit(); } }
  107           else synchronized ( l ) { l.enter(); try { d = l.load( s ); } finally { l.exit(); } }
  108   
  109           return (Document) d;
  110       }
  111   
  112       public static Document parse ( String s, XmlOptions options )
  113           throws XmlException
  114       {
  115           Locale l = newLocale();
  116   
  117           Dom d;
  118   
  119           if (l.noSync())         { l.enter(); try { d = l.load( s, options ); } finally { l.exit(); } }
  120           else synchronized ( l ) { l.enter(); try { d = l.load( s, options ); } finally { l.exit(); } }
  121   
  122           return (Document) d;
  123       }
  124   
  125       public static Document parse ( String s, Saaj saaj )
  126           throws XmlException
  127       {
  128           Locale l = newLocale( saaj );
  129   
  130           Dom d;
  131   
  132           if (l.noSync())         { l.enter(); try { d = l.load( s ); } finally { l.exit(); } }
  133           else synchronized ( l ) { l.enter(); try { d = l.load( s ); } finally { l.exit(); } }
  134   
  135           return (Document) d;
  136       }
  137   
  138       public static Document parse ( InputStream is, XmlOptions options )
  139           throws XmlException, IOException
  140       {
  141           Locale l = newLocale();
  142   
  143           Dom d;
  144   
  145           if (l.noSync())         { l.enter(); try { d = l.load( is, options ); } finally { l.exit(); } }
  146           else synchronized ( l ) { l.enter(); try { d = l.load( is, options ); } finally { l.exit(); } }
  147   
  148           return (Document) d;
  149       }
  150   
  151       public static Document parse ( InputStream is, Saaj saaj )
  152           throws XmlException, IOException
  153       {
  154           Locale l = newLocale( saaj );
  155   
  156           Dom d;
  157   
  158           if (l.noSync())         { l.enter(); try { d = l.load( is ); } finally { l.exit(); } }
  159           else synchronized ( l ) { l.enter(); try { d = l.load( is ); } finally { l.exit(); } }
  160   
  161           return (Document) d;
  162       }
  163   
  164       public static Node getNode ( XMLStreamReader s )
  165       {
  166           return Jsr173.nodeFromStream( s );
  167       }
  168   
  169       public static XMLStreamReader getStream ( Node n )
  170       {
  171           assert n instanceof Dom;
  172   
  173           Dom d = (Dom) n;
  174   
  175           Locale l = d.locale();
  176   
  177           if (l.noSync())         { l.enter(); try { return DomImpl.getXmlStreamReader( d ); } finally { l.exit(); } }
  178           else synchronized ( l ) { l.enter(); try { return DomImpl.getXmlStreamReader( d ); } finally { l.exit(); } }
  179       }
  180   
  181       public static String save ( Node n )
  182       {
  183           return save( n, null );
  184       }
  185   
  186       public static void save ( Node n, OutputStream os, XmlOptions options ) throws IOException
  187       {
  188           XmlCursor c = getCursor( n );
  189   
  190           c.save( os, options );
  191   
  192           c.dispose();
  193       }
  194   
  195       public static String save ( Node n, XmlOptions options )
  196       {
  197           assert n instanceof Dom;
  198   
  199           Dom d = (Dom) n;
  200   
  201           Locale l = d.locale();
  202   
  203           if (l.noSync())         { l.enter(); try { return saveImpl( d, options ); } finally { l.exit(); } }
  204           else synchronized ( l ) { l.enter(); try { return saveImpl( d, options ); } finally { l.exit(); } }
  205       }
  206   
  207       private static String saveImpl ( Dom d, XmlOptions options )
  208       {
  209           Cur c = d.tempCur();
  210   
  211           String s = new TextSaver( c, options, null ).saveToString();
  212   
  213           c.release();
  214   
  215           return s;
  216       }
  217   
  218       public static String save ( XmlCursor c )
  219       {
  220           return save( c, null );
  221       }
  222   
  223       public static String save ( XmlCursor xc, XmlOptions options )
  224       {
  225           Cursor cursor = (Cursor) xc;
  226   
  227           Locale l = cursor.locale();
  228   
  229           if (l.noSync())         { l.enter(); try { return saveImpl( cursor, options ); } finally { l.exit(); } }
  230           else synchronized ( l ) { l.enter(); try { return saveImpl( cursor, options ); } finally { l.exit(); } }
  231       }
  232   
  233       private static String saveImpl ( Cursor cursor, XmlOptions options )
  234       {
  235           Cur c = cursor.tempCur();
  236   
  237           String s = new TextSaver( c, options, null ).saveToString();
  238   
  239           c.release();
  240   
  241           return s;
  242       }
  243   
  244       public static XmlCursor newStore ( )
  245       {
  246           return newStore( null );
  247       }
  248   
  249       public static XmlCursor newStore ( Saaj saaj )
  250       {
  251           Locale l = newLocale( saaj );
  252   
  253           if (l.noSync())         { l.enter(); try { return _newStore( l ); } finally { l.exit(); } }
  254           else synchronized ( l ) { l.enter(); try { return _newStore( l ); } finally { l.exit(); } }
  255       }
  256   
  257       public static XmlCursor _newStore ( Locale l )
  258       {
  259           Cur c = l.tempCur();
  260   
  261           c.createRoot();
  262   
  263           Cursor cursor = new Cursor( c );
  264   
  265           c.release();
  266   
  267           return cursor;
  268       }
  269   
  270       public static XmlCursor getCursor ( Node n )
  271       {
  272           assert n instanceof Dom;
  273   
  274           Dom d = (Dom) n;
  275   
  276           Locale l = d.locale();
  277   
  278           if (l.noSync())         { l.enter(); try { return DomImpl.getXmlCursor( d ); } finally { l.exit(); } }
  279           else synchronized ( l ) { l.enter(); try { return DomImpl.getXmlCursor( d ); } finally { l.exit(); } }
  280       }
  281   
  282       public static void dump ( PrintStream o, Dom d )
  283       {
  284           d.dump( o );
  285       }
  286   
  287       public static void dump ( PrintStream o, Node n )
  288       {
  289           dump( o, (Dom) n );
  290       }
  291   
  292       public static void dump ( PrintStream o, XmlCursor c )
  293       {
  294           ((Cursor) c).dump( o );
  295       }
  296   
  297       public static void dump ( PrintStream o, XmlObject x )
  298       {
  299           XmlCursor xc = x.newCursor();
  300           Node n = xc.getDomNode();
  301           Dom d = (Dom) n;
  302           xc.dispose();
  303           
  304           dump( o, d );
  305       }
  306   
  307       public static void dump ( Dom  d )      { dump( System.out, d ); }
  308       public static void dump ( Node n )      { dump( System.out, n ); }
  309       public static void dump ( XmlCursor c ) { dump( System.out, c ); }
  310       public static void dump ( XmlObject x ) { dump( System.out, x ); }
  311   
  312       private static class TestTypeStoreUser implements TypeStoreUser
  313       {
  314           TestTypeStoreUser ( String value ) { _value = value; }
  315           public void attach_store(TypeStore store) { }
  316           public SchemaType get_schema_type() { throw new RuntimeException( "Not impl" ); }
  317           public TypeStore get_store() { throw new RuntimeException( "Not impl" ); }
  318           public void invalidate_value() { }
  319           public boolean uses_invalidate_value() { throw new RuntimeException( "Not impl" ); }
  320           public String build_text(NamespaceManager nsm) { return _value; }
  321           public boolean build_nil() { throw new RuntimeException( "Not impl" ); }
  322           public void invalidate_nilvalue() { throw new RuntimeException( "Not impl" ); }
  323           public void invalidate_element_order() { throw new RuntimeException( "Not impl" ); }
  324           public void validate_now() { throw new RuntimeException( "Not impl" ); }
  325           public void disconnect_store() { throw new RuntimeException( "Not impl" ); }
  326           public TypeStoreUser create_element_user(QName eltName, QName xsiType) { return new TestTypeStoreUser( "ELEM" ); }
  327           public TypeStoreUser create_attribute_user(QName attrName) { throw new RuntimeException( "Not impl" ); }
  328           public String get_default_element_text(QName eltName) { throw new RuntimeException( "Not impl" ); }
  329           public String get_default_attribute_text(QName attrName) { throw new RuntimeException( "Not impl" ); }
  330           public SchemaType get_element_type(QName eltName, QName xsiType) { throw new RuntimeException( "Not impl" ); }
  331           public SchemaType get_attribute_type(QName attrName) { throw new RuntimeException( "Not impl" ); }
  332           public int get_elementflags(QName eltName) { throw new RuntimeException( "Not impl" ); }
  333           public int get_attributeflags(QName attrName) { throw new RuntimeException( "Not impl" ); }
  334           public SchemaField get_attribute_field(QName attrName) { throw new RuntimeException( "Not impl" ); }
  335           public boolean is_child_element_order_sensitive() { throw new RuntimeException( "Not impl" ); }
  336           public QNameSet get_element_ending_delimiters(QName eltname) { throw new RuntimeException( "Not impl" ); }
  337           public TypeStoreVisitor new_visitor() { throw new RuntimeException( "Not impl" ); }
  338   
  339           private String _value;
  340       }
  341   
  342       public static void test ( ) throws Exception
  343       {
  344           Xobj x = (Xobj) Public2.parse( "<a>XY</a>" );
  345           
  346           Locale l = x._locale;
  347   
  348           l.enter();
  349   
  350           try
  351           {
  352               Cur c = x.tempCur();
  353   
  354               c.next();
  355               
  356               Cur c2 = c.tempCur();
  357               c2.next();
  358               
  359               Cur c3 = c2.tempCur();
  360               c3.nextChars( 1 );
  361               
  362               Cur c4 = c3.tempCur();
  363               c4.nextChars( 1 );
  364   
  365               c.dump();
  366               
  367               c.moveNodeContents( c, true );
  368               
  369               c.dump();
  370           }
  371           catch ( Throwable e )
  372           {
  373               e.printStackTrace();
  374           }
  375           finally
  376           {
  377               l.exit();
  378           }
  379       }
  380   }

Save This Page
Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » store » [javadoc | source]