KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > fileextraction > ExtractedDocumentImpl


1 package org.jahia.services.fileextraction;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Date JavaDoc;
5 import java.util.HashMap JavaDoc;
6 ;
7
8 /**
9  *
10  * @author hollis
11  */

12 public class ExtractedDocumentImpl implements ExtractedDocument {
13
14     private HashMap JavaDoc properties = new HashMap JavaDoc();
15     private String JavaDoc content;
16     
17     public ExtractedDocumentImpl(){
18     }
19     
20     public ExtractedDocumentImpl(String JavaDoc content){
21         this(content,null);
22     }
23     
24     public ExtractedDocumentImpl(String JavaDoc content, HashMap JavaDoc properties){
25         this.content = content;
26         if ( properties != null ){
27             this.properties = properties;
28         }
29     }
30
31     public void setContent(String JavaDoc content){
32         this.content = content;
33     }
34     
35     public void setProperties(HashMap JavaDoc properties){
36         if ( properties != null ){
37             this.properties = properties;
38         }
39     }
40     
41     public void setProperty(String JavaDoc name, Object JavaDoc[] values){
42         if ( name != null && values != null ){
43             this.getProperties().put(name,values);
44         }
45     }
46
47     public void setProperty(String JavaDoc name, Object JavaDoc value){
48         if ( name != null && value != null ){
49             Object JavaDoc[] values = {value};
50             this.getProperties().put(name, values);
51         }
52     }
53
54     /**
55      *
56      * @return returns the main content as tring
57      */

58     public String JavaDoc getContentAsString(){
59         return this.content;
60     }
61     
62     /**
63      *
64      * @return the additional extracted metadatas
65      */

66     public HashMap JavaDoc getProperties(){
67         if ( this.properties == null) {
68             this.properties = new HashMap JavaDoc();
69         }
70         return this.properties;
71     }
72
73     /**
74      *
75      * @return the values of properties as a String Array
76      */

77     public String JavaDoc[] getPropertiesAsStrings(String JavaDoc name){
78         String JavaDoc[] results = null;
79         ArrayList JavaDoc arrayList = new ArrayList JavaDoc();
80         Object JavaDoc[] objs = (Object JavaDoc[])this.getProperties().get(name);
81         Object JavaDoc obj = null;
82         String JavaDoc strVal = null;
83         for ( int i=0; i<objs.length; i++){
84             strVal = null;
85             obj = objs[i];
86             try {
87                 if ( obj instanceof String JavaDoc ){
88                     strVal = (String JavaDoc)obj;
89                 } else if ( obj instanceof Date JavaDoc ){
90                     strVal = String.valueOf(((Date JavaDoc)obj).getTime());
91                 } else {
92                     strVal = String.valueOf(obj);
93                 }
94             } catch ( Throwable JavaDoc t ){
95             }
96             if ( strVal != null) {
97                 arrayList.add(strVal);
98             }
99         }
100         
101         results = new String JavaDoc[arrayList.size()];
102         for ( int i=0; i<arrayList.size(); i++ ){
103             results[i] = (String JavaDoc)arrayList.get(i);
104         }
105         return results;
106     }
107
108     /**
109      * Returns the value with index=0 in case of multivalues
110      *
111      * @return the value of property as a String
112      */

113     public String JavaDoc getPropertyAsString(String JavaDoc name){
114         Object JavaDoc[] objs = (Object JavaDoc[])this.getProperties().get(name);
115         Object JavaDoc obj = null;
116         String JavaDoc strVal = null;
117         for ( int i=0; i<objs.length; i++){
118             strVal = null;
119             obj = objs[i];
120             try {
121                 if ( obj instanceof String JavaDoc ){
122                     strVal = (String JavaDoc)obj;
123                 } else if ( obj instanceof Date JavaDoc ){
124                     strVal = String.valueOf(((Date JavaDoc)obj).getTime());
125                 } else {
126                     strVal = String.valueOf(obj);
127                 }
128             } catch ( Throwable JavaDoc t ){
129             }
130             break;
131         }
132         return strVal;
133     }
134
135 }
136
Popular Tags