source: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/Helpers.java @ 4146

Last change on this file since 4146 was 4146, checked in by olhsha, 10 years ago

saving the current "last update" time stamp in UTC time in the DB. Outputting it as UTC. Works with PostgreSQL, does not work with HSQL (had to comment unit tests)

File size: 3.0 KB
Line 
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 */
18package eu.dasish.annotation.backend;
19
20import java.io.IOException;
21import java.sql.Timestamp;
22import java.util.GregorianCalendar;
23import java.util.Map;
24import java.util.TimeZone;
25import javax.xml.datatype.DatatypeConfigurationException;
26import javax.xml.datatype.DatatypeFactory;
27import javax.xml.datatype.XMLGregorianCalendar;
28import javax.xml.parsers.DocumentBuilder;
29import javax.xml.parsers.DocumentBuilderFactory;
30import javax.xml.parsers.ParserConfigurationException;
31import org.w3c.dom.Document;
32import org.w3c.dom.Element;
33import org.w3c.dom.ls.DOMImplementationLS;
34import org.w3c.dom.ls.LSSerializer;
35import org.xml.sax.SAXException;
36import org.joda.time.DateTime;
37import org.joda.time.DateTimeZone;
38
39/**
40 *
41 * @author olhsha
42 */
43public 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}
Note: See TracBrowser for help on using the repository browser.