KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > metadata > MetadataIterator


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.mapper.metadata;
24
25 import java.util.List JavaDoc;
26
27 import org.xquark.mapper.RepositoryException;
28 import org.xquark.mapper.storage.PathIterator;
29 import org.xquark.schema.ElementDeclaration;
30 import org.xquark.xpath.*;
31
32 /** This class allows to iterate through mappings of a collection RepositoryMapping
33  * object using names (and not Schema declarations).
34  */

35 public class MetadataIterator implements PathIterator
36 {
37     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
38     private static final String JavaDoc RCSName = "$Name: $";
39     
40     //
41
// DATA
42
//
43

44     /** The XMLCollection Mapping browsed
45      */

46     protected PathSetFactory factory;
47     protected PathSet pathSet;
48     protected PathNode wNode;
49     protected XNode pattern = new XNode();
50     
51     //
52
// CONSTRUCTORS
53
//
54

55     public MetadataIterator(PathSetFactory factory)
56     {
57         this(factory, (PathNode)factory.getTree().getRoot());
58     }
59     public MetadataIterator(PathSetFactory factory, PathNode root)
60     {
61         this.factory = factory;
62         pathSet = (PathSet)factory.getTree();
63         wNode = root;
64     }
65     
66     
67     public void reset()
68     {
69         wNode = (PathNode)factory.getTree().getRoot();
70     }
71     
72     /** Get the default table mapping
73      */

74     public List JavaDoc getDefaultMapping()
75     {
76         return pathSet.getCollection().getDefaultMappingList();
77     }
78     
79     /** Get the table mappings for a particular subelement in the current context
80      */

81     public List JavaDoc getElementTableMappings(String JavaDoc namespace, String JavaDoc localName)
82     {
83         PathNode node = getChild(namespace, localName, NodeKind.ELEMENT);
84         if (node == null)
85             return null;
86         else
87             return node.getMapping().getTableMappings();
88     }
89     
90     /** Get the column mappings for a particular subelement in the current context
91      */

92     public List JavaDoc getElementColumnMappings(String JavaDoc namespace, String JavaDoc localName)
93     {
94         PathNode node = getChild(namespace, localName, NodeKind.ELEMENT);
95         if (node == null)
96             return null; // SET schema component ?
97
else
98             return node.getMapping().getColumnMappings();
99     }
100     
101     /** Get the table mappings for a particular attribute in the current context
102      */

103     public List JavaDoc getAttributeTableMappings(String JavaDoc namespace, String JavaDoc localName)
104     {
105         PathNode node = getChild(namespace, localName, NodeKind.ATTRIBUTE);
106         if (node == null)
107             return null; // SET schema component ?
108
else
109             return node.getMapping().getTableMappings();
110     }
111     
112     /** Get the column mappings for a particular attribute in the current context
113      */

114     public List JavaDoc getAttributeColumnMappings(String JavaDoc namespace, String JavaDoc localName)
115     {
116         PathNode node = getChild(namespace, localName, NodeKind.ATTRIBUTE);
117         if (node == null)
118             return null; // SET schema component ?
119
else
120             return node.getMapping().getColumnMappings();
121     }
122     
123     /** Get the column mappings for a particular attribute in the current context
124      */

125     public PathNode getChild(String JavaDoc namespace, String JavaDoc localName, byte type)
126     {
127         pattern.set(namespace, localName, type);
128         return (PathNode)wNode.getChild(pattern);
129     }
130
131     /** Get the column mappings for a particular attribute in the current context
132      */

133     public PathMetadata getPathMetadata(short pid)
134     throws RepositoryException
135     {
136         return (PathNode)pathSet.get(pid);
137     }
138     
139     public PathNode getPathNode(short pid)
140     throws RepositoryException
141     {
142         return (PathNode)pathSet.get(pid);
143     }
144
145     public String JavaDoc getNameSpace()
146     {
147         return wNode.getNamespace();
148     }
149     
150     public String JavaDoc getLocalName()
151     {
152         return wNode.getLocalName();
153     }
154     
155     public ElementDeclaration getElementDeclaration()
156     {
157         return (ElementDeclaration)wNode.getDeclaration();
158     }
159     
160     /* Create a node if it does not exist without entering it */
161     public StoragePathMetadata createNode(String JavaDoc namespace, String JavaDoc localName, byte type)
162     throws RepositoryException
163     {
164         PathNode node;
165         try {
166             node = (PathNode)factory.createNamedNodeIfNotExist(wNode, namespace, localName, type); // SET schema component ?
167
}
168         catch (XTreeRuntimeException e)
169         {
170             if (e.getException() instanceof RepositoryException)
171                 throw (RepositoryException)e.getException();
172             XNode parser = new XNode(namespace, localName, type);
173             throw new RepositoryException(RepositoryException.DATA_LOSS,
174             "Error while creating new path " + wNode.getLocation() + "/" + parser.getAbbreviatedSyntax()
175             + " in metadata.", e.getException());
176         }
177         
178         return node; // SET schema component ?
179
}
180
181     /* Enter the specified node element or create it if does not exist */
182     public StoragePathMetadata push(String JavaDoc namespace, String JavaDoc localName) throws RepositoryException
183     {
184         return wNode = (PathNode)createNode(namespace, localName, NodeKind.ELEMENT);
185     }
186
187     /* Enter the specified node */
188     public PathMetadata push(short pid)
189     throws RepositoryException
190     {
191         return wNode = (PathNode)pathSet.get(pid);
192     }
193
194     /* Enter the specified node */
195     public PathMetadata push(PathNode node)
196     {
197         return wNode = node;
198     }
199     
200     /* Enter the specified node or create it if does not exist */
201     public StoragePathMetadata pop()
202     {
203         PathNode leftNode = wNode;
204         wNode = (PathNode)wNode.getParent();
205         if (wNode == null)
206             wNode = leftNode;
207 // Debug.print("MetadataIterator :: POP = ", leftNode.getAbbreviatedSyntax());
208
return leftNode;
209     }
210     
211     /** Return the absolute position of the locator.
212      * <p>i.e., !isDiscardable()</p>
213      */

214     public PathExpr getLocation()
215     {
216         return wNode.getLocation();
217     }
218     
219     public StoragePathMetadata getStoragePathMetadata()
220     {
221         return wNode;
222     }
223     
224     public PathMetadata getPathMetadata()
225     {
226         return wNode;
227     }
228     
229     public PathNode getPathNode()
230     {
231         return wNode;
232     }
233 }
234
Popular Tags