1 | /* |
---|
2 | * Copyright (C) 2013 DASISH |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or |
---|
5 | * modify it under the terms of the GNU General Public License |
---|
6 | * as published by the Free Software Foundation; either version 2 |
---|
7 | * of the License, or (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
17 | */ |
---|
18 | package eu.dasish.annotation.backend; |
---|
19 | |
---|
20 | import eu.dasish.annotation.schema.Principal; |
---|
21 | import java.io.ByteArrayInputStream; |
---|
22 | import java.io.IOException; |
---|
23 | import java.io.InputStream; |
---|
24 | import java.util.Map; |
---|
25 | import java.util.UUID; |
---|
26 | import javax.xml.parsers.DocumentBuilder; |
---|
27 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
28 | import javax.xml.parsers.ParserConfigurationException; |
---|
29 | import org.springframework.security.authentication.encoding.ShaPasswordEncoder; |
---|
30 | import org.w3c.dom.Document; |
---|
31 | import org.w3c.dom.Element; |
---|
32 | import org.w3c.dom.ls.DOMImplementationLS; |
---|
33 | import org.w3c.dom.ls.LSSerializer; |
---|
34 | import org.xml.sax.SAXException; |
---|
35 | import java.util.Random; |
---|
36 | /** |
---|
37 | * |
---|
38 | * @author olhsha |
---|
39 | */ |
---|
40 | public class Helpers { |
---|
41 | |
---|
42 | //exception messages |
---|
43 | //final static public String INVALID_BODY_EXCEPTION = "Invalide annotation body: both, text and xml options, are null."; |
---|
44 | final static String hexa = "ABCDEabcde"; |
---|
45 | final static int hexan= hexa.length(); |
---|
46 | |
---|
47 | public static String replace(String text, Map<String, ?> pairs) { |
---|
48 | StringBuilder result = new StringBuilder(text); |
---|
49 | for (String old : pairs.keySet()) { |
---|
50 | if (old != null) { |
---|
51 | if (!old.equals("")) { |
---|
52 | replaceString(result, old, pairs.get(old)); |
---|
53 | } |
---|
54 | } |
---|
55 | } |
---|
56 | return result.toString(); |
---|
57 | } |
---|
58 | |
---|
59 | public static StringBuilder replaceString(StringBuilder source, String oldFragment, Object newObject) { |
---|
60 | if (oldFragment != null) { |
---|
61 | int lengthOld = oldFragment.length(); |
---|
62 | String newFragment; |
---|
63 | if (newObject != null) { |
---|
64 | if (newObject instanceof Integer) { |
---|
65 | newFragment = ((Integer) newObject).toString(); |
---|
66 | } else { |
---|
67 | if (newObject instanceof String) { |
---|
68 | newFragment = (String) newObject; |
---|
69 | } else { |
---|
70 | newFragment = newObject.toString(); |
---|
71 | } |
---|
72 | } |
---|
73 | } else { |
---|
74 | newFragment = " "; |
---|
75 | } |
---|
76 | int lengthNew = newFragment.length(); |
---|
77 | int indexOf = source.indexOf(oldFragment); |
---|
78 | while (indexOf > 0) { |
---|
79 | source.delete(indexOf, indexOf + lengthOld); |
---|
80 | source.insert(indexOf, newFragment); |
---|
81 | indexOf = source.indexOf(oldFragment, indexOf + lengthNew); |
---|
82 | } |
---|
83 | } |
---|
84 | return source; |
---|
85 | } |
---|
86 | |
---|
87 | public static Element stringToElement(String string) throws ParserConfigurationException, IOException, SAXException { |
---|
88 | DocumentBuilder dbf = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
---|
89 | InputStream is = new ByteArrayInputStream(string.getBytes("UTF-16")); |
---|
90 | Document doc = dbf.parse(is); |
---|
91 | return doc.getDocumentElement(); |
---|
92 | |
---|
93 | } |
---|
94 | |
---|
95 | public static String elementToString(Element element) { |
---|
96 | Document document = element.getOwnerDocument(); |
---|
97 | DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation(); |
---|
98 | LSSerializer serializer = domImplLS.createLSSerializer(); |
---|
99 | String result = serializer.writeToString(element); |
---|
100 | return result; |
---|
101 | } |
---|
102 | |
---|
103 | public static String welcomeString(String baseUri, String remoteID) { |
---|
104 | String welcome = "<!DOCTYPE html><body>" |
---|
105 | + "You are logged in as " |
---|
106 | + remoteID + ".<br>" |
---|
107 | + "<h3>Welcome to DASISH Webannotator (DWAN)</h3><br>" |
---|
108 | + "<a href=\"" + baseUri + "\"> To DWAN REST overview page</a>" |
---|
109 | + "</body>"; |
---|
110 | return welcome; |
---|
111 | } |
---|
112 | |
---|
113 | public static Principal createPrincipalElement(String name, String e_mail) { |
---|
114 | Principal result = new Principal(); |
---|
115 | result.setDisplayName(name); |
---|
116 | result.setEMail(e_mail); |
---|
117 | return result; |
---|
118 | } |
---|
119 | |
---|
120 | public static String hashPswd(String pswd, int strength, String salt) { |
---|
121 | ShaPasswordEncoder encoder = new ShaPasswordEncoder(strength); |
---|
122 | return encoder.encodePassword(pswd, salt); |
---|
123 | } |
---|
124 | |
---|
125 | public static UUID generateUUID(){ |
---|
126 | UUID result = UUID.randomUUID(); |
---|
127 | char[] chars = result.toString().toCharArray(); |
---|
128 | if (chars[0] >= 'a' && chars[0] <='z') { |
---|
129 | return result; |
---|
130 | } else { |
---|
131 | Random r = new Random(); |
---|
132 | chars[0] = hexa.charAt(r.nextInt(hexan)); |
---|
133 | result = UUID.fromString(new String(chars)); |
---|
134 | return result; |
---|
135 | } |
---|
136 | } |
---|
137 | } |
---|