BrightSide Workbench Full Report + Source Code
ConfigurationSet.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.configuration;
19 
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.OutputStreamWriter;
23 import java.util.List;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.jdom.Document;
27 import org.jdom.Element;
28 import org.jdom.JDOMException;
29 import org.jdom.input.SAXBuilder;
30 import org.jdom.output.Format;
31 import org.jdom.output.XMLOutputter;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.impl.util.FileUtil;
34 
39 public class ConfigurationSet {
40 
41  private static final String
42  CONFIGURATION_FOLDER = "/WEB-INF/_zul-site/conf";
43  private final String confKey;
44  private Document doc;
45 
46  public ConfigurationSet(String confKey) {
47  this.confKey = confKey;
48  }
49 
50  public void setChildrenValue(Path path, String element, String name, List<String> values) {
51  try {
52  loadSet();
53  path.setDoc(doc);
54  path.setChildrenValue(element, name, values);
55  saveSet();
56  } catch (JDOMException ex) {
57  Logger.getLogger(ConfigurationSet.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
58  }
59  }
60 
61  public void setChildrenValues(Path path, String element, String name[], List<String[]> values) {
62  try {
63  loadSet();
64  path.setDoc(doc);
65  path.setChildrenValues(element, name, values);
66  saveSet();
67  } catch (JDOMException ex) {
68  Logger.getLogger(ConfigurationSet.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
69  }
70  }
71 
72  public void setNodeValue(Path path, String name, String value) {
73  try {
74  loadSet();
75  path.setDoc(doc);
76  path.setNodeValue(name, value);
77  saveSet();
78  } catch (JDOMException ex) {
79  Logger.getLogger(ConfigurationSet.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
80  }
81  }
82 
83  public List<String> getChildrenValue(Path path, String element, String name) {
84  try {
85  loadSet();
86  path.setDoc(doc);
87  return path.getChildrenValue(element, name);
88  } catch (JDOMException ex) {
89  Logger.getLogger(ConfigurationSet.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
90  }
91  return null;
92  }
93 
94  public List<String[]> getChildrenValues(Path path, String element, String names[]) {
95  try {
96  loadSet();
97  path.setDoc(doc);
98  return path.getChildrenValues(element, names);
99  } catch (JDOMException ex) {
100  Logger.getLogger(ConfigurationSet.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
101  }
102  return null;
103  }
104 
105  public String getNodeValue(Path path, String name) {
106  try {
107  loadSet();
108  path.setDoc(doc);
109  return path.getNodeValue(name);
110  } catch (JDOMException ex) {
111  Logger.getLogger(ConfigurationSet.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
112  }
113  return null;
114  }
115 
116  public Document getDocument() {
117  loadSet();
118  return doc;
119  }
120 
121  public void setDocument(Document doc) {
122  this.doc = doc;
123  saveSet();
124  }
125 
126  private void saveSet() {
127  try {
128  File folder = new File(ElephantContext.getRealPath(CONFIGURATION_FOLDER));
129  if(!folder.exists()) folder.mkdirs();
130  try (OutputStreamWriter fw = FileUtil.getFileWriter(
131  ElephantContext.getRealPath(CONFIGURATION_FOLDER + "/" + convertName(confKey) + ".xml"))) {
132  Format fm = Format.getPrettyFormat();
133  fm.setEncoding(ElephantContext.getEncoding());
134  XMLOutputter xo = new XMLOutputter(fm);
135  xo.output(doc, fw);
136  }
137  } catch (IOException ex) {
138  Logger.getLogger(ConfigurationSet.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
139  }
140  }
141 
142  private void loadSet() {
143  SAXBuilder builder = new SAXBuilder();
144  try {
145  File confFile = new File(ElephantContext.getRealPath(CONFIGURATION_FOLDER + "/" + convertName(confKey) + ".xml"));
146  if(confFile.exists()) {
147  doc = builder.build(confFile);
148  } else {
149  doc = new Document(new Element("elephant-configuration"));
150  }
151  } catch (IOException | JDOMException ex) {
152  Logger.getLogger(ConfigurationSet.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
153  }
154  }
155 
156  private String convertName(String confKey) {
157  return confKey.replaceAll("[^A-Za-z0-9]", "");
158  }
159 
160 }
List< String > getChildrenValue(Path path, String element, String name)
void setChildrenValue(Path path, String element, String name, List< String > values)
String getNodeValue(Path path, String name)
void setNodeValue(Path path, String name, String value)
List< String[]> getChildrenValues(Path path, String element, String names[])
void setChildrenValues(Path path, String element, String name[], List< String[]> values)
List< String[]> getChildrenValues(String element, String names[])
Definition: Path.java:88
List< String > getChildrenValue(String element, String name)
Definition: Path.java:79
void setChildrenValue(String element, String name, List< String > values)
Definition: Path.java:115
void setChildrenValues(String element, String names[], List< String[]> values)
Definition: Path.java:125
String getNodeValue(String name)
Definition: Path.java:101
void setDoc(Document doc)
Definition: Path.java:53
void setNodeValue(String name, String value)
Definition: Path.java:137
static OutputStreamWriter getFileWriter(String folder, String file)
Definition: FileUtil.java:158