4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
AttributeManager.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Jan Planer xplane01@stud.fit.vutbr.cz
4  * File: AttributeManager.java
5  * Description: Attribute manager provides a way how to create new attributes.
6  * This is handful for example at xml parsing
7  */
8 
9 /**
10  * @file AttributeManager.java
11  *
12  * @brief Attribute manager provides a way how to create new attributes.
13  */
14 
15 package cz.vutbr.fit.knot.annotations.entity.attribute;
16 
19 import java.util.HashMap;
20 
21 /**
22  * Class attribute manager provides a way how to create new attributes.
23  *
24  * @brief Class attribute manager provides a way how to create new attributes.
25  */
26 public class AttributeManager {
27 
28  /**
29  * Constructor
30  */
31  public static BaseAttribute createAttribute() { return null; }
32 
33  /**
34  * Mapping of types of attributes to classes
35  */
36  private static final HashMap<String,Class> mapping = new HashMap<String,Class>();
37  static {
38  mapping.put("AnyAnnotation", AnyAnnotationAttribute.class);
39  mapping.put("AnnotationLink", LinkedAnnotationAttribute.class);
40  mapping.put("annotationLink", LinkedAnnotationAttribute.class);
41  mapping.put("Boolean", BooleanAttribute.class);
42  mapping.put("Date", DateAttribute.class);
43  mapping.put("DateTime", DateTimeAttribute.class);
44  mapping.put("Decimal", DecimalAttribute.class);
45  mapping.put("GeoPoint", GeoPointAttribute.class);
46  mapping.put("geoPoint", GeoPointAttribute.class);
47  mapping.put("Image", ImageAttribute.class);
48  mapping.put("Integer", IntegerAttribute.class);
49  mapping.put("NestedAnnotation", NestedAnnotationAttribute.class);
50  mapping.put("nestedAnnotation", NestedAnnotationAttribute.class);
51  mapping.put("Person", PersonAttribute.class);
52  mapping.put("person", PersonAttribute.class);
53  mapping.put("String", StringAttribute.class);
54  mapping.put("Time", TimeAttribute.class);
55  mapping.put("URI", UriAttribute.class);
56  mapping.put("Duration", DurationAttribute.class);
57  mapping.put("Binary", BinaryAttribute.class);
58  mapping.put("Text", TextAttribute.class);
59  mapping.put("Entity", EntityAttribute.class);
60  mapping.put("entity", EntityAttribute.class);
61  mapping.put("http://www.w3.org/2001/XMLSchema#string", StringAttribute.class);
62  mapping.put("http://knot.fit.vutbr.cz/annotations/knotOAExtension#entity", EntityAttribute.class);
63  mapping.put("http://knot.fit.vutbr.cz/annotations/knotOAExtension#text", TextAttribute.class);
64  mapping.put("http://www.w3.org/2001/XMLSchema#base64binary", BinaryAttribute.class);
65  mapping.put("http://www.w3.org/2001/XMLSchema#duration", DurationAttribute.class);
66  mapping.put("http://www.w3.org/2001/XMLSchema#time", TimeAttribute.class);
67  mapping.put("http://www.w3.org/2001/XMLSchema#anyUri", UriAttribute.class);
68  mapping.put("http://www.w3.org/2003/01/geo/wgs84_pos#Point", GeoPointAttribute.class);
69  mapping.put("http://knot.fit.vutbr.cz/annotations/knotOAExtension#imageUri", ImageAttribute.class);
70  mapping.put("http://www.w3.org/2001/XMLSchema#integer", IntegerAttribute.class);
71  mapping.put("http://knot.fit.vutbr.cz/annotations/knotOAExtension#anyAnnotation", AnyAnnotationAttribute.class);
72  mapping.put("http://www.w3.org/2001/XMLSchema#boolean", BooleanAttribute.class);
73  mapping.put("http://www.w3.org/2001/XMLSchema#date", DateAttribute.class);
74  mapping.put("http://www.w3.org/2001/XMLSchema#dateTime", DateTimeAttribute.class);
75  mapping.put("http://www.w3.org/2001/XMLSchema#decimal", DecimalAttribute.class);
76 
77  }
78 
79 
80  /**
81  * Creates a new instance according to type
82  *
83  * @param type Type of new instance
84  * @return Returns a new instance of appropriate class
85  */
86  private static BaseAttribute createObject(String type) throws ClassNotFoundException {
87  type = type.substring(0, 1).toUpperCase() + type.substring(1);
88  try {
89  BaseAttribute ret = (BaseAttribute) mapping.get(type).newInstance();
90  ret.setSimpleType(type);
91  return ret;
92  } catch (Exception e) {
93  throw new ClassNotFoundException("Class representing attribute " + type + " not found");
94  }
95  }
96 
97  /**
98  * Creates new empty attribute
99  *
100  * @param name Name of attribute
101  * @param simpleType Name of simple type of attribute
102  * @param refAnnotation Annotation to which this attribute belongs
103  * @return Returns new empty attribute
104  */
105  public static BaseAttribute createAttribute(String name, String simpleType, Annotation refAnnotation) throws ClassNotFoundException {
106  BaseAttribute ret = createObject(simpleType);
107  ret.setName(name);
108  ret.setRefAnnotation(refAnnotation);
109  return ret;
110  }
111 
112  /**
113  * Creates new attribute
114  *
115  * @param name Name of attribute
116  * @param simpleType Name of simple type of attribute
117  * @param value Value of attribute according to its type. For example, if
118  * type is Date then value is expected to be a date.
119  * @param refAnnotation Annotation to which this attribute belongs
120  * @return Returns new attribute
121  */
122  public static BaseAttribute createAttribute(String name, String simpleType, Object value, Annotation refAnnotation) throws ClassNotFoundException {
123  BaseAttribute ret = createAttribute(name, simpleType, refAnnotation);
124  ret.setValue(value);
125  ret.setRefAnnotation(refAnnotation);
126  return ret;
127  }
128 
129 
130  /**
131  * Creates a new Attribute with structured type. (For example with nested
132  * annotation)
133  *
134  * @param name Name of attribute
135  * @param simpleType Name of simple type of attribute
136  * @param attributeType Structured type of attribute (type of annotation)
137  * @param value Nested value. Usually nested annotation
138  * @param refAnnotation Annotation to which this attribute belongs
139  * @return Returns new attribute of structured type
140  */
141  public static BaseAttribute createAttribute(String name, String simpleType, AnnotType attributeType, Object value, Annotation refAnnotation) throws ClassNotFoundException {
142  BaseAttribute ret = AttributeManager.createAttribute(name, simpleType, value, refAnnotation);
143  ret.setAttributeType(attributeType);
144  return ret;
145  }
146 
147  /**
148  * Returns true if String type is correct type of attribute
149  *
150  * @param type Type of attribute
151  * @return True if it can be created, false if not
152  */
153  public static boolean containsType(String type){
154  return mapping.containsKey(type);
155  }
156 
157  /**
158  * Returns true if attr has the right class instance of it's simpletype
159  *
160  * @param attr Attribute for check
161  * @return True if simpletype matches the class instance, false otherwise
162  */
163  public static boolean hasAttributeRightInstance(BaseAttribute attr){
164  if(mapping.get(attr.getSimpleType()).isInstance(attr)){
165  return true;
166  }
167  else{
168  return false;
169  }
170  }
171 
172  /**
173  * Cast the right Attribute class
174  * Attention! If simpletype is some bad string, then method returns the same attribute as
175  * inserted in parameters!
176  *
177  * @param attr querried nested attribute
178  * @return Right class instance
179  */
181  try{
183  ret.updateFromBaseAttributeAll(attr);
184  return ret;
185  }
186  catch(ClassNotFoundException ex){
187  return attr;
188  }
189  }
190 
191 } // class AttributeManager
static BaseAttribute createAttribute(String name, String simpleType, Object value, Annotation refAnnotation)
static BaseAttribute createAttribute(String name, String simpleType, Annotation refAnnotation)
Base class representing attribute of annotation.
Class representing type of annotation.
Definition: AnnotType.java:58
static BaseAttribute changeAttributeInstance(BaseAttribute attr)
Class attribute manager provides a way how to create new attributes.
static BaseAttribute createAttribute(String name, String simpleType, AnnotType attributeType, Object value, Annotation refAnnotation)