| /* Copyright 2004 The Apache Software Foundation |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| import org.xml.sax.Attributes; |
| import org.xml.sax.ContentHandler; |
| import org.xml.sax.EntityResolver; |
| import org.xml.sax.ErrorHandler; |
| import org.xml.sax.ext.LexicalHandler; |
| import org.xml.sax.InputSource; |
| import org.xml.sax.Locator; |
| import org.xml.sax.SAXException; |
| import org.xml.sax.SAXParseException; |
| import org.xml.sax.XMLReader; |
| import org.apache.xmlbeans.impl.store.Root; |
| import org.apache.xmlbeans.impl.tool.CodeGenUtil; |
| import org.apache.xmlbeans.impl.tool.SchemaCompiler; |
| import org.apache.xmlbeans.SchemaType; |
| import org.apache.xmlbeans.SchemaTypeLoader; |
| import org.apache.xmlbeans.SchemaTypeSystem; |
| import org.apache.xmlbeans.SchemaGlobalElement; |
| import org.apache.xmlbeans.XmlBeans; |
| import org.apache.xmlbeans.XmlCursor.TokenType; |
| import org.apache.xmlbeans.XmlCursor.XmlBookmark; |
| import org.apache.xmlbeans.XmlCursor.ChangeStamp; |
| import org.apache.xmlbeans.XmlCursor; |
| import org.apache.xmlbeans.GDateBuilder; |
| import org.apache.xmlbeans.SimpleValue; |
| import org.apache.xmlbeans.XmlDate; |
| import org.apache.xmlbeans.XmlDocumentProperties; |
| import org.apache.xmlbeans.XmlError; |
| import org.apache.xmlbeans.XmlException; |
| import org.apache.xmlbeans.XmlInt; |
| import org.apache.xmlbeans.XmlInteger; |
| import org.apache.xmlbeans.XmlLineNumber; |
| import org.apache.xmlbeans.XmlObject; |
| import org.apache.xmlbeans.XmlOptions; |
| import java.io.ByteArrayOutputStream; |
| import java.io.ByteArrayInputStream; |
| import java.io.File; |
| import java.io.FileInputStream; |
| import java.io.FileNotFoundException; |
| import java.io.FileOutputStream; |
| import java.io.InputStream; |
| import java.io.IOException; |
| import java.io.OutputStream; |
| import java.io.PrintStream; |
| import java.io.Reader; |
| import java.io.StringReader; |
| import java.lang.reflect.Field; |
| import java.lang.ref.WeakReference; |
| import java.lang.ref.ReferenceQueue; |
| import java.math.BigDecimal; |
| import java.math.BigInteger; |
| import java.net.URI; |
| import java.util.ArrayList; |
| import java.util.Arrays; |
| import java.util.Date; |
| import java.util.HashMap; |
| import java.util.Collection; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.Random; |
| import javax.xml.namespace.QName; |
| import javax.xml.parsers.SAXParser; |
| import javax.xml.parsers.SAXParserFactory; |
| import org.w3.x2001.xmlSchema.*; |
| import org.xml.sax.InputSource; |
| import org.xml.sax.XMLReader; |
| import org.apache.xmlbeans.xml.stream.XMLEvent; |
| import org.apache.xmlbeans.xml.stream.XMLInputStream; |
| import org.apache.xmlbeans.xml.stream.XMLName; |
| import org.apache.xml.xmlbeans.x2004.x02.xbean.config.ConfigDocument; |
| import javax.xml.stream.XMLStreamReader; |
| |
| import org.w3c.dom.Node; |
| import org.w3c.dom.Attr; |
| import org.w3c.dom.CDATASection; |
| import org.w3c.dom.Comment; |
| import org.w3c.dom.ProcessingInstruction; |
| import org.w3c.dom.DocumentFragment; |
| import org.w3c.dom.Document; |
| import org.w3c.dom.Element; |
| import org.w3c.dom.EntityReference; |
| import org.w3c.dom.Text; |
| import org.w3c.dom.CharacterData; |
| import org.w3c.dom.DocumentType; |
| import org.w3c.dom.NodeList; |
| import org.w3c.dom.DOMImplementation; |
| import org.w3c.dom.NamedNodeMap; |
| |
| import javax.xml.stream.XMLStreamReader; |
| |
| import javax.xml.parsers.DocumentBuilderFactory; |
| |
| import org.apache.xmlbeans.impl.newstore2.Public2; |
| |
| import org.nfis.nfi.*; |
| import org.nfis.nfi.TopElementDocument.*; |
| |
| public class EricTest |
| { |
| public static void main ( String[] args ) throws Exception |
| { |
| Document doc = Public2.parse( "<a><b id='1'/><b id='2'/></a>" ); |
| |
| XMLStreamReader xs = Public2.getStream( doc ); |
| |
| xs.next(); |
| xs.next(); |
| |
| Public2.dump( Public2.getNode( xs ) ); |
| |
| // Cache cache = new Cache( doc, new QName( "id" ) ); |
| // |
| // Public2.dump( cache.lookup( "1" ) ); |
| // Public2.dump( cache.lookup( "2" ) ); |
| } |
| |
| public static class Cache |
| { |
| public Cache ( Node n, QName attrName ) |
| { |
| QName[] names = new QName[ 1 ]; |
| names[ 0 ] = attrName; |
| |
| init( n, names ); |
| } |
| |
| public Cache ( Node n, QName[] attrNames ) |
| { |
| init( n, attrNames ); |
| } |
| |
| public Node lookup ( String key ) |
| { |
| ensureCache(); |
| |
| return (Node) _map.get( key ); |
| } |
| |
| private void init ( Node n, QName[] attrNames ) |
| { |
| _node = n; |
| _map = new HashMap(); |
| |
| StringBuffer sb = new StringBuffer(); |
| |
| for ( int i = 0 ; i < attrNames.length ; i++ ) |
| { |
| if (attrNames[ i ].getNamespaceURI().length() > 0) |
| { |
| sb.append( "declare namespace ns" ); |
| sb.append( i ); |
| sb.append( "='" ); |
| sb.append( attrNames[ i ].getNamespaceURI() ); |
| sb.append( "' " ); |
| } |
| } |
| |
| for ( int i = 0 ; i < attrNames.length ; i++ ) |
| { |
| if (i > 0) |
| sb.append( "|" ); |
| |
| sb.append( ".//@" ); |
| |
| if (attrNames[ i ].getNamespaceURI().length() > 0) |
| { |
| sb.append( "ns" ); |
| sb.append( i ); |
| sb.append( ":" ); |
| } |
| |
| sb.append( attrNames[ i ].getLocalPart() ); |
| } |
| |
| _path = Public2.compilePath( sb.toString(), null ); |
| } |
| |
| private void ensureCache ( ) |
| { |
| if (_stamp != null && !_stamp.hasChanged()) |
| return; |
| |
| XmlCursor c = Public2.getCursor( _node ); |
| |
| _stamp = c.getDocChangeStamp(); |
| |
| _map.clear(); |
| |
| c.selectPath( _path ); |
| |
| while ( c.toNextSelection() ) |
| _map.put( c.getTextValue(), c.getDomNode() ); |
| |
| c.dispose(); |
| } |
| |
| private Node _node; |
| private String _path; |
| private ChangeStamp _stamp; |
| private HashMap _map; |
| } |
| } |
| |