4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
NestedAnnotationAttribute.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Ing. Jaroslav Dytrych idytrych@fit.vutbr.cz, Jan Planer xplane01@stud.fit.vutbr.cz
4  * File: NestedAnnotationAttribute.java
5  * Description: Class representing attribute of type NestedAnnotation
6  */
7 
8 package cz.vutbr.fit.knot.annotations.entity.attribute;
9 
13 import java.util.Iterator;
14 import java.util.logging.Level;
15 import java.util.logging.Logger;
16 import javax.persistence.DiscriminatorValue;
17 import javax.persistence.Entity;
18 import javax.persistence.NamedQueries;
19 import javax.persistence.NamedQuery;
20 
21 /**
22  * @file NestedAnnotationAttribute.java
23  *
24  * @brief Class representing attribute of type NestedAnnotation
25  */
26 
27 /**
28  * Class representing attribute of type NestedAnnotation
29  *
30  * @brief Class representing attribute of type NestedAnnotation
31  */
32 @Entity
33 @DiscriminatorValue("NestedAnnotation")
34 @NamedQueries({
35  @NamedQuery(name = "Attribute.findByNested", query = "SELECT a FROM Attribute a WHERE a.nested = :nested"),
36 })
38 
39  /**
40  * Constructor
41  */
43  }
44 
45  /**
46  * Constructor
47  *
48  * @param name Name of the attribute
49  * @param nestedAnnotation Value of the attribute
50  * @param refAnnotation Annotation to which this attribute belongs.
51  */
52  public NestedAnnotationAttribute(String name, Annotation nestedAnnotation, Annotation refAnnotation) {
53  this.name = name;
54  this.simpleType = "NestedAnnotation";
55  this.nestedAnnotation = nestedAnnotation;
56  this.refAnnotation = refAnnotation;
57  }
58 
59  /**
60  * Gets value of the attribute
61  *
62  * @return value of the attribute
63  */
64  @Override
65  public Object getValue() {
66  return this.nestedAnnotation;
67  }
68 
69  /**
70  * Sets value of the attribute. Value should already be a native java value.
71  *
72  * @param value New value of the attribute
73  */
74  @Override
75  public void setValue(Object value) {
76  if(value != null){
77  this.nestedAnnotation = (Annotation) value;
78  this.uri = this.nestedAnnotation.getURI();
79  }else{
80  this.nestedAnnotation = null;
81  this.uri = null;
82  }
83  }
84 
85  /**
86  * Parses provided value and sets that value as a value of attribute
87  *
88  * This method should not be used, because this type of attribute is processed
89  * in MessageProcessor.
90  *
91  * @param value new value of the attribute in raw form from XML
92  */
93  @Override
94  public void setRawValue(String value) throws IllegalArgumentException {
95  throw new UnsupportedOperationException("Server internal error.");
96  }
97 
98  /**
99  * Returns serialized informations about attribute of annotation in XML
100  *
101  * @param proto11 If true, protocol version is greater then 1.0
102  * @param tmpIdForNested If true, nested annotations will have attribute tmpId
103  * @param withOntology If true, URI in ontology will be serialized, if false, it will be omitted
104  * @return Returns serialized informations about attribute of annotation in XML
105  */
106  @Override
107  public String toXMLString(boolean proto11, boolean tmpIdForNested, boolean withOntology) {
108 
109  String sCom = "/";
110  String sCom2 = "";
111  String ontoString = "";
112  if (proto11 && comment != null && !comment.isEmpty()) {
113  sCom = "><a:comment>"
114  + "<![CDATA["
115  + comment
116  + "]]>"
117  + "</a:comment></a:attribute";
118  sCom2 = "<a:comment>"
119  + "<![CDATA["
120  + comment
121  + "]]>"
122  + "</a:comment>";
123  }
124 
125  if(withOntology){
126  if(!getTypeOntologyUri().isEmpty()){
127  ontoString += " typeOntologyUri=\""+getTypeOntologyUri()+"\"";
128  }
129 
130  Iterator<AnnotTypeAttr> typeAttrIt = refAnnotation.getAnnotType().getAttributes().iterator();
131  while(typeAttrIt.hasNext()){
132  AnnotTypeAttr actualAttr = typeAttrIt.next();
133  if(name.equals(actualAttr.getName())){
134  if(actualAttr.getUriInOntology() != null){
135  ontoString += " ontologyUri=\"" + actualAttr.getUriInOntology() + "\"";
136  }
137  }
138  }
139  }
140 
141  if (nestedAnnotation == null) {
142  if (attributeType != null) {
143  return "<a:attribute name=\"" + name + "\"" + ontoString + " type=\"" + attributeType.getUri() + "\"" + sCom + ">";
144  } else {
145  // this is not reachable
147  String msg = "Unknown type of attribute created.";
148  Logger.getLogger(NestedAnnotationAttribute.class.getName()).log(Level.SEVERE, msg);
149  }
150  return "<a:attribute name=\"" + name + "\"" + ontoString + " type=\"\"" + sCom + ">";
151  }
152 
153  }
154 
155  return "<a:attribute name=\"" + name + "\"" + ontoString + " type=\"nestedAnnotation\" "
156  + "tmpId=\"" + nestedAnnotation.getTmpId() + "\">"
157  + nestedAnnotation.toXMLString(false, "", proto11, tmpIdForNested)
158  + sCom2
159  + "</a:attribute>";
160  } // toXMLString()
161 
162  /**
163  * Returns serialized informations about attribute of annotation in XML
164  * for protocol v. 2.0 without header (target and attribute specification
165  * triples)
166  *
167  * @param ontologyUri URI of attribute in ontology
168  * @return Returns serialized informations about attribute of annotation in XML
169  */
170  public String toXMLStringWHV2(String ontologyUri){
171  StringBuilder result = new StringBuilder();
172 
173  if(this.nestedAnnotation != null){
174  // serialize triple with information about nesting
175  result.append("<trix:uri>");
176  result.append(this.nestedAnnotation.getURIV2());
177  result.append("</trix:uri>");
178  result.append("</trix:triple>");
179 
180  // <annotation uri><nestedIn><annotation uri>
181  result.append("<trix:triple>");
182 
183  result.append("<trix:uri>");
184  result.append(this.nestedAnnotation.getURIV2());
185  result.append("</trix:uri>");
186 
187  result.append("<trix:uri>koae:nestedIn</trix:uri>");
188 
189  result.append("<trix:uri>");
190  result.append(this.refAnnotation.getURIV2());
191  result.append("</trix:uri>");
192 
193  result.append("</trix:triple>");
194 
195 
196  // <uri of document><attr name><annotation uri>
197  result.append("<trix:triple>");
198  result.append("<trix:uri>");
199  result.append(this.refAnnotation.getSource()).append(this.refAnnotation.getFragmentXpointersV2());
200  result.append("</trix:uri>");
201 
202  if (ontologyUri == null || ontologyUri.isEmpty()) {
203  result.append("<trix:name>");
204  result.append(this.name);
205  result.append("</trix:name>");
206  } else {
207  result.append("<trix:uri>");
208  result.append(ontologyUri);
209  result.append("</trix:uri>");
210  }
211 
212  result.append("<trix:uri>");
213  result.append(this.refAnnotation.getAnnotType().getUri());
214  result.append("</trix:uri>");
215 
216  result.append("</trix:triple>");
217 
218 
219  // <document uri><attr name><uri of type>
220  result.append("<trix:triple>");
221  result.append("<trix:uri>");
222  result.append(this.refAnnotation.getSource()).append(this.refAnnotation.getFragmentXpointersV2());
223  result.append("</trix:uri>");
224 
225  // serialize triple with name
226  if (ontologyUri == null || ontologyUri.isEmpty()) {
227  result.append("<trix:name>");
228  result.append(this.name);
229  result.append("</trix:name>");
230  } else {
231  result.append("<trix:uri>");
232  result.append(ontologyUri);
233  result.append("</trix:uri>");
234  }
235 
236  // serialize triple with information about nesting
237  result.append("<trix:uri>");
238  result.append("koae:nestedAnnotation");
239  result.append("</trix:uri>");
240  result.append("</trix:triple>");
241  }else{
242  // nested
243  result.append("<trix:uri>");
244  if(getAttributeType() == null){
245 
246  }
247  else{
248  result.append(getAttributeType().getUri());
249  }
250  result.append("</trix:uri>");
251  result.append("</trix:triple>");
252 
253  // <document uri><attr name><koae:nested>
254  result.append("<trix:triple>");
255  result.append("<trix:uri>");
256  result.append(this.refAnnotation.getSource()).append(this.refAnnotation.getFragmentXpointersV2());
257  result.append("</trix:uri>");
258 
259  // serialize triple with name
260  if (ontologyUri == null || ontologyUri.isEmpty()) {
261  result.append("<trix:name>");
262  result.append(this.name);
263  result.append("</trix:name>");
264  } else {
265  result.append("<trix:uri>");
266  result.append(ontologyUri);
267  result.append("</trix:uri>");
268  }
269 
270  // serialize triple with information about nesting
271  result.append("<trix:uri>");
272  result.append("koae:nestedAnnotation");
273  result.append("</trix:uri>");
274  result.append("</trix:triple>");
275  }
276 
277  return result.toString();
278  }
279 
280  /**
281  * Gets URI addres in ontology for this type of attribute
282  *
283  * @return Return URI addres in ontology for this type of attribute.
284  */
285  @Override
286  public String getTypeOntologyUri(){
287  if(this.attributeType != null)
288  {
289  return this.attributeType.getUriInOntology();
290  }else
291  {
292  return "";
293  }
294  }
295 } // class NestedAnnotationAttribute
Class representing attribute of type of annotation.
NestedAnnotationAttribute(String name, Annotation nestedAnnotation, Annotation refAnnotation)
Base class representing attribute of annotation.
String toXMLString(boolean proto11, boolean tmpIdForNested, boolean withOntology)