1 /**
2  * EGroupware - Notifications Java Desktop App
3  *
4  * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
5  * @package notifications
6  * @subpackage jdesk
7  * @link http://www.egroupware.org
8  * @author Stefan Werfling <stefan.werfling@hw-softwareentwicklung.de>
9  * @author Maik Hüttner <maik.huettner@hw-softwareentwicklung.de>
10  */
11 
12 package egroupwaretray;
13 
14 import java.util.ArrayList;
15 import java.util.logging.Level;
16 import java.util.logging.Logger;
17 import javax.xml.stream.XMLEventFactory;
18 import javax.xml.stream.XMLEventReader;
19 import javax.xml.stream.XMLEventWriter;
20 import javax.xml.stream.XMLStreamException;
21 import javax.xml.stream.events.XMLEvent;
22 
23 /**
24  * jegwxmlconfig
25  *
26  * @author Stefan Werfling <stefan.werfling@hw-softwareentwicklung.de>
27  */
28 public class jegwxmlconfig extends hwxml
29 {
30     public static final String CONF_CONFIGURATIONS  = new String("configurations");
31     public static final String CONF_CONFIG          = new String("config");
32     public static final String CONF_ACTIVCONFIG     = new String("activconfig");
33 
34     public static final String[] CONF_STRUCT        = new String[]{
35         "host", "user", "password", "subdir", "egwurl", "logindomain",
36         "sslcert", "port"};
37 
38     private ArrayList ConfigList    = new ArrayList();
39     private KeyArray tmpConfig      = new KeyArray(jegwxmlconfig.CONF_STRUCT);
40     private Boolean tagstatus       = false;
41     private Integer activconfig     = 0;
42 
jegwxmlconfig(String xmlfile)43     public jegwxmlconfig(String xmlfile) throws Exception
44     {
45         super(xmlfile);
46     }
47 
addConf(KeyArray conf)48     public void addConf(KeyArray conf)
49     {
50         this.ConfigList.add(conf);
51     }
52 
setActivConf(Integer index)53     public void setActivConf(Integer index)
54     {
55         if( index < this.ConfigList.size() )
56         {
57             this.activconfig = index;
58         }
59     }
60 
getActivConf()61     public KeyArray getActivConf()
62     {
63         return this.getConf(this.activconfig);
64     }
65 
getConf(Integer index)66     public KeyArray getConf(Integer index)
67     {
68         if( index < this.ConfigList.size() )
69         {
70             return (KeyArray) this.ConfigList.get(index);
71         }
72 
73         return null;
74     }
75 
countConf()76     public Integer countConf()
77     {
78         Integer i = this.ConfigList.size();
79         return i;
80     }
81 
deleteConf(Integer index)82     public void deleteConf(Integer index)
83     {
84         if( index < this.ConfigList.size() )
85         {
86             ArrayList newlist = new ArrayList();
87 
88             for(int i=0; i<this.ConfigList.size(); i++ )
89             {
90                 if(i != index)
91                 {
92                     newlist.add(this.ConfigList.get(i));
93                 }
94             }
95 
96             this.ConfigList = newlist;
97         }
98     }
99 
mRead(XMLEvent event, XMLEventReader eventReader)100     @Override protected boolean mRead(XMLEvent event, XMLEventReader eventReader)
101     {
102         if( event.isStartElement() )
103         {
104             String tag = event.asStartElement().getName().getLocalPart();
105 
106             if(tag.compareTo(jegwxmlconfig.CONF_CONFIG) == 0)
107             {
108                 this.tagstatus = true;
109                 // Config Leeren für neue Config
110                 this.tmpConfig = new KeyArray(this.tmpConfig.getKeys());
111                 return false;
112             }
113 
114             // Aktive Configuration
115             if(tag.compareTo(jegwxmlconfig.CONF_ACTIVCONFIG) == 0)
116             {
117                 try
118                 {
119                     event = eventReader.nextEvent();
120                     String value = event.asCharacters().getData();
121                     this.activconfig = Integer.parseInt(value);
122                     return false;
123                 }
124                 catch (XMLStreamException ex)
125                 {
126                     Logger.getLogger(jegwxmlconfig.class.getName()).log(Level.SEVERE, null, ex);
127                 }
128             }
129 
130             if(!this.tagstatus)
131             {
132                 // Andere Elemente Interessieren nicht
133                 return false;
134             }
135 
136             String[] keys = this.tmpConfig.getKeys();
137 
138             for(int i=0; i<keys.length; i++)
139             {
140                 if(tag.compareTo(keys[i]) == 0)
141                 {
142                     try
143                     {
144                         event = eventReader.nextEvent();
145                         String value = "";
146 
147                         if((!event.isEndElement()) && (!event.isStartElement()))
148                         {
149                             value = event.asCharacters().getData();
150 
151                         }
152 
153                         this.tmpConfig.add(keys[i], value);
154                     }
155                     catch (XMLStreamException ex)
156                     {
157                         Logger.getLogger(jegwxmlconfig.class.getName()).log(Level.SEVERE, null, ex);
158                     }
159 
160                     return false;
161                 }
162             }
163         }
164         else if(event.isEndElement())
165         {
166             String tag = event.asEndElement().getName().getLocalPart();
167 
168             if(tag.compareTo(jegwxmlconfig.CONF_CONFIG) == 0)
169             {
170                 this.tagstatus = false;
171                 this.ConfigList.add(this.tmpConfig.clone());
172                 return false;
173             }
174         }
175 
176         return false;
177     }
178 
mWrite(XMLEventFactory eventFactory, XMLEventWriter eventWriter)179     @Override protected void mWrite(XMLEventFactory eventFactory, XMLEventWriter eventWriter)
180     {
181         XMLEvent end = eventFactory.createDTD("\r\n");
182         XMLEvent tab = eventFactory.createDTD("\t");
183 
184         try
185         {
186             eventWriter.add(this.makeStartTag(jegwxmlconfig.CONF_CONFIGURATIONS));
187             eventWriter.add(end);
188 
189             for( int i=0; i<this.ConfigList.size(); i++ )
190             {
191                 KeyArray conf = (KeyArray) this.ConfigList.get(i);
192 
193                 String[] keys = conf.getKeys();
194 
195                 eventWriter.add(tab);
196                 eventWriter.add(this.makeStartTag(jegwxmlconfig.CONF_CONFIG));
197                 eventWriter.add(end);
198 
199                 for( int e=0; e<keys.length; e++ )
200                 {
201                     eventWriter.add(tab);
202                     eventWriter.add(tab);
203                     eventWriter.add(this.makeStartTag(keys[e]));
204                     eventWriter.add(this.makeValue(conf.getString(keys[e])));
205                     eventWriter.add(this.makeEndTag(keys[e]));
206                     eventWriter.add(end);
207                 }
208 
209                 eventWriter.add(tab);
210                 eventWriter.add(this.makeEndTag(jegwxmlconfig.CONF_CONFIG));
211                 eventWriter.add(end);
212             }
213 
214             eventWriter.add(this.makeEndTag(jegwxmlconfig.CONF_CONFIGURATIONS));
215             eventWriter.add(end);
216 
217             // Aktiv Config
218             eventWriter.add(this.makeStartTag(jegwxmlconfig.CONF_ACTIVCONFIG));
219             eventWriter.add(this.makeValue(Integer.toString(this.activconfig)));
220             eventWriter.add(this.makeEndTag(jegwxmlconfig.CONF_ACTIVCONFIG));
221             eventWriter.add(end);
222         }
223         catch (XMLStreamException ex)
224         {
225             Logger.getLogger(jegwxmlconfig.class.getName()).log(Level.SEVERE, null, ex);
226         }
227     }
228 }
229