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

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

lintegrity unit test reconstructed so it does not mock any more. getAnnotation works (the others are "ignored"). Needs refactoring (the subdirectory with beans and DummySecurityFilter? class.

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