Skip to content

Commit c565546

Browse files
author
Alexander Lippling
committed
Added support for data
Change-Id: Ibbf1264312473bd9462b1f49bdf50a71c9ac777a
1 parent 545c79e commit c565546

11 files changed

Lines changed: 211 additions & 4 deletions

File tree

src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/Array.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ public interface Array extends List<Object>
5454

5555
void addDate(Date value);
5656

57+
byte[] getData(int index);
58+
59+
String getDataAsUTF8String(int index);
60+
61+
void setData(int index, byte[] value);
62+
63+
void setDataAsUTF8String(int index, String value);
64+
65+
void addData(byte[] value);
66+
67+
void addDataAsUTF8String(String value);
68+
5769
Dict getDict(int index);
5870

5971
void addDict(Dict value);

src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/Dict.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ public interface Dict extends Map<String, Object>
4444

4545
void setDate(String key, Date value);
4646

47+
byte[] getData(String key);
48+
49+
String getDataAsUTF8String(String key);
50+
51+
void setData(String key, byte[] value);
52+
53+
void setDataAsUTF8String(String key, String value);
54+
4755
Array getArray(String key);
4856

4957
Array getOrCreateAndSetArray(String key);

src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/ArrayListArray.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
package com.sap.prd.mobile.ios.mios.xcodeprojreader.jaxb;
2121

22+
import java.io.UnsupportedEncodingException;
2223
import java.util.ArrayList;
2324
import java.util.Date;
2425

@@ -119,6 +120,64 @@ public void addDate(Date value)
119120
add(value);
120121
}
121122

123+
@Override
124+
public byte[] getData(int index)
125+
{
126+
return (byte[]) get(index);
127+
}
128+
129+
@Override
130+
public String getDataAsUTF8String(int index)
131+
{
132+
byte[] bytes = (byte[]) get(index);
133+
try
134+
{
135+
return new String(bytes, "UTF-8");
136+
}
137+
catch (UnsupportedEncodingException e)
138+
{
139+
throw new RuntimeException(e);
140+
}
141+
}
142+
143+
@Override
144+
public void setData(int index, byte[] value)
145+
{
146+
set(index, value);
147+
}
148+
149+
@Override
150+
public void setDataAsUTF8String(int index, String value)
151+
{
152+
try
153+
{
154+
set(index, value.getBytes("UTF-8"));
155+
}
156+
catch (UnsupportedEncodingException e)
157+
{
158+
throw new RuntimeException(e);
159+
}
160+
}
161+
162+
@Override
163+
public void addData(byte[] value)
164+
{
165+
add(value);
166+
}
167+
168+
@Override
169+
public void addDataAsUTF8String(String value)
170+
{
171+
try
172+
{
173+
add(value.getBytes("UTF-8"));
174+
}
175+
catch (UnsupportedEncodingException e)
176+
{
177+
throw new RuntimeException(e);
178+
}
179+
}
180+
122181
@Override
123182
public Dict getDict(int index)
124183
{

src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBArray.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public class JAXBArray
4141
@XmlElement(name = "real", type = Double.class),
4242
@XmlElement(name = "true", type = JAXBTrue.class),
4343
@XmlElement(name = "false", type = JAXBFalse.class),
44-
@XmlElement(name = "date", type = JAXBDate.class)
44+
@XmlElement(name = "date", type = JAXBDate.class),
45+
@XmlElement(name = "data", type = JAXBData.class)
4546
})
4647
public Array getElements()
4748
{
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* #%L
3+
* xcode-project-reader
4+
* %%
5+
* Copyright (C) 2012 SAP AG
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.sap.prd.mobile.ios.mios.xcodeprojreader.jaxb;
21+
22+
import javax.xml.bind.annotation.XmlType;
23+
import javax.xml.bind.annotation.XmlValue;
24+
25+
@XmlType(name = "data")
26+
public class JAXBData
27+
{
28+
private String value;
29+
30+
@XmlValue
31+
public String getValue()
32+
{
33+
return value;
34+
}
35+
36+
void setValue(String value)
37+
{
38+
this.value = value;
39+
}
40+
}

src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBDict.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public class JAXBDict
4040
@XmlElement(name = "real", type = Double.class),
4141
@XmlElement(name = "true", type = JAXBTrue.class),
4242
@XmlElement(name = "false", type = JAXBFalse.class),
43-
@XmlElement(name = "date", type = JAXBDate.class)
43+
@XmlElement(name = "date", type = JAXBDate.class),
44+
@XmlElement(name = "data", type = JAXBData.class)
4445
})
4546
public Array getElements()
4647
{

src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBPlistElementConverter.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ else if (value instanceof JAXBDate)
6565
JAXBDate date = (JAXBDate) value;
6666
value = DatatypeConverter.parseDateTime(date.getValue()).getTime();
6767
}
68+
else if (value instanceof JAXBData)
69+
{
70+
JAXBData data = (JAXBData) value;
71+
value = DatatypeConverter.parseBase64Binary(data.getValue());
72+
}
6873
return value;
6974
}
7075

@@ -88,6 +93,13 @@ else if (value instanceof Date)
8893
date.setValue(format.format((Date) value));
8994
value = date;
9095
}
96+
else if (value instanceof byte[])
97+
{
98+
JAXBData data = new JAXBData();
99+
byte[] bytes = (byte[]) value;
100+
data.setValue(DatatypeConverter.printBase64Binary(bytes));
101+
value = data;
102+
}
91103
return value;
92104
}
93105
}

src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/JAXBPlistParser.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141

4242
import com.sap.prd.mobile.ios.mios.xcodeprojreader.Plist;
4343

44-
// TODO unsupported element type: Data
4544
public class JAXBPlistParser
4645
{
4746
public Plist load(String projectFile) throws SAXException, ParserConfigurationException, FileNotFoundException,

src/main/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/jaxb/LinkedHashMapDict.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
package com.sap.prd.mobile.ios.mios.xcodeprojreader.jaxb;
2121

22+
import java.io.UnsupportedEncodingException;
2223
import java.util.Date;
2324
import java.util.LinkedHashMap;
2425

@@ -89,6 +90,45 @@ public void setDate(String key, Date value)
8990
put(key, value);
9091
}
9192

93+
@Override
94+
public byte[] getData(String key)
95+
{
96+
return (byte[]) get(key);
97+
}
98+
99+
@Override
100+
public String getDataAsUTF8String(String key)
101+
{
102+
byte[] bytes = (byte[]) get(key);
103+
try
104+
{
105+
return new String(bytes, "UTF-8");
106+
}
107+
catch (UnsupportedEncodingException e)
108+
{
109+
throw new RuntimeException(e);
110+
}
111+
}
112+
113+
@Override
114+
public void setData(String key, byte[] value)
115+
{
116+
put(key, value);
117+
}
118+
119+
@Override
120+
public void setDataAsUTF8String(String key, String value)
121+
{
122+
try
123+
{
124+
put(key, value.getBytes("UTF-8"));
125+
}
126+
catch (UnsupportedEncodingException e)
127+
{
128+
throw new RuntimeException(e);
129+
}
130+
}
131+
92132
@Override
93133
public Array getArray(String key)
94134
{

src/test/java/com/sap/prd/mobile/ios/mios/xcodeprojreader/DataTypesTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,38 @@ public void dateInArray()
139139
assertEquals(newDate, array.getDate(7));
140140
}
141141

142+
@Test
143+
public void dataInDictAsBytes() throws Exception
144+
{
145+
assertEquals("hello world", new String(dict.getData("data"), "UTF-8"));
146+
dict.setData("data", "test".getBytes("UTF-8"));
147+
assertEquals("test", new String(dict.getData("data"), "UTF-8"));
148+
}
149+
150+
@Test
151+
public void dataInArrayAsBytes() throws Exception
152+
{
153+
assertEquals("hello world", new String(array.getData(8), "UTF-8"));
154+
array.setData(8, "test".getBytes("UTF-8"));
155+
assertEquals("test", new String(array.getData(8), "UTF-8"));
156+
}
157+
158+
@Test
159+
public void dataInDictAsString() throws Exception
160+
{
161+
assertEquals("hello world", dict.getDataAsUTF8String("data"));
162+
dict.setDataAsUTF8String("data", "test");
163+
assertEquals("test", dict.getDataAsUTF8String("data"));
164+
}
165+
166+
@Test
167+
public void dataInArrayAsString() throws Exception
168+
{
169+
assertEquals("hello world", array.getDataAsUTF8String(8));
170+
array.setDataAsUTF8String(8, "test");
171+
assertEquals("test", array.getDataAsUTF8String(8));
172+
}
173+
142174
@Test
143175
public void dictInDict()
144176
{
@@ -175,7 +207,7 @@ public void dictInArrayNonExistingIndex()
175207
public void getOrCreateAndSetArrayInDict()
176208
{
177209
Array array = dict.getOrCreateAndSetArray("arraytest");
178-
assertEquals(8, array.size());
210+
assertEquals(9, array.size());
179211
assertEquals("value", array.get(0));
180212
}
181213

0 commit comments

Comments
 (0)