KickJava   Java API By Example, From Geeks To Geeks.

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


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.mapper.metadata.Repository;
27 import org.xquark.schema.*;
28 import org.xquark.xpath.*;
29
30 /**
31  * Specific implementation of XTreeBuilder.
32  *
33  */

34 public class MappingFactory extends XTreeBuilder
35 {
36     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
37     private static final String JavaDoc RCSName = "$Name: $";
38     
39     private SchemaManager schemaManager;
40     private SchemaLocator schemaLocator;
41     private Repository rep;
42     
43     public MappingFactory(SchemaManager manager, SchemaLocator locator)
44     {
45         set(manager, locator);
46     }
47     
48     public MappingFactory(Repository rep, SchemaManager manager, SchemaLocator locator)
49     {
50         this(manager, locator);
51         this.rep = rep;
52     }
53     
54     public MappingFactory (
55                             RepositoryMapping mapping,
56                             SchemaManager manager,
57                             SchemaLocator locator
58                             )
59     {
60         super(mapping);
61         set(manager, locator);
62     }
63     
64     private void set(SchemaManager manager, SchemaLocator locator)
65     {
66         this.schemaManager = manager;
67         this.schemaLocator = locator;
68     }
69     
70     public SchemaLocator getSchemaLocator()
71     {
72         return schemaLocator;
73     }
74     
75     public SchemaManager getSchemaManager()
76     {
77         return schemaManager;
78     }
79     
80     public XTree allocateTree()
81     {
82         return new RepositoryMapping(rep); // may be null
83
}
84     
85     //
86
// XModelFactory IMPLEMENTATION
87
//
88

89     public XTreeNode allocateNode(XTreeNode parent, String JavaDoc namespace, String JavaDoc localName, byte type)
90     {
91         MappingNode node = new MappingNode(tree, parent, namespace, localName, type);
92         // get decl from parent or from Schema Manager
93
node.setComponent(findComponent(parent == null ? null:((MappingNode)parent).getSchemaComponent(), node));
94         return node;
95     }
96     
97     /** Version used by the PathSet class for flying nodes.
98      */

99     public XTreeNode allocateNode(SchemaComponent comp, XTreeNode pathNode)
100     {
101         MappingNode node = new MappingNode(tree, null, pathNode.getNamespace(),
102                                             pathNode.getLocalName(),
103                                             pathNode.getType());
104         // get decl from parent or from Schema Manager
105
node.setComponent(findComponent(comp, node));
106         return node;
107     }
108     
109     private Declaration findComponent(SchemaComponent comp, MappingNode node)
110     {
111         Declaration decl = null;
112         
113         // use parent element declaration (parent is an element because type has no parent...)
114
if (comp != null)
115         {
116             org.xquark.schema.Type pType;
117             if (comp instanceof org.xquark.schema.Type) // for a type mapping
118
pType = (org.xquark.schema.Type)comp;
119             else
120                 pType = ((ElementDeclaration)comp).getType();
121             switch (node.getType())
122             {
123                 case NodeKind.ATTRIBUTE:
124                     decl = pType.getAttributeDeclaration(node.getNamespace(), node.getLocalName());
125                     break;
126                 case NodeKind.ELEMENT:
127                     decl = pType.getElementDeclaration(node.getNamespace(), node.getLocalName());
128                     break;
129                 default:
130             }
131         }
132         // use SchemaManager
133
if ((decl == null) && (node.getType() != NodeKind.NONE)) // not found or no parent comp:
134
// may be a wildcard or a substitution group : jump to declaration
135
{ // IMPORTANT : responsability is left to document validation or mapping loader
136
// to verify that "jump" is allowed
137
try
138             {
139                 Schema schema = schemaManager.loadSchema(schemaLocator, node.getNamespace());
140                 if (schema != null)
141                     decl = schema.getElementDeclaration(node.getLocalName());
142             }
143             catch (SAXException JavaDoc e)
144             {
145                 // TO DO : not so good !
146
}
147 // if ((decl == null) && !node.getNamespace().equals(MappingConstants.MAPPING_URI))
148
// throw new RuntimeException("No declaration was found in the Repository for: " + node);
149
// TO DO : add control to check when schema is added : doc already stored may be unvalid
150
}
151         return decl;
152     }
153     
154     public MappingNode createTypeNode(org.xquark.schema.Type type)
155     {
156         MappingNode node = (MappingNode)createNode(null, NodeKind.NONE); // type ROOT is needed to avoid null pointer exception in getLocation()
157
node.setComponent(type);
158         ((RepositoryMapping)tree).addTypeNode(node);
159         return node; // return the created node, not the replaced returned by put (should not happen)
160
}
161     
162     /** Add a new metaNode for an element.
163      * @param type if ELEMENT normal processing. If NODE, supposed to be a shared
164      * type mapping, the corresponding type node is cloned.
165      */

166     public MappingNode createElementNode(MappingNode parent, ElementDeclaration decl, boolean typeMapping)
167     {
168         MappingNode node;
169         if (typeMapping) // The corresponding type NodeEntry is cloned
170
{
171             MappingNode typeNode = ((RepositoryMapping)tree).getTypeNode(decl.getType());
172             // duplicate the type branch
173
node =(MappingNode)typeNode.clone();
174             node.getHashKey().set(decl.getNamespace(), decl.getName(), NodeKind.ELEMENT);
175             node.setComponent(decl);
176             parent.addChild(node);
177             tree.register(node);
178             // add type mapping in scope to element hierachy
179
for (MappingNode elt = node; elt != null; elt = (MappingNode)elt.getParent())
180             {
181                 elt.addMappingsInScope(typeNode.getScopeMappings());
182             }
183         }
184         else
185             node = (MappingNode)createNamedNodeIfNotExist(parent, decl.getNamespace(), decl.getName(), NodeKind.ELEMENT);
186         return node;
187     }
188     
189     /* Used by mapping loader (ns must no be null) */
190     public org.xquark.schema.Type findType(String JavaDoc namespace, String JavaDoc localName) throws SAXException JavaDoc
191     {
192         org.xquark.schema.Schema schema = schemaManager.loadSchema(schemaLocator, namespace);
193         if (schema == null)
194             throw new SAXException JavaDoc("The schema for \"" + namespace
195             + "\" namespace is not available so the mapping file cannot be loaded.");
196         return schema.getType(localName);
197     }
198     
199     
200 }
201
Popular Tags