1 22 23 package org.xquark.xquery.metadata; 24 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 29 import org.xquark.schema.*; 30 import org.xquark.xpath.NodeKind; 31 import org.xquark.xpath.XTree; 32 import org.xquark.xpath.XTreeNode; 33 import org.xquark.xpath.schema.SchemaNode; 34 import org.xquark.xpath.schema.SchemaTreeBuilder; 35 import org.xquark.xquery.metadata.resolver.CollectionMetadata; 36 37 41 public class MetaCollection implements CollectionMetadata { 42 private static final String RCSRevision = "$Revision: 1.2 $"; 46 private static final String RCSName = "$Name: $"; 47 48 private String collection_name = null; 52 private MetaDataImpl metadata = null; 53 private XTree schematree = null; 54 55 private SchemaTreeBuilder factory = null; 56 private SchemaContext schemacontext = null; 57 private SchemaNode parentnode = null; 58 59 private Declaration declaration = null; 60 61 private String namespace = null; 62 63 private Schema schema = null; 64 private String strschema = null; 65 private boolean inAttribute = false; 66 private ArrayList documents = null; 69 73 78 public MetaCollection(MetaDataImpl metadata, String collection_name) { 79 this.metadata = metadata; 80 this.collection_name = collection_name; 81 factory = new SchemaTreeBuilder(); 82 schematree = factory.createTree(); 83 schemacontext = new SchemaContext(metadata.getSchemaManager()); 84 parentnode = (SchemaNode) schematree.getRoot(); 85 } 86 87 public MetaCollection(MetaDataImpl metadata, MetaCollection oldcol) { 89 this.metadata = metadata; 90 this.collection_name = oldcol.getCollectionName(); 91 factory = new SchemaTreeBuilder(); 92 this.schematree = oldcol.getXTree(); 93 schemacontext = new SchemaContext(metadata.getSchemaManager()); 94 parentnode = (SchemaNode) schematree.getRoot(); 95 } 96 97 public void setDocuments(ArrayList documents) { 102 this.documents = documents; 103 } 104 public ArrayList getDocuments() { 105 return documents; 106 } 107 108 115 public void addMetaElement(String namespace, String name, boolean isAttribute) { 116 if (namespace != null) { 117 this.namespace = namespace; 118 } 119 120 SchemaNode schemanode = null; 121 if (isAttribute) { 122 AttributeDeclaration attDecl = schemacontext.getAttributeDeclaration(namespace, name); 123 if ((attDecl != null) && (attDecl.getSchema() != null)) 124 schema = attDecl.getSchema(); 125 if (hasChild(parentnode, name, namespace) == null) { 130 if (attDecl == null) 131 schemanode = (SchemaNode) factory.createNamedNode(parentnode, namespace, name, NodeKind.ATTRIBUTE); 132 else 133 schemanode = (SchemaNode) factory.createTypedNode(parentnode, attDecl); 134 } 135 inAttribute = true; 136 } else { 137 declaration = schemacontext.getElementDeclaration(namespace, name); 138 139 if ((declaration != null) && (declaration.getSchema() != null)) 140 schema = declaration.getSchema(); 141 if (declaration instanceof ElementDeclaration) { 147 schemacontext.push((ElementDeclaration) declaration); 148 } 149 150 SchemaNode childnode = null; 151 if ((childnode = (SchemaNode) hasChild(parentnode, name, namespace)) != null) { 152 parentnode = childnode; 153 } else { 154 if (declaration == null) 155 schemanode = (SchemaNode) factory.createNamedNode(parentnode, namespace, name, NodeKind.ELEMENT); 156 else 157 schemanode = (SchemaNode) factory.createTypedNode(parentnode, declaration); 158 parentnode = schemanode; 160 } 161 162 } 163 } 164 165 private XTreeNode hasChild(XTreeNode parentnode, String name, String ns) { 166 if (ns != null && ns.equals("")) 167 ns = null; 168 if (name == null || name.equals("")) 169 return null; 170 if (parentnode == null) 171 return null; 172 Collection children = parentnode.getChildren(); 173 if (children == null) 174 return null; 175 for (Iterator iterator = children.iterator(); iterator.hasNext();) { 176 XTreeNode child = (XTreeNode) iterator.next(); 177 String childns = child.getNamespace(); 178 if (childns != null && childns.equals("")) 179 childns = null; 180 String childname = child.getLocalName(); 181 if (childname == null || childname.equals("")) 182 return null; 183 if (((ns == null && childns == null) || ((ns != null) && (childns != null) && (ns.equalsIgnoreCase(childns)))) && ((name != null) && (childname != null) && (name.equalsIgnoreCase(childname)))) 184 return child; 185 } 186 return null; 187 } 188 189 public void addEndElement() { 190 if (inAttribute) { 191 inAttribute = false; 192 return; 193 } 194 if ((declaration != null) && ((declaration instanceof ElementDeclaration))) { 195 declaration = schemacontext.pop(); 196 } 197 198 if (parentnode != null) { 199 parentnode = (SchemaNode) parentnode.getParent(); 200 } 201 } 202 203 209 public XTree getXTree() { 210 return schematree; 211 } 212 213 219 public String getCollectionName() { 220 return collection_name; 221 } 222 223 public Collection getRootNodes() { 224 if (schematree != null) { 225 return schematree.getRoot().getChildren(); 226 } else 227 return null; 228 } 229 230 234 238 protected void finalize() throws Throwable {} 239 } 240 | Popular Tags |