KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > metadata > MetaCollection


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.xquery.metadata;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
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 // ***************************************************************************
38
// * Class 'MetaCollection'
39
// ***************************************************************************
40

41 public class MetaCollection implements CollectionMetadata {
42     // **********************************************************************
43
// * Constant
44
// **********************************************************************
45
private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
46     private static final String JavaDoc RCSName = "$Name: $";
47
48     // **********************************************************************
49
// * Fields
50
// **********************************************************************
51
private String JavaDoc 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 JavaDoc namespace = null;
62
63     private Schema schema = null;
64     private String JavaDoc strschema = null;
65     private boolean inAttribute = false;
66     // ADD 27/05/2003
67
private ArrayList JavaDoc documents = null; // list of strings representing document paths (relative or absolute)
68

69     // **********************************************************************
70
// * Constructor
71
// **********************************************************************
72

73     /**
74      * Construct the class MetaCollection
75      *
76      * @param
77      */

78     public MetaCollection(MetaDataImpl metadata, String JavaDoc 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     // used when cloning for importing
88
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     // **********************************************************************
98
// * Methods
99
// **********************************************************************
100
// ONLY for QA
101
public void setDocuments(ArrayList JavaDoc documents) {
102         this.documents = documents;
103     }
104     public ArrayList JavaDoc getDocuments() {
105         return documents;
106     }
107
108     /**
109      *
110      *
111      * @param
112      *
113      * @return
114      */

115     public void addMetaElement(String JavaDoc namespace, String JavaDoc 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 (attDecl == null) {
126
// attDecl = new AttributeDeclaration(schema, name, null);
127
// attDecl.setType(metadata.getSchemaManager().getType("http://www.w3.org/2001/XMLSchema", "anyAttribute"));
128
// }
129
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 == null)
142
// {
143
// declaration = new ElementDeclaration(schema, name, null);
144
// declaration.setType(metadata.getSchemaManager ().getType("http://www.w3.org/2001/XMLSchema","anyType"));
145
// }
146
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                 // if (declaration instanceof ElementDeclaration)
159
parentnode = schemanode;
160             }
161
162         }
163     }
164
165     private XTreeNode hasChild(XTreeNode parentnode, String JavaDoc name, String JavaDoc 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 JavaDoc children = parentnode.getChildren();
173         if (children == null)
174             return null;
175         for (Iterator JavaDoc iterator = children.iterator(); iterator.hasNext();) {
176             XTreeNode child = (XTreeNode) iterator.next();
177             String JavaDoc childns = child.getNamespace();
178             if (childns != null && childns.equals(""))
179                 childns = null;
180             String JavaDoc 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     /**
204      * Return the XTree associated to this metacollection (defined from
205      * abstract CollectionMetadata defined in parent class).
206      *
207      * @return XTree associated to this metacollection.
208      */

209     public XTree getXTree() {
210         return schematree;
211     }
212
213     /**
214      * Return the collection_name associated to this metacollection
215      * (defined from abstract CollectionMetadata defined in parent class).
216      *
217      * @return the collection_name of the collection
218      */

219     public String JavaDoc getCollectionName() {
220         return collection_name;
221     }
222
223     public Collection JavaDoc getRootNodes() {
224         if (schematree != null) {
225             return schematree.getRoot().getChildren();
226         } else
227             return null;
228     }
229
230     // **********************************************************************
231
// * Destructor
232
// **********************************************************************
233

234     /**
235      * Dereference allocated object which will be further be destroy
236      * by the garbage collector.
237      */

238     protected void finalize() throws Throwable JavaDoc {}
239 }
240
Popular Tags