This project has retired. For details please refer to its Attic page.
PropertyMapperTika xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.client.mapper;
20  
21  import java.text.DateFormatSymbols;
22  import java.text.ParseException;
23  import java.text.SimpleDateFormat;
24  import java.util.ArrayList;
25  import java.util.Date;
26  import java.util.GregorianCalendar;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Locale;
30  import java.util.Map;
31  import java.util.Properties;
32  import java.util.Set;
33  
34  import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
35  import org.apache.chemistry.opencmis.commons.enums.Cardinality;
36  import org.apache.chemistry.opencmis.commons.enums.PropertyType;
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  public class PropertyMapperTika extends AbstractPropertyMapper {
41  
42      private static final Log LOG = LogFactory.getLog(PropertyMapperTika.class.getName());
43      
44      private Map<String, String> propMapTags = new HashMap<String, String> (); // tag to property id
45      private Map<String, String> tokenizerMap = new HashMap<String, String> (); // tag to tokenizer regexp
46  
47      public PropertyMapperTika() {
48          reset();
49      }
50      
51      public boolean initialize(String cfgPrefix, String typeKey, Properties properties) {
52          super.initialize(cfgPrefix, typeKey, properties);
53          buildIdMap(typeKey, properties);
54          
55          return true;
56      }
57      
58      public void reset() {
59      }
60      
61      public String getMappedPropertyId(String key) {
62          String propId = propMapTags.get(key);
63          return propId;
64      }
65  
66      public Object convertValue(String key, PropertyDefinition<?> propDef, final String strValue) {
67          Object value = null;
68          PropertyType pt = propDef.getPropertyType();
69          
70          if (null == pt)
71              value = null;
72          else {
73              switch (pt) {
74              case STRING:
75              case HTML:
76              case URI:
77              case ID:
78                  if (propDef.getCardinality() == Cardinality.SINGLE)
79                      value = strValue;
80                  else {
81                      String tokenizer = tokenizerMap.containsKey(key) ? tokenizerMap.get(key) : "\\W";
82                      String[] result = strValue.split(tokenizer);
83                      List<String> valList = new ArrayList<String>();
84                      for (String s : result)
85                          valList.add(s.trim());
86                      value = valList;
87                  }
88                  break;
89              case INTEGER:
90                  if (propDef.getCardinality() == Cardinality.SINGLE)
91                      value = Integer.valueOf(strValue);
92                  else {
93                          String tokenizer = tokenizerMap.containsKey(key) ? tokenizerMap.get(key) : "\\W";
94                          String[] result = strValue.split(tokenizer);
95                          List<Integer> valList = new ArrayList<Integer>();
96                          for (String s : result)
97                              valList.add(Integer.valueOf(s.trim()));
98                          value = valList;
99                      }
100                 break;
101             case DECIMAL:
102                 if (propDef.getCardinality() == Cardinality.SINGLE)
103                     value = Double.valueOf(strValue);                
104                 else {
105                     String tokenizer = tokenizerMap.containsKey(key) ? tokenizerMap.get(key) : "[\\s;:]";                        
106                     String[] result = strValue.split(tokenizer);
107                         List<Double> valList = new ArrayList<Double>();
108                         for (String s : result)
109                             valList.add(Double.valueOf(s.trim()));
110                         value = valList;
111                     }
112                 break;
113             case DATETIME:
114                 try {
115                     SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, new DateFormatSymbols(Locale.US));
116                     if (propDef.getCardinality() == Cardinality.SINGLE) {
117                         Date date = sdf.parse(strValue);
118                         GregorianCalendar cal = new GregorianCalendar();
119                         cal.setTime(date);
120                         value = date;
121                     } else {
122                         String tokenizer = tokenizerMap.containsKey(key) ? tokenizerMap.get(key) : "[;,:]";                        
123                         String[] result = strValue.split(tokenizer);
124                         List<GregorianCalendar> valList = new ArrayList<GregorianCalendar>();
125                         for (String s : result) {
126                             Date date = sdf.parse(s.trim());
127                             GregorianCalendar cal = new GregorianCalendar();
128                             cal.setTime(date);
129                             valList.add(cal);
130                         }
131                         value = valList;
132                     }
133                 } catch (ParseException e) {
134                     LOG.error("Could not parse date: " + strValue + " (check date format");
135                     LOG.error(e);
136                     value = null;
137                     e.printStackTrace();
138                 }
139                 break;
140             default:
141                 throw new MapperException("unknown property type " + pt);
142             }            
143         }
144         return value;
145     }
146     
147     void buildIdMap(String typeKey, Properties properties) {
148         Set<String> keys = properties.stringPropertyNames(); 
149         String prefix = propPrefix + ".id.";
150         String tokenizerPrefix = propPrefix + ".tokenizer.";
151 
152         for (String key : keys) {
153             if (key.startsWith(prefix)) {
154                 String id = key.substring(prefix.length());
155                 String cmisPropId = properties.getProperty(key).trim();
156                 if (null == cmisPropId)
157                     throw new MapperException("Configuration key " + key + " must have a value assigned");
158                 LOG.debug("Found mapping for type " + typeKey + " with " + id + " to " + cmisPropId);
159                 propMapTags.put(id,  cmisPropId);
160             }
161             if (key.startsWith(tokenizerPrefix)) {
162                 String id = key.substring(tokenizerPrefix.length());
163                 String regex = properties.getProperty(key).trim();
164                 if (null == regex)
165                     throw new MapperException("Configuration key " + key + " must have a value assigned");
166                 LOG.debug("Found tokenizer mapping for property " + id + " to " + regex);
167                 tokenizerMap.put(id, regex);
168             }
169         }
170     }
171     
172     int getSize() {
173         return propMapTags.size();
174     }
175     
176  }