KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > mapping > MappingIterator


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.mapping;
24
25 import org.xquark.mapper.RepositoryException;
26 import org.xquark.mapper.metadata.StoragePathMetadata;
27 import org.xquark.mapper.storage.PathIterator;
28 import org.xquark.xpath.NodeKind;
29 import org.xquark.xpath.PathExpr;
30 import org.xquark.xpath.XNode;
31
32 /** This class allows to iterate through mappings of RepositoryMapping object
33  * using names (and not Schema declarations) reading only. Push made out of the
34  * mapping will lead the getPathNode() method to return null.
35  */

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

45     /** The XMLCollection Mapping browsed
46      */

47     private RepositoryMapping mapping;
48     private MappingNode wNode;
49     private XNode pattern = new XNode();
50     private int extraPop;
51     
52     //
53
// CONSTRUCTORS
54
//
55

56     public MappingIterator(RepositoryMapping mapping)
57     {
58         this.mapping = mapping;
59         reset();
60     }
61
62     public void reset()
63     {
64         wNode = (MappingNode)mapping.getRoot();
65         extraPop = 0;
66     }
67     
68     /* Create a node if it does not exist without entering it */
69     public StoragePathMetadata createNode(String JavaDoc namespace, String JavaDoc localName, byte type) throws RepositoryException
70     {
71         return getChild(namespace, localName, type);
72     }
73
74     /* Enter the specified node element or create it if does not exist */
75     public StoragePathMetadata push(String JavaDoc namespace, String JavaDoc localName) throws RepositoryException
76     {
77         MappingNode node = null;
78         if ((extraPop > 0) || ((node = getChild(namespace, localName, NodeKind.ELEMENT)) == null))
79             extraPop++;
80         else
81             wNode = node;
82         return node;
83     }
84
85     /** Get the column mappings for a particular attribute in the current context
86      */

87     private MappingNode getChild(String JavaDoc namespace, String JavaDoc localName, byte type)
88     {
89         pattern.set(namespace, localName, type);
90         return (MappingNode)wNode.getChild(pattern);
91     }
92
93     /* Enter the specified node or create it if does not exist */
94     public StoragePathMetadata pop()
95     {
96         MappingNode leftNode = wNode;
97         if (extraPop == 0)
98             wNode = (MappingNode)wNode.getParent();
99         else
100         {
101             extraPop--;
102             leftNode = null;
103         }
104         return leftNode;
105     }
106     
107     /** Return the absolute position of the locator.
108      * <p>i.e., !isDiscardable()</p>
109      */

110     public PathExpr getLocation()
111     {
112         return wNode.getLocation();
113     }
114     
115     public StoragePathMetadata getStoragePathMetadata()
116     {
117         if (extraPop == 0)
118             return wNode;
119         else
120             return null;
121     }
122 }
123
Popular Tags