KickJava   Java API By Example, From Geeks To Geeks.

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


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.xml.sax.SAXException JavaDoc;
26 import org.xquark.schema.*;
27 import org.xquark.xpath.NodeKind;
28 import org.xquark.xpath.XNode;
29
30 /** This class allows builds a repository mapping iterating through it
31  * following the associated schema.
32  * <p>Used in particular by the mapping loader</p>
33  * @see org.xquark.mapper.mapping.Loader
34  */

35 public class BuilderMappingContext extends SchemaContext
36 {
37 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
38 private static final String JavaDoc RCSName = "$Name: $";
39     //
40
// DATA
41
//
42
protected MappingFactory factory;
43     protected RepositoryMapping mapping;
44     protected MappingNode wNode;
45     protected XNode pattern = new XNode();
46     
47     //
48
// CONSTRUCTORS
49
//
50

51     public BuilderMappingContext(MappingFactory factory)
52     {
53         super(factory.getSchemaManager(), factory.getSchemaLocator());
54         this.factory = factory;
55         mapping = (RepositoryMapping)factory.getTree();
56         wNode = (MappingNode)mapping.getRoot();
57     }
58     
59     //
60
// MappingContext OVERLOADING
61
//
62

63     //
64
// public Methods
65
//
66

67     public void createElementMapping(ElementDeclaration decl, boolean typeMapping)
68     {
69         factory.createElementNode(wNode, decl, typeMapping);
70         // must be called after the node creation
71
push(decl); // careful if overidden !
72
}
73     
74         /* Create a root node for Complex type */
75     public org.xquark.schema.Type createTypeMapping(String JavaDoc namespace, String JavaDoc localName) throws SAXException JavaDoc
76     {
77         org.xquark.schema.Type type = factory.findType(namespace, localName);
78         wNode = factory.createTypeNode(type);
79         return type;
80     }
81     
82         /* Remove Type node from mapping context stack */
83     public void popType()
84     {
85         wNode = (MappingNode)mapping.getRoot();
86     }
87     
88         /* Register a mapping */
89     public void register(Mapping mapping)
90     {
91         SchemaComponent comp = mapping.getSchemaComponent();
92         if (comp instanceof AttributeDeclaration)
93             this.mapping.register((MappingNode)factory.createNamedNodeIfNotExist(wNode,
94             comp.getNamespace(),
95             comp.getName(),
96         NodeKind.ATTRIBUTE), mapping);
97         else // ElementDeclaration, Type
98
this.mapping.register(wNode, mapping);
99     }
100     
101     //
102
// SchemaContext OVERLOADING
103
//
104

105     public final void push(ElementDeclaration decl)
106     {
107         super.push(decl);
108         pattern.set(decl.getNamespace(), decl.getName(), NodeKind.ELEMENT);
109         wNode = (MappingNode)wNode.getChild(pattern);
110     }
111     
112     public final ElementDeclaration pop()
113     {
114         ElementDeclaration decl = super.pop();
115         wNode = (MappingNode)wNode.getParent();
116         return decl;
117     }
118     
119     /**
120      * @return
121      */

122     MappingNode getCurrentNode() {
123         return wNode;
124     }
125
126 }
127
Popular Tags