KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > storage > MapperTupleBuilder


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 /*
24  * LogicalStructureBuilder.java
25  *
26  * Created on 25 juillet 2001, 11:45
27  */

28
29 package org.xquark.mapper.storage;
30
31 import java.sql.SQLException JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34
35 import org.xml.sax.Locator JavaDoc;
36 import org.xquark.mapper.RepositoryException;
37 import org.xquark.mapper.dbms.AbstractConnection;
38 import org.xquark.mapper.mapping.ColumnMapping;
39 import org.xquark.mapper.mapping.RepositoryMapping;
40 import org.xquark.mapper.metadata.StoragePathMetadata;
41 import org.xquark.schema.validation.SchemaValidationContext;
42 import org.xquark.xml.xdbc.XMLDBCException;
43 import org.xquark.xpath.NodeKind;
44
45 /**
46  *
47  */

48 public class MapperTupleBuilder extends AbstractModelBuilder
49 {
50     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
51     private static final String JavaDoc RCSName = "$Name: $";
52
53   // Not final for QA purpose
54
public static boolean USE_EMPTY_STRING = false;
55     
56     public MapperTupleBuilder(AbstractConnection connection, RepositoryMapping mapping, SchemaValidationContext schemaContext) throws XMLDBCException
57     {
58         super(connection, schemaContext);
59         storageBuffer = new StorageBuffer(mapping, "dummy", connection, batcher, this, USE_EMPTY_STRING);
60     }
61     
62     public void setDocumentIDs(int docOID, String JavaDoc docID)
63     {
64         // no op
65
}
66     
67     /**
68      * For PI, comments and prefix definition.
69      **/

70     public void extraNode(byte type, StoragePathMetadata path, String JavaDoc data)
71     throws RepositoryException, SQLException JavaDoc
72     {
73         // no op
74
}
75     
76     protected AbstractModelBuilder.AbstractModelNode getLogicalStructureNode(byte type, StoragePathMetadata path, String JavaDoc data, Locator JavaDoc locator)
77     throws SQLException JavaDoc, RepositoryException
78     {
79         AbstractModelBuilder.AbstractModelNode node = null;
80         
81         // process mappings for elements & attributes & increment parent child counters
82
short rank = 0;
83         List JavaDoc nodeTableMappings = null, nodeColumnMappings = null;
84         switch (type)
85         {
86             case NodeKind.ELEMENT:
87                 if (stack.size() != 0) // not root element
88
rank = getCurrentNode().incChildCount();
89                 // no break
90

91             case NodeKind.ATTRIBUTE:
92                 nodeTableMappings = path.getTableMappings();
93                 nodeColumnMappings = path.getColumnMappings();
94                 break;
95                 
96             default :
97         }
98         
99         // allocate or recycle a node
100
node = (AbstractModelNode)stack.push();
101         node.set(path, type, data, rank);
102         
103         // Process table mappings (create tuples)
104
if (nodeTableMappings != null)
105             node.setTupleNode(storageBuffer.bufferizeNode(nodeTableMappings, locator));
106         
107         // Process column mappings (set dependencies)
108
if (nodeColumnMappings != null)
109         {
110             // Create dependance links between tuples (references : up to now we
111
// consider that ref => (FK,PK) and thus a tuple must no be stored
112
// before its master.
113
Iterator JavaDoc it = nodeColumnMappings.iterator();
114             ColumnMapping column = null;
115             int refIndex;
116             while (it.hasNext())
117             {
118                 column = (ColumnMapping)it.next();
119                 refIndex = column.getTableRefIndex();
120                 if (refIndex != -1)
121                     storageBuffer.getTupleFactory(column.getTableIndex()).addMasterTuple(storageBuffer.getTupleFactory(refIndex).getTuple());
122             }
123         }
124         
125         return node;
126     }
127     
128     protected AbstractModelBuilder.AbstractModelNode finalizeNode(Locator JavaDoc locator)
129     throws XMLDBCException, SQLException JavaDoc
130     {
131         AbstractModelBuilder.AbstractModelNode node = getCurrentNode();
132         
133         // process mappings for elements & attributes
134
List JavaDoc nodeTableMappings = null, nodeColumnMappings = null;
135         switch (node.type)
136         {
137             case NodeKind.ELEMENT:
138             case NodeKind.ATTRIBUTE:
139                 nodeTableMappings = node.getPath().getTableMappings();
140                 nodeColumnMappings = node.getPath().getColumnMappings();
141                 break;
142             default :
143         }
144         
145         // process column mappings (simple type elements or attributes)
146
if (nodeColumnMappings != null)
147         {
148             Iterator JavaDoc it = nodeColumnMappings.iterator();
149             ColumnMapping column = null;
150             
151             while (it.hasNext())
152             {
153                 column = (ColumnMapping)it.next();
154                 storageBuffer.getTupleFactory(column.getTableIndex()).getTuple().addColumnMappingData(column, this);
155             }
156         }
157         
158         // Complete table mappings
159
if (nodeTableMappings != null)
160             node.getTupleNode().complete();
161         
162         // TO IMPROVE save data : could be done on mapping complete
163
if (node.type == NodeKind.ELEMENT)
164         {
165             // set tuple to complete, only waiting for dependences
166
// TO IMPLEMENT : check before endElement that no subelement is still waited for an element completion
167
//storageBuffer.getTupleFactory(index).setCompleted();
168

169             // try to flush completed tuples when an element is ended
170
// TO DO : refine it : do it only when necessary
171
storageBuffer.flush();
172         }
173         return (AbstractModelNode)stack.pop();
174     }
175     
176 }
177
Popular Tags