-/*******************************************************************************\r
- * Copyright (c) 2007, 2013 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.objmap.structural.schema;\r
-\r
-\r
-\r
-import java.util.Stack;\r
-\r
-import gnu.trove.map.hash.THashMap;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.objmap.exceptions.MappingException;\r
-import org.simantics.objmap.graph.schema.ILinkType;\r
-import org.simantics.objmap.graph.schema.IMappingSchema;\r
-import org.simantics.objmap.structural.IStructuralObject;\r
-import org.simantics.objmap.structural.StructuralResource;\r
-\r
-\r
-/**\r
- * \r
- */\r
-public class DefaultSchema implements IMappingSchema<StructuralResource, IStructuralObject> {\r
-\r
- THashMap<Resource, ILinkType<StructuralResource, IStructuralObject>> domainLinkTypes = \r
- new THashMap<Resource, ILinkType<StructuralResource, IStructuralObject>>();\r
- THashMap<Class<?>, ILinkType<StructuralResource, IStructuralObject>> rangeLinkTypes = \r
- new THashMap<Class<?>, ILinkType<StructuralResource, IStructuralObject>>();\r
- \r
- public void addLinkType(SimpleLinkType linkType) {\r
- domainLinkTypes.put(linkType.domainType, linkType);\r
- rangeLinkTypes.put(linkType.rangeType, linkType);\r
- }\r
- \r
- public void addLinkType(AdaptedLinkType linkType) {\r
- domainLinkTypes.put(linkType.domainType, linkType);\r
- rangeLinkTypes.put(linkType.rangeType, linkType);\r
- }\r
- \r
- @Override\r
- public ILinkType<StructuralResource, IStructuralObject> linkTypeOfDomainElement(ReadGraph g, StructuralResource element) throws MappingException { \r
- try {\r
- \r
- for(Resource type : g.getTypes(element.getResource())) {\r
-\r
- ILinkType<StructuralResource, IStructuralObject> linkType = domainLinkTypes.get(type);\r
- if(linkType != null) return linkType;\r
- \r
- }\r
- \r
- throw new MappingException("Didn't find a link type for " +\r
- NameUtils.getSafeName(g, element.getResource()) + ".");\r
- \r
- } catch (DatabaseException e) {\r
- throw new MappingException(e);\r
- }\r
- }\r
-\r
- @Override\r
- public ILinkType<StructuralResource, IStructuralObject> linkTypeOfRangeElement(IStructuralObject element) throws MappingException {\r
- ILinkType<StructuralResource, IStructuralObject> type = rangeLinkTypes.get(element.getClass());\r
- if(type == null) {\r
- Stack<Class<?>> clazzes = new Stack<Class<?>>();\r
- for (Class<?> clazz : element.getClass().getInterfaces()) {\r
- clazzes.add(clazz);\r
- }\r
- clazzes.add(element.getClass().getSuperclass());\r
- \r
- while (!clazzes.isEmpty()) {\r
- Class<?> clazz = clazzes.pop();\r
- \r
- type = rangeLinkTypes.get(clazz);\r
- if (type != null)\r
- return type;\r
- for (Class<?> c : clazz.getInterfaces())\r
- clazzes.add(c);\r
- \r
- }\r
- throw new MappingException("Didn't find a link type for " + element + ".");\r
- }\r
- return type;\r
- }\r
-\r
- \r
- public ILinkType<StructuralResource, IStructuralObject> linkTypeOfDomainType(ReadGraph g, Resource type) { \r
- return domainLinkTypes.get(type);\r
- }\r
- \r
- public ILinkType<StructuralResource, IStructuralObject> linkTypeOfRangeType(Class<?> clazz) {\r
- ILinkType<StructuralResource, IStructuralObject> type = rangeLinkTypes.get(clazz);\r
- if(type == null) {\r
- Stack<Class<?>> clazzes = new Stack<Class<?>>();\r
- for (Class<?> c : clazz.getInterfaces()) {\r
- clazzes.add(c);\r
- }\r
- clazzes.add(clazz.getSuperclass());\r
- \r
- while (!clazzes.isEmpty()) {\r
- Class<?> c = clazzes.pop();\r
- \r
- type = rangeLinkTypes.get(c);\r
- if (type != null)\r
- return type;\r
- for (Class<?> c2 : c.getInterfaces())\r
- clazzes.add(c2);\r
- \r
- }\r
- \r
- }\r
- return null;\r
- }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2013 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.objmap.structural.schema;
+
+
+
+import java.util.Stack;
+
+import gnu.trove.map.hash.THashMap;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.objmap.exceptions.MappingException;
+import org.simantics.objmap.graph.schema.ILinkType;
+import org.simantics.objmap.graph.schema.IMappingSchema;
+import org.simantics.objmap.structural.IStructuralObject;
+import org.simantics.objmap.structural.StructuralResource;
+
+
+/**
+ *
+ */
+public class DefaultSchema implements IMappingSchema<StructuralResource, IStructuralObject> {
+
+ THashMap<Resource, ILinkType<StructuralResource, IStructuralObject>> domainLinkTypes =
+ new THashMap<Resource, ILinkType<StructuralResource, IStructuralObject>>();
+ THashMap<Class<?>, ILinkType<StructuralResource, IStructuralObject>> rangeLinkTypes =
+ new THashMap<Class<?>, ILinkType<StructuralResource, IStructuralObject>>();
+
+ public void addLinkType(SimpleLinkType linkType) {
+ domainLinkTypes.put(linkType.domainType, linkType);
+ rangeLinkTypes.put(linkType.rangeType, linkType);
+ }
+
+ public void addLinkType(AdaptedLinkType linkType) {
+ domainLinkTypes.put(linkType.domainType, linkType);
+ rangeLinkTypes.put(linkType.rangeType, linkType);
+ }
+
+ @Override
+ public ILinkType<StructuralResource, IStructuralObject> linkTypeOfDomainElement(ReadGraph g, StructuralResource element) throws MappingException {
+ try {
+
+ for(Resource type : g.getTypes(element.getResource())) {
+
+ ILinkType<StructuralResource, IStructuralObject> linkType = domainLinkTypes.get(type);
+ if(linkType != null) return linkType;
+
+ }
+
+ throw new MappingException("Didn't find a link type for " +
+ NameUtils.getSafeName(g, element.getResource()) + ".");
+
+ } catch (DatabaseException e) {
+ throw new MappingException(e);
+ }
+ }
+
+ @Override
+ public ILinkType<StructuralResource, IStructuralObject> linkTypeOfRangeElement(IStructuralObject element) throws MappingException {
+ ILinkType<StructuralResource, IStructuralObject> type = rangeLinkTypes.get(element.getClass());
+ if(type == null) {
+ Stack<Class<?>> clazzes = new Stack<Class<?>>();
+ for (Class<?> clazz : element.getClass().getInterfaces()) {
+ clazzes.add(clazz);
+ }
+ clazzes.add(element.getClass().getSuperclass());
+
+ while (!clazzes.isEmpty()) {
+ Class<?> clazz = clazzes.pop();
+
+ type = rangeLinkTypes.get(clazz);
+ if (type != null)
+ return type;
+ for (Class<?> c : clazz.getInterfaces())
+ clazzes.add(c);
+
+ }
+ throw new MappingException("Didn't find a link type for " + element + ".");
+ }
+ return type;
+ }
+
+
+ public ILinkType<StructuralResource, IStructuralObject> linkTypeOfDomainType(ReadGraph g, Resource type) {
+ return domainLinkTypes.get(type);
+ }
+
+ public ILinkType<StructuralResource, IStructuralObject> linkTypeOfRangeType(Class<?> clazz) {
+ ILinkType<StructuralResource, IStructuralObject> type = rangeLinkTypes.get(clazz);
+ if(type == null) {
+ Stack<Class<?>> clazzes = new Stack<Class<?>>();
+ for (Class<?> c : clazz.getInterfaces()) {
+ clazzes.add(c);
+ }
+ clazzes.add(clazz.getSuperclass());
+
+ while (!clazzes.isEmpty()) {
+ Class<?> c = clazzes.pop();
+
+ type = rangeLinkTypes.get(c);
+ if (type != null)
+ return type;
+ for (Class<?> c2 : c.getInterfaces())
+ clazzes.add(c2);
+
+ }
+
+ }
+ return null;
+ }
+}