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

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

adding trasnactional, refactoring and fixing bugs in updated annotations, removing try-catch from resource methods (The Greek's advice)

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