01: /*
02: * This file is part of PFIXCORE.
03: *
04: * PFIXCORE is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU Lesser General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * PFIXCORE 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 Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public License
15: * along with PFIXCORE; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18:
19: package de.schlund.pfixcore.editor2.core.spring;
20:
21: import java.io.File;
22: import java.io.FileNotFoundException;
23: import java.io.IOException;
24: import java.util.HashMap;
25: import java.util.Iterator;
26: import java.util.List;
27: import java.util.Map;
28:
29: import javax.xml.parsers.ParserConfigurationException;
30: import javax.xml.transform.TransformerException;
31:
32: import org.apache.log4j.Logger;
33: import org.w3c.dom.Document;
34: import org.w3c.dom.Element;
35: import org.w3c.dom.Node;
36: import org.xml.sax.SAXException;
37:
38: import de.schlund.pfixcore.editor2.core.exception.EditorInitializationException;
39: import de.schlund.pfixxml.util.XPath;
40:
41: public class ConfigurationServiceImpl implements ConfigurationService {
42: private HashMap<String, String> map;
43: private PathResolverService pathresolver;
44: private FileSystemService filesystem;
45: private String projectsFile;
46:
47: public void setPathResolverService(PathResolverService pathresolver) {
48: this .pathresolver = pathresolver;
49: }
50:
51: public void setFileSystemService(FileSystemService filesystem) {
52: this .filesystem = filesystem;
53: }
54:
55: public void setProjectsFilePath(String path) {
56: this .projectsFile = path;
57: }
58:
59: public void init() throws FileNotFoundException, SAXException,
60: IOException, ParserConfigurationException,
61: EditorInitializationException {
62: Document doc = filesystem.readXMLDocumentFromFile(new File(
63: pathresolver.resolve(projectsFile)));
64: List<Node> nlist;
65: this .map = new HashMap<String, String>();
66: try {
67: nlist = XPath.select(doc.getDocumentElement(),
68: "common/namespaces/namespace-declaration");
69: } catch (TransformerException e) {
70: // Should never happen
71: String err = "XPath error!";
72: Logger.getLogger(this .getClass()).error(err, e);
73: throw new RuntimeException(err, e);
74: }
75: for (Iterator<Node> i = nlist.iterator(); i.hasNext();) {
76: Element node = (Element) i.next();
77: if (!node.hasAttribute("prefix")) {
78: String err = "Mandatory attribute prefix is missing for tag namespace-declaration!";
79: Logger.getLogger(this .getClass()).error(err);
80: throw new EditorInitializationException(err);
81: }
82: if (!node.hasAttribute("url")) {
83: String err = "Mandatory attribute url is missing for tag namespace-declaration!";
84: Logger.getLogger(this .getClass()).error(err);
85: throw new EditorInitializationException(err);
86: }
87: String prefix = node.getAttribute("prefix");
88: String url = node.getAttribute("url");
89: this .map.put(prefix, url);
90: }
91: }
92:
93: public Map<String, String> getPrefixToNamespaceMappings() {
94: return new HashMap<String, String>(this.map);
95: }
96:
97: }
|