Skip to content
This repository was archived by the owner on Sep 26, 2025. It is now read-only.

Commit 2d1eb80

Browse files
committed
Attribute support for the json->xml transformation
1 parent 8114b97 commit 2d1eb80

4 files changed

Lines changed: 249 additions & 10 deletions

File tree

JSONObject.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ of this software and associated documentation files (the "Software"), to deal
3030
import java.lang.reflect.Field;
3131
import java.lang.reflect.Method;
3232
import java.lang.reflect.Modifier;
33+
import java.util.ArrayList;
3334
import java.util.Collection;
3435
import java.util.Enumeration;
3536
import java.util.HashMap;
3637
import java.util.Iterator;
38+
import java.util.List;
3739
import java.util.Locale;
3840
import java.util.Map;
3941
import java.util.Map.Entry;
@@ -324,6 +326,64 @@ public JSONObject(String source) throws JSONException {
324326
this(new JSONTokener(source));
325327
}
326328

329+
/**
330+
* Method for checking does the object containing attributes
331+
* attributes are keys with @ as first character
332+
* @return returns true value if attributes are present
333+
*/
334+
public boolean hasAttributeKeys(){
335+
Set set = this.map.keySet();
336+
Iterator keyIterator = set.iterator();
337+
while(keyIterator.hasNext()){
338+
String key = (String) keyIterator.next();
339+
if(key.startsWith("@")){
340+
return true;
341+
}
342+
}
343+
return false;
344+
}
345+
346+
/**
347+
* Find attribute values from children objects
348+
* @return Map containing attribute key value pairs
349+
*/
350+
protected Map<String,String> removeAttributeKeys(){
351+
HashMap<String,String> attributeKeys = new HashMap<String,String>();
352+
//search attributes and add them to map
353+
Set set = this.map.keySet();
354+
Iterator keyIterator = set.iterator();
355+
while(keyIterator.hasNext()){
356+
String key = (String) keyIterator.next();
357+
if(key.startsWith("@")){
358+
Object objValue = map.get(key);
359+
if(objValue instanceof String){ //attributes does not have arrays
360+
attributeKeys.put(key,objValue.toString());
361+
}
362+
}
363+
}
364+
//remove children attributes
365+
Set attrSet = attributeKeys.keySet();
366+
Iterator keyAttrIterator = attrSet.iterator();
367+
while(keyAttrIterator.hasNext()){
368+
String key = (String) keyAttrIterator.next();
369+
this.map.remove(key);
370+
}
371+
return attributeKeys;
372+
}
373+
374+
public String transformXmlAtrributeString(){
375+
String txt = "";
376+
Map<String,String> attributes = this.removeAttributeKeys();
377+
Iterator it = attributes.entrySet().iterator();
378+
while (it.hasNext()) {
379+
Map.Entry pairs = (Map.Entry)it.next();
380+
txt = txt + pairs.getKey().toString().substring(1) + "=\"" + pairs.getValue() + "\" ";
381+
}
382+
return txt;
383+
}
384+
385+
386+
327387
/**
328388
* Construct a JSONObject from a ResourceBundle.
329389
*

XML.java

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ of this software and associated documentation files (the "Software"), to deal
2525
*/
2626

2727
import java.util.Iterator;
28+
import java.util.List;
29+
import java.util.Map;
2830

2931
/**
3032
* This provides static methods to convert an XML text into a JSONObject,
@@ -209,7 +211,7 @@ private static boolean parse(XMLTokener x, JSONObject context,
209211

210212
// Open tag <
211213

212-
} else {
214+
} else {
213215
tagName = (String)token;
214216
token = null;
215217
jsonobject = new JSONObject();
@@ -227,9 +229,12 @@ private static boolean parse(XMLTokener x, JSONObject context,
227229
token = x.nextToken();
228230
if (!(token instanceof String)) {
229231
throw x.syntaxError("Missing value");
230-
}
231-
jsonobject.accumulate(string,
232+
}else{
233+
//attributes
234+
jsonobject.accumulate("@"+string,
232235
XML.stringToValue((String)token));
236+
}
237+
233238
token = null;
234239
} else {
235240
jsonobject.accumulate(string, "");
@@ -390,11 +395,25 @@ public static String toString(Object object, String tagName)
390395
if (object instanceof JSONObject) {
391396

392397
// Emit <tagName>
393-
394-
if (tagName != null) {
395-
sb.append('<');
396-
sb.append(tagName);
397-
sb.append('>');
398+
//TODO: check does the map (object) contain keys with @ value
399+
// if contain add opening elemnt without > close character
400+
// then add attributes and remove them from map and close the eleent
401+
if (tagName != null) {
402+
if(object instanceof JSONObject){
403+
JSONObject jsonObj =(JSONObject)object;
404+
if(jsonObj.hasAttributeKeys()){
405+
String attributes = jsonObj.transformXmlAtrributeString();
406+
sb.append('<');
407+
sb.append(tagName);
408+
sb.append(" ");
409+
sb.append(attributes);
410+
sb.append('>');
411+
}
412+
}else{
413+
sb.append('<');
414+
sb.append(tagName);
415+
sb.append('>');
416+
}
398417
}
399418

400419
// Loop thru the keys.
@@ -431,8 +450,8 @@ public static String toString(Object object, String tagName)
431450
ja = (JSONArray)value;
432451
length = ja.length();
433452
for (i = 0; i < length; i += 1) {
434-
value = ja.get(i);
435-
if (value instanceof JSONArray) {
453+
value = ja.get(i);
454+
if (value instanceof JSONArray) {
436455
sb.append('<');
437456
sb.append(key);
438457
sb.append('>');
@@ -452,6 +471,11 @@ public static String toString(Object object, String tagName)
452471
// Emit a new tag <k>
453472

454473
} else {
474+
//check that if key is attribute (@)
475+
//TODO: logic here
476+
if(key.charAt(0)=='@'){
477+
key = key.substring(1);
478+
}
455479
sb.append(toString(value, key));
456480
}
457481
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
7+
package fi.flexim.json;
8+
9+
import org.json.JSONObject;
10+
import org.json.JSONML;
11+
import org.json.JSONString;
12+
import org.json.XML;
13+
import org.json.XMLTokener;
14+
import org.junit.After;
15+
import org.junit.AfterClass;
16+
import org.junit.Before;
17+
import org.junit.BeforeClass;
18+
import org.junit.Test;
19+
import static org.junit.Assert.*;
20+
21+
/**
22+
*
23+
* @author kaustni
24+
*/
25+
public class JsonXMLTransformJUnitTest {
26+
27+
public JsonXMLTransformJUnitTest() {
28+
}
29+
30+
@BeforeClass
31+
public static void setUpClass() {
32+
}
33+
34+
@AfterClass
35+
public static void tearDownClass() {
36+
}
37+
38+
@Before
39+
public void setUp() {
40+
}
41+
42+
@After
43+
public void tearDown() {
44+
}
45+
46+
// TODO add test methods here.
47+
// The methods must be annotated with annotation @Test. For example:
48+
//
49+
@Test
50+
public void transformXMLtoJSON() {
51+
String xmlStr = "<ws><daily-schema data=\"http://schemas.flexim.fi/flexim6/2012/data\" xmlns:ref=\"http://schemas.flexim.fi/flexim6/2012/ref\" ref:id=\"00000000000w_DSC\" in-use=\"restricted\"><period start=\"06:00:00\" end=\"18:00:00\"></period></daily-schema></ws>";
52+
String jsonStr = org.json.XML.toJSONObject(xmlStr).toString();
53+
System.out.println("json:"+jsonStr);
54+
55+
JSONObject json = new JSONObject(jsonStr);
56+
System.out.println("jsonObject:"+json);
57+
String xml = XML.toString(json);
58+
//String xml = XML.toString((Object)json);
59+
System.out.println("xml:"+xml);
60+
assertEquals(xmlStr,xml);
61+
}
62+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
7+
package org.json;
8+
9+
import java.util.List;
10+
import java.util.Map;
11+
import org.junit.After;
12+
import org.junit.AfterClass;
13+
import org.junit.Before;
14+
import org.junit.BeforeClass;
15+
import org.junit.Test;
16+
import static org.junit.Assert.*;
17+
18+
/**
19+
*
20+
* @author kaustni
21+
*/
22+
public class JSONObjectUnitTest {
23+
24+
String jsonTxtWithAttributes = "{\"@ref:id\":\"00000000000w_DSC\",\n" +
25+
"\"@data\":\"http://schemas.flexim.fi/flexim6/2012/data\",\n" +
26+
"\"@xmlns:ref\":\"http://schemas.flexim.fi/flexim6/2012/ref\",\n" +
27+
"\"period\":\n" +
28+
" {\"@end\":\"18:00:00\",\"@start\":\"06:00:00\"}\n" +
29+
",\"@in-use\":\"restricted\"}";
30+
String jsonStandard = "{\"ID\": \"SGML\",\n" +
31+
"\"SortAs\": \"SGML\",\n" +
32+
"\"GlossTerm\": \"Standard Generalized Markup Language\",\n" +
33+
"\"Acronym\": \"SGML\",\n" +
34+
"\"Abbrev\": \"ISO 8879:1986\",\n" +
35+
"\"GlossDef\": {\n" +
36+
" \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\n" +
37+
" \"GlossSeeAlso\": [\"GML\", \"XML\"]\n" +
38+
"}}";
39+
40+
JSONObject jsonObjWithAttributes = null;
41+
JSONObject jsonObjStandard = null;
42+
public JSONObjectUnitTest() {
43+
}
44+
45+
@BeforeClass
46+
public static void setUpClass() {
47+
}
48+
49+
@AfterClass
50+
public static void tearDownClass() {
51+
}
52+
53+
@Before
54+
public void setUp() {
55+
jsonObjStandard = new JSONObject(jsonStandard);
56+
jsonObjWithAttributes = new JSONObject(jsonTxtWithAttributes);
57+
}
58+
59+
@After
60+
public void tearDown() {
61+
}
62+
63+
/**
64+
* Test hasAttributeKeys
65+
*/
66+
@Test
67+
public void testHasAttributeKeys() {
68+
69+
assertFalse(jsonObjStandard.hasAttributeKeys());
70+
assertTrue(jsonObjWithAttributes.hasAttributeKeys());
71+
}
72+
73+
@Test
74+
public void testGetAttributeKeysNoAttributes() {
75+
Map<String,String> keys = jsonObjStandard.removeAttributeKeys();
76+
assertNotNull(keys);
77+
assertEquals(0,keys.size());
78+
}
79+
@Test
80+
public void testGetAttributeKeysAttributesPresent(){
81+
Map<String,String> keys = jsonObjWithAttributes.removeAttributeKeys();
82+
assertNotNull(keys);
83+
assertEquals(4,keys.size());
84+
}
85+
@Test
86+
public void transformXmlAtrributeString(){
87+
String standardStr = jsonObjStandard.transformXmlAtrributeString();
88+
assertEquals("",standardStr);
89+
String attributeStr = jsonObjWithAttributes.transformXmlAtrributeString();
90+
assertEquals("ref:id=\"00000000000w_DSC\" data=\"http://schemas.flexim.fi/flexim6/2012/data\" in-use=\"restricted\" xmlns:ref=\"http://schemas.flexim.fi/flexim6/2012/ref\" ",attributeStr);
91+
92+
}
93+
}

0 commit comments

Comments
 (0)