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 java.io.IOException; |
---|
21 | import java.sql.Timestamp; |
---|
22 | import java.util.GregorianCalendar; |
---|
23 | import java.util.Map; |
---|
24 | import java.util.TimeZone; |
---|
25 | import javax.xml.datatype.DatatypeConfigurationException; |
---|
26 | import javax.xml.datatype.DatatypeFactory; |
---|
27 | import javax.xml.datatype.XMLGregorianCalendar; |
---|
28 | import javax.xml.parsers.DocumentBuilder; |
---|
29 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
30 | import javax.xml.parsers.ParserConfigurationException; |
---|
31 | import org.w3c.dom.Document; |
---|
32 | import org.w3c.dom.Element; |
---|
33 | import org.w3c.dom.ls.DOMImplementationLS; |
---|
34 | import org.w3c.dom.ls.LSSerializer; |
---|
35 | import org.xml.sax.SAXException; |
---|
36 | import org.joda.time.DateTime; |
---|
37 | import org.joda.time.DateTimeZone; |
---|
38 | |
---|
39 | /** |
---|
40 | * |
---|
41 | * @author olhsha |
---|
42 | */ |
---|
43 | public class Helpers { |
---|
44 | |
---|
45 | //exception messages |
---|
46 | final static public String INVALID_BODY_EXCEPTION = "Invalide annotation body: both, text and xml options, are null."; |
---|
47 | |
---|
48 | public static String replace(String text, Map<String, String> pairs) { |
---|
49 | String result = (new StringBuilder(text)).toString(); |
---|
50 | for (String tempTarget : pairs.keySet()) { |
---|
51 | result = result.replaceAll(tempTarget, pairs.get(tempTarget)); |
---|
52 | } |
---|
53 | return result; |
---|
54 | } |
---|
55 | |
---|
56 | public static Element stringToElement(String string) { |
---|
57 | try { |
---|
58 | DocumentBuilder dbf = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
---|
59 | try { |
---|
60 | try { |
---|
61 | Document doc = dbf.parse(string); |
---|
62 | return doc.getDocumentElement(); |
---|
63 | } catch (SAXException saxException) { |
---|
64 | System.out.println(saxException); |
---|
65 | } |
---|
66 | } catch (IOException ioe) { |
---|
67 | System.out.println(ioe); |
---|
68 | } |
---|
69 | } catch (ParserConfigurationException parserException) { |
---|
70 | System.out.println(parserException); |
---|
71 | } |
---|
72 | return null; |
---|
73 | } |
---|
74 | |
---|
75 | public static String elementToString(Element element) { |
---|
76 | Document document = element.getOwnerDocument(); |
---|
77 | DOMImplementationLS domImplLS = (DOMImplementationLS) document |
---|
78 | .getImplementation(); |
---|
79 | LSSerializer serializer = domImplLS.createLSSerializer(); |
---|
80 | String result = serializer.writeToString(element); |
---|
81 | return result; |
---|
82 | } |
---|
83 | } |
---|