1 22 23 package org.xquark.mapper.mapping; 24 25 import org.xml.sax.SAXException ; 26 import org.xquark.mapper.metadata.Repository; 27 import org.xquark.schema.*; 28 import org.xquark.xpath.*; 29 30 34 public class MappingFactory extends XTreeBuilder 35 { 36 private static final String RCSRevision = "$Revision: 1.1 $"; 37 private static final String 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); } 84 85 89 public XTreeNode allocateNode(XTreeNode parent, String namespace, String localName, byte type) 90 { 91 MappingNode node = new MappingNode(tree, parent, namespace, localName, type); 92 node.setComponent(findComponent(parent == null ? null:((MappingNode)parent).getSchemaComponent(), node)); 94 return node; 95 } 96 97 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 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 if (comp != null) 115 { 116 org.xquark.schema.Type pType; 117 if (comp instanceof org.xquark.schema.Type) 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 if ((decl == null) && (node.getType() != NodeKind.NONE)) { try 138 { 139 Schema schema = schemaManager.loadSchema(schemaLocator, node.getNamespace()); 140 if (schema != null) 141 decl = schema.getElementDeclaration(node.getLocalName()); 142 } 143 catch (SAXException e) 144 { 145 } 147 } 151 return decl; 152 } 153 154 public MappingNode createTypeNode(org.xquark.schema.Type type) 155 { 156 MappingNode node = (MappingNode)createNode(null, NodeKind.NONE); node.setComponent(type); 158 ((RepositoryMapping)tree).addTypeNode(node); 159 return node; } 161 162 166 public MappingNode createElementNode(MappingNode parent, ElementDeclaration decl, boolean typeMapping) 167 { 168 MappingNode node; 169 if (typeMapping) { 171 MappingNode typeNode = ((RepositoryMapping)tree).getTypeNode(decl.getType()); 172 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 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 190 public org.xquark.schema.Type findType(String namespace, String localName) throws SAXException 191 { 192 org.xquark.schema.Schema schema = schemaManager.loadSchema(schemaLocator, namespace); 193 if (schema == null) 194 throw new SAXException ("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 |