2 * Copyright (C) 2011 Morphoss Ltd
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.morphoss
.acal
.activity
.serverconfig
;
22 import java
.io
.FileInputStream
;
23 import java
.io
.FileNotFoundException
;
24 import java
.io
.FileOutputStream
;
25 import java
.io
.IOException
;
26 import java
.io
.InputStream
;
27 import java
.net
.URLDecoder
;
28 import java
.net
.URLEncoder
;
29 import java
.util
.ArrayList
;
30 import java
.util
.List
;
31 import java
.util
.Map
.Entry
;
33 import javax
.xml
.parsers
.ParserConfigurationException
;
34 import javax
.xml
.parsers
.SAXParser
;
35 import javax
.xml
.parsers
.SAXParserFactory
;
37 import org
.xml
.sax
.Attributes
;
38 import org
.xml
.sax
.SAXException
;
39 import org
.xml
.sax
.helpers
.DefaultHandler
;
40 import org
.xmlpull
.v1
.XmlSerializer
;
42 import android
.content
.ContentValues
;
43 import android
.util
.Log
;
44 import android
.util
.Xml
;
46 import com
.morphoss
.acal
.Constants
;
47 import com
.morphoss
.acal
.database
.AcalDBHelper
;
48 import com
.morphoss
.acal
.providers
.Servers
;
50 public class ServerConfigData
{
52 static public final String TAG
= "aCal ServerConfigData";
53 private ContentValues server
;
55 public ServerConfigData(ContentValues cv
) {
59 /**This method should be called by any class that is going to write a ContentValues
60 * object to the Server DB when the cv originated from this class.
62 * @param cv The content Values object to prune
64 public static final void removeNonDBFields(final ContentValues cv
) {
65 if (cv
.containsKey("INFO")) cv
.remove("INFO"); //information string describing server
68 public ContentValues
getContentValues() {
69 if ( server
.getAsString(Servers
.SUPPLIED_USER_URL
) == null
70 && server
.getAsString(Servers
.OLD_SUPPLIED_DOMAIN
) != null ) {
71 server
.put(Servers
.SUPPLIED_USER_URL
,
72 server
.getAsString(Servers
.OLD_SUPPLIED_DOMAIN
) +
73 (server
.getAsString(Servers
.OLD_SUPPLIED_PATH
) == null ?
"/" : server
.getAsString(Servers
.OLD_SUPPLIED_PATH
)));
79 public void writeToFile(File file
) throws IOException
{
80 FileOutputStream fileos
= null;
82 fileos
= new FileOutputStream(file
);
83 } catch (FileNotFoundException e
) {
84 throw new IOException("File not found: "+e
);
87 XmlSerializer serializer
= Xml
.newSerializer();
89 //we set the FileOutputStream as output for the serializer, using UTF-8 encoding
90 serializer
.setOutput(fileos
, "UTF-8");
91 //Write <?xml declaration with encoding (if encoding not null) and standalone flag (if standalone not null)
92 serializer
.startDocument("utf-8",null);
93 //set indentation option
94 serializer
.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
95 //set our namespace prefix
96 serializer
.setPrefix("", Constants
.NS_ACALCONFIG
);
98 serializer
.startTag(Constants
.NS_ACALCONFIG
, "acal");
99 serializer
.startTag(null, "db_version");
100 serializer
.text(AcalDBHelper
.DB_VERSION
+"");
101 serializer
.endTag(null, "db_version");
102 serializer
.startTag(null, Servers
.DATABASE_TABLE
);
103 serializer
.startTag(null, Servers
.FRIENDLY_NAME
);
104 serializer
.text(server
.getAsString(Servers
.FRIENDLY_NAME
));
105 serializer
.endTag(null, Servers
.FRIENDLY_NAME
);
106 serializer
.startTag(null, "config_values");
109 for (Entry
<String
,Object
> s
: server
.valueSet()) {
110 if (s
.getValue() == null) continue;
111 serializer
.startTag(null, s
.getKey());
113 serializer
.text(URLEncoder
.encode(s
.getValue().toString(),"utf-8"));
114 } catch (NullPointerException e
) {}
115 serializer
.endTag(null, s
.getKey());
119 serializer
.endTag(null, "config_values");
120 serializer
.endTag(null, Servers
.DATABASE_TABLE
);
121 serializer
.endTag(Constants
.NS_ACALCONFIG
, "acal");
122 serializer
.endDocument();
123 //write xml data into the FileOutputStream
125 //finally we close the file stream
129 public static List
<ServerConfigData
> getServerConfigDataFromFile(InputStream in
) {
130 //use Sax to deconstruct xml
131 SAXParserFactory spf
= SAXParserFactory
.newInstance();
132 ServerDataSaxParser sdsp
= new ServerDataSaxParser();
135 //get a new instance of parser
136 SAXParser sp
= spf
.newSAXParser();
138 //parse the file and also register this class for call backs
141 }catch(SAXException se
) {
142 if (Constants
.LOG_DEBUG
)Log
.d(TAG
,Log
.getStackTraceString(se
));
143 }catch(ParserConfigurationException pce
) {
144 if (Constants
.LOG_DEBUG
)Log
.d(TAG
,Log
.getStackTraceString(pce
));
145 }catch (IOException ie
) {
146 if (Constants
.LOG_DEBUG
)Log
.d(TAG
,Log
.getStackTraceString(ie
));
149 return sdsp
.getList();
152 public static List
<ServerConfigData
> getServerConfigDataFromFile(File file
) {
154 return getServerConfigDataFromFile(new FileInputStream(file
));
156 catch ( FileNotFoundException e
) {
157 Log
.e(TAG
,"File '"+file
.getAbsolutePath()+"' not found", e
);
162 private static class ServerDataSaxParser
extends DefaultHandler
{
163 private StringBuffer tempVal
= new StringBuffer();
164 private boolean inServerSection
= false;
165 private List
<ServerConfigData
> serversList
;
166 ContentValues currentValues
;
168 public ServerDataSaxParser() {
169 serversList
= new ArrayList
<ServerConfigData
>();
172 public List
<ServerConfigData
> getList() {
173 return this.serversList
;
177 public void startElement(String uri
, String localName
, String qName
, Attributes attributes
) throws SAXException
{
178 if (localName
.equalsIgnoreCase(Servers
.DATABASE_TABLE
)) {
179 if (!inServerSection
) {
180 currentValues
= new ContentValues();
181 inServerSection
= !inServerSection
;
184 tempVal
= new StringBuffer();
188 public void endElement(String uri
, String localName
, String qName
) throws SAXException
{
189 if (!inServerSection
) return;
191 if (localName
.equalsIgnoreCase(Servers
.DATABASE_TABLE
)) {
192 serversList
.add(new ServerConfigData(currentValues
));
193 currentValues
= null;
194 inServerSection
= !inServerSection
;
197 if (localName
.equalsIgnoreCase(Servers
.FRIENDLY_NAME
)) {
198 currentValues
.put(Servers
.FRIENDLY_NAME
,tempVal
.toString());
200 if (localName
.equalsIgnoreCase("INFO")) {
201 currentValues
.put("INFO",tempVal
.toString());
203 else if (localName
.equalsIgnoreCase(Servers
.HOSTNAME
)) {
204 currentValues
.put(Servers
.HOSTNAME
,tempVal
.toString());
206 else if (localName
.equalsIgnoreCase(Servers
.SUPPLIED_USER_URL
)) {
207 currentValues
.put(Servers
.SUPPLIED_USER_URL
,tempVal
.toString());
209 else if (localName
.equalsIgnoreCase(Servers
.USERNAME
)) {
210 currentValues
.put(Servers
.USERNAME
,tempVal
.toString());
212 else if (localName
.equalsIgnoreCase(Servers
.PASSWORD
)) {
213 currentValues
.put(Servers
.PASSWORD
,tempVal
.toString());
215 else if (localName
.equalsIgnoreCase(Servers
.PORT
)) {
216 currentValues
.put(Servers
.PORT
,tempVal
.toString());
218 else if (localName
.equalsIgnoreCase(Servers
.AUTH_TYPE
)) {
219 currentValues
.put(Servers
.AUTH_TYPE
,tempVal
.toString());
221 else if (localName
.equalsIgnoreCase(Servers
.ACTIVE
)) {
222 currentValues
.put(Servers
.ACTIVE
,tempVal
.toString());
224 else if (localName
.equalsIgnoreCase(Servers
.USE_SSL
)) {
225 currentValues
.put(Servers
.USE_SSL
,tempVal
.toString());
227 else if (localName
.equalsIgnoreCase(Servers
.OLD_SUPPLIED_PATH
)) {
228 currentValues
.put(Servers
.OLD_SUPPLIED_PATH
,tempVal
.toString());
230 else if (localName
.equalsIgnoreCase(Servers
.OLD_SUPPLIED_DOMAIN
)) {
231 currentValues
.put(Servers
.OLD_SUPPLIED_DOMAIN
,tempVal
.toString());
236 public void characters(char[] ch
, int start
, int length
) throws SAXException
{
237 //try utf-8 decoding first, otherwise just dump the literal string
239 tempVal
.append(URLDecoder
.decode(new String(ch
,start
,length
),"utf-8"));
240 } catch (Exception e
) { tempVal
.append(new String(ch
,start
,length
)); }