BrightSide Workbench Full Report + Source Code
TypeByValues.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.contacts.util;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.Map;
24 import java.util.Properties;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import org.turro.string.Strings;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.impl.util.FileUtil;
30 
35 public class TypeByValues {
36 
37  public static String getType(String value) {
38  for(Map.Entry entry: getProperties().entrySet()) {
39  if(matchValue((String) entry.getValue(), value)) {
40  return (String) entry.getKey();
41  }
42  }
43  return null;
44  }
45 
46  /* Singleton */
47 
48  private TypeByValues() {
49  }
50 
51  /* Statics */
52 
53  private static Properties properties;
54 
55  private static Properties getProperties() {
56  if(properties == null) {
57  try {
58  File file = new File(ElephantContext.getRealPath("/WEB-INF/elephant/conf/vCard.properties"));
59  properties = new Properties();
60  properties.load(FileUtil.getBufferedFile(file));
61  } catch (IOException ex) {
62  Logger.getLogger(TypeByValues.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
63  }
64  }
65  return properties;
66  }
67 
68  private static boolean matchValue(String values, String value) {
69  if(!Strings.isBlank(values)) {
70  return values.toLowerCase().matches(".*(,|^)" + value.toLowerCase() + "(,|$).*");
71  }
72  return false;
73  }
74 
75 }
static String getType(String value)