Comments (1)
Index: src/main/java/org/codehaus/jettison/Convention.java
--- src/main/java/org/codehaus/jettison/Convention.java (revision 159)
+++ src/main/java/org/codehaus/jettison/Convention.java (working copy)
@@ -23,7 +23,7 @@
public interface Convention {
- void processAttributesAndNamespaces(Node n, JSONObject object)
- void processAttributesAndNamespaces(Node n, JSONObject object, String key)
throws JSONException, XMLStreamException;
QName createQName(String name, Node node) throws XMLStreamException;
Index: src/main/java/org/codehaus/jettison/Node.java
--- src/main/java/org/codehaus/jettison/Node.java (revision 159)
+++ src/main/java/org/codehaus/jettison/Node.java (working copy)
@@ -53,7 +53,7 @@
this.namespaces = new LinkedHashMap();
this.attributes = new LinkedHashMap();
-
con.processAttributesAndNamespaces(this, object);
-
con.processAttributesAndNamespaces(this, object, name); keys = object.keys();
@@ -60,10 +60,12 @@
this.name = con.createQName(name, this);
}
-
public Node(String name, Convention con) throws XMLStreamException {
-
public Node(String name, Convention con) throws JSONException, XMLStreamException {
this.name = con.createQName(name, this);
this.namespaces = new HashMap();
this.attributes = new HashMap(); -
// must call the following method to extract the namesapce from the name
-
con.processAttributesAndNamespaces(this, object, name);
}
public Node(JSONObject object) {
Index: src/main/java/org/codehaus/jettison/badgerfish/BadgerFishConvention.java
--- src/main/java/org/codehaus/jettison/badgerfish/BadgerFishConvention.java (revision 159)
+++ src/main/java/org/codehaus/jettison/badgerfish/BadgerFishConvention.java (working copy)
@@ -32,9 +32,12 @@
super();
} -
public void processAttributesAndNamespaces(Node n, JSONObject object)
-
public void processAttributesAndNamespaces(Node n, JSONObject object, String key)
throws JSONException, XMLStreamException {
// Read in the attributes, and stop when there are no more -
if (object == null){
-
return;
-
} for (Iterator itr = object.keys(); itr.hasNext();) { String k = (String) itr.next();
Index: src/main/java/org/codehaus/jettison/mapped/MappedNamespaceConvention.java
--- src/main/java/org/codehaus/jettison/mapped/MappedNamespaceConvention.java (revision 159)
+++ src/main/java/org/codehaus/jettison/mapped/MappedNamespaceConvention.java (working copy)
@@ -92,12 +92,29 @@
/* (non-Javadoc)
* @see org.codehaus.xson.mapped.Convention#processNamespaces(org.codehaus.xson.Node, org.json.JSONObject)
*/
-
public void processAttributesAndNamespaces( Node n, JSONObject object ) throws JSONException {
-
public void processAttributesAndNamespaces( Node n, JSONObject object, String key) throws JSONException {
-
// namespace is defined on the key
-
int dot = key.lastIndexOf( '.' );
-
if ( dot != -1 ) {
-
String jns = key.substring( 0, dot );
-
String xns = getNamespaceURI( jns );
-
if (!ignoreNamespaces){
-
n.setNamespace( "", xns );
-
}
-
}else{
-
// you must set the empty namespace, because we do not use prefixes
-
if (!ignoreNamespaces){
-
n.setNamespace("", "");
-
}
-
}
-
if (object == null){
-
return;
-
} // Read in the attributes, and stop when there are no more for (Iterator<?> itr = object.keys(); itr.hasNext();) { String k = (String) itr.next();
-
if ( this.supressAtAttributes ) { if ( k.startsWith( attributeKey ) ) { k = k.substring( 1 );
@@ -109,7 +126,7 @@
this.jsonAttributesAsElements.add( k );
}
} -
if ( k.startsWith( attributeKey ) ) { Object o = object.opt( k ); // String value = object.optString( k );
@@ -122,11 +139,11 @@
// set namespace if one is specified on this attribute.
String prefix = (String) pitr.next();
String uri = jo.getString( prefix ); -
// if ( prefix.equals( "$" ) ) { // prefix = ""; // }
-
n.setNamespace( prefix, uri ); } }
@@ -137,7 +154,10 @@
// note that a non-prefixed attribute name implies NO namespace,
// i.e. as opposed to the in-scope default namespace
if ( k.contains( "." ) ) { -
name = createQName( k, n );
-
name = createQName( k, true );
-
if (!ignoreNamespaces){
-
n.setNamespace(name.getPrefix(), name.getNamespaceURI());
-
} } else { name = new QName( XMLConstants.DEFAULT_NS_PREFIX, k );
@@ -146,18 +166,10 @@
}
itr.remove();
} -
else {
-
// set namespace if one is specified on this attribute.
-
int dot = k.lastIndexOf( '.' );
-
if ( dot != -1 ) {
-
String jns = k.substring( 0, dot );
-
String xns = getNamespaceURI( jns );
-
n.setNamespace( "", xns );
-
}
-
} }
}
-
/*
-
(non-Javadoc)
-
@see javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String)
@@ -198,8 +210,10 @@public QName createQName( String rootName, Node node ) {
-
return createQName( rootName );
-
return createQName( rootName, false );
}
-
@SuppressWarnings("unused")
private void readAttribute( Node n, String k, JSONArray array ) throws JSONException {
@@ -211,21 +225,24 @@private void readAttribute( Node n, String name, String value ) throws JSONException {
-
QName qname = createQName( name );
-
QName qname = createQName( name, false ); n.getAttributes().put( qname, value );
}
-
private QName createQName( String name ) {
-
private QName createQName( String name, boolean isAttr ) {
int dot = name.lastIndexOf( '.' );
QName qname = null;
String local = name;
-
String prefix; if ( dot == -1 ) { dot = 0;
-
prefix = null; } else { local = local.substring( dot + 1 );
-
prefix = name.substring( 0, dot ); } String jns = name.substring( 0, dot );
@@ -235,11 +252,17 @@
qname = new QName( name );
}
else { -
qname = new QName( xns, local );
-
if (isAttr){
-
// attributes with namespace must have a prefix
-
qname = new QName( xns, local, prefix );
-
}else{
-
qname = new QName( xns, local );
-
} } return qname;
}
-
public String createAttributeKey( String p, String ns, String local ) {
Index: src/main/java/org/codehaus/jettison/mapped/MappedXMLStreamReader.java
--- src/main/java/org/codehaus/jettison/mapped/MappedXMLStreamReader.java (revision 159)
+++ src/main/java/org/codehaus/jettison/mapped/MappedXMLStreamReader.java (working copy)
@@ -53,7 +53,7 @@
this.node = new Node(null, rootName, obj, convention);
} else {
node = new Node(rootName, convention);
-
convention.processAttributesAndNamespaces(node, obj);
-
convention.processAttributesAndNamespaces(node, obj, rootName); currentValue = JSONObject.NULL.equals(top) ? null : top.toString(); } nodes.push(node);
Index: src/test/java/org/codehaus/jettison/mapped/MappedRoundTripTest.java
--- src/test/java/org/codehaus/jettison/mapped/MappedRoundTripTest.java (revision 0)
+++ src/test/java/org/codehaus/jettison/mapped/MappedRoundTripTest.java (working copy)
@@ -0,0 +1,128 @@
+package org.codehaus.jettison.mapped;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.Map;
+import java.util.TreeMap;
+
+import javax.xml.parsers.FactoryConfigurationError;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLEventWriter;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.codehaus.jettison.AbstractXMLInputFactory;
+import org.codehaus.jettison.AbstractXMLOutputFactory;
+
+import junit.framework.TestCase;
+
+/** Tests XML to JSoN and JSON to XML in a round trip. */
+public class MappedRoundTripTest extends TestCase {
+ - public void testRootElementWithNamespaceChildWithout() throws Exception {
-
String input = "<?xml version='1.0' encoding='UTF-8'?><root xmlns=\"http://simple\">" + //
-
"<WithoutNs xmlns=\"\">value</WithoutNs>" + //
-
"</root>";
-
Map<String, String> xml2JsonNs = new TreeMap<String, String>();
-
xml2JsonNs.put("http://simple", "simple");
-
doRoundTrip(input, input, xml2JsonNs, false);
- }
- public void testRootElementWithNamespaceChildWithoutButWithPrefixe() throws Exception {
-
String input = "<?xml version='1.0' encoding='UTF-8'?><p:root xmlns:p=\"http://simple\">" + //
-
"<WithoutNs>value</WithoutNs>" + //
-
"/p:root";
-
String expectedResult = "<?xml version='1.0' encoding='UTF-8'?><root xmlns=\"http://simple\">" + //
-
"<WithoutNs xmlns=\"\">value</WithoutNs>" + //
-
"</root>";
-
Map<String, String> xml2JsonNs = new TreeMap<String, String>();
-
xml2JsonNs.put("http://simple", "simple");
-
doRoundTrip(input, expectedResult, xml2JsonNs, false);
- }
- public void testAttributesWithAndWithoutNs() throws Exception {
-
String input = "<?xml version='1.0' encoding='UTF-8'?><root xmlns=\"http://simple\">" + //
-
"<WithoutNs xmlns=\"\" xmlns:ns=\"http://attrNs\" attr=\"a_value\" ns:attrNs=\"ans_v\">value</WithoutNs>" + //
-
"</root>";
-
Map<String, String> xml2JsonNs = new TreeMap<String, String>();
-
xml2JsonNs.put("http://simple", "simple");
-
xml2JsonNs.put("http://attrNs", "ns");
-
doRoundTrip(input, input, xml2JsonNs, false);
- }
- public void testIgnoreNs() throws Exception {
-
String input = "<?xml version='1.0' encoding='UTF-8'?><root xmlns=\"http://simple\">" + //
-
"<WithoutNs xmlns=\"\" xmlns:ns=\"http://attrNs\" attr=\"a_value\" ns:attrNs=\"ans_v\">value</WithoutNs>" + //
-
"</root>";
-
Map<String, String> xml2JsonNs = new TreeMap<String, String>();
-
xml2JsonNs.put("http://simple", "simple");
-
xml2JsonNs.put("http://attrNs", "ns");
-
String expected = "<?xml version='1.0' encoding='UTF-8'?><root>" + //
-
"<WithoutNs attr=\"a_value\" attrNs=\"ans_v\">value</WithoutNs>" + //
-
"</root>";
-
doRoundTrip(input, expected, xml2JsonNs, true);
- }
- private void doRoundTrip(String input, String expectedResult, Map<String, String> xml2JsonNs, boolean ignoreNamespaces)
-
throws FactoryConfigurationError, XMLStreamException, UnsupportedEncodingException, Exception {
-
//System.out.println(input);
-
byte[] json = xmlToJson(input.getBytes("UTF-8"), xml2JsonNs, ignoreNamespaces);
-
String result = jsonToXml(json, xml2JsonNs, ignoreNamespaces);
-
//System.out.println(result);
-
assertEquals(expectedResult, result);
- }
- private byte[] xmlToJson(byte[] xml, Map<String, String> xml2JsonNs, boolean ignoreNamespaces) throws FactoryConfigurationError,
-
XMLStreamException, UnsupportedEncodingException {
-
XMLInputFactory readerFactory = XMLInputFactory.newInstance();
-
XMLEventReader eventReader = readerFactory.createXMLEventReader(new ByteArrayInputStream(xml));
-
Configuration configuration = new Configuration();
-
configuration.setXmlToJsonNamespaces(xml2JsonNs);
-
configuration.setIgnoreNamespaces(ignoreNamespaces);
-
AbstractXMLOutputFactory writerFactory = new MappedXMLOutputFactory(configuration);
-
ByteArrayOutputStream os = new ByteArrayOutputStream();
-
XMLEventWriter eventWriter = writerFactory.createXMLEventWriter(os, "UTF-8");
-
eventWriter.add(eventReader);
-
eventWriter.close();
-
byte[] bytes = os.toByteArray();
-
//System.out.println(new String(bytes, "UTF-8"));
-
return bytes;
- }
- private String jsonToXml(byte[] json, Map<String, String> xml2JsonNs, boolean ignoreNamespaces) throws Exception {
-
Configuration configuration = new Configuration();
-
configuration.setXmlToJsonNamespaces(xml2JsonNs);
-
configuration.setIgnoreNamespaces(ignoreNamespaces);
-
AbstractXMLInputFactory inputFactory = new MappedXMLInputFactory(configuration);
-
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new ByteArrayInputStream(json), "UTF-8");
-
XMLInputFactory readerFactory = XMLInputFactory.newInstance();
-
XMLEventReader eventReader = readerFactory.createXMLEventReader(streamReader);
-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
XMLOutputFactory writerFactory = XMLOutputFactory.newInstance();
-
XMLEventWriter eventWriter = writerFactory.createXMLEventWriter(baos, "UTF-8");
-
eventWriter.add(eventReader);
-
eventWriter.close();
-
return new String(baos.toByteArray(), "UTF-8");
- }
+}
Property changes on: src/test/java/org/codehaus/jettison/mapped/MappedRoundTripTest.java
Added: svn:mime-type
-0,0 +1
+text/plain
\ No newline at end of property
Index: src/test/java/org/codehaus/jettison/mapped/MappedXMLStreamReaderTest.java
--- src/test/java/org/codehaus/jettison/mapped/MappedXMLStreamReaderTest.java (revision 159)
+++ src/test/java/org/codehaus/jettison/mapped/MappedXMLStreamReaderTest.java (working copy)
@@ -15,25 +15,38 @@
*/
package org.codehaus.jettison.mapped;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.StringReader;
+import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.TreeMap;
import javax.xml.namespace.QName;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.stream.Location;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.events.Namespace;
+import javax.xml.stream.events.StartElement;
+import javax.xml.stream.events.XMLEvent;
import junit.framework.TestCase;
+import org.codehaus.jettison.AbstractXMLInputFactory;
+import org.codehaus.jettison.AbstractXMLOutputFactory;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.codehaus.jettison.json.JSONTokener;
@@ -1022,4 +1035,58 @@
assertEquals(refEvents, mpdEvents);
}
- public void testNamspace() throws Exception {
-
JSONObject obj = new JSONObject("{\"p1.A\":{\"p2.B\":\"value\"}}");
-
Map<String, String> xml2JsonNs = new TreeMap<String, String>();
-
xml2JsonNs.put("http://namespaceOfElementA", "p1");
-
xml2JsonNs.put("http://namespaceOfElementB", "p2");
-
Configuration configuration = new Configuration();
-
configuration.setXmlToJsonNamespaces(xml2JsonNs);
-
MappedNamespaceConvention con = new MappedNamespaceConvention(configuration);
-
MappedXMLStreamReader reader = new MappedXMLStreamReader(obj, con);
-
XMLInputFactory readerFactory = XMLInputFactory.newInstance();
-
XMLEventReader eventReader = readerFactory.createXMLEventReader(reader);
-
assertTrue(eventReader.hasNext());
-
// document event
-
eventReader.next();
-
assertTrue(eventReader.hasNext());
-
XMLEvent event = eventReader.nextEvent();
-
assertTrue(event.isStartElement());
-
StartElement el = event.asStartElement();
-
assertEquals("A", el.getName().getLocalPart());
-
Iterator<Namespace> ns = el.getNamespaces();
-
assertTrue(ns.hasNext());
-
assertEquals("http://namespaceOfElementA", ns.next().getNamespaceURI());
-
assertTrue(eventReader.hasNext());
-
event = eventReader.nextEvent();
-
assertTrue(event.isStartElement());
-
el = event.asStartElement();
-
assertEquals("B", el.getName().getLocalPart());
-
ns = el.getNamespaces();
-
assertTrue(ns.hasNext());
-
assertEquals("http://namespaceOfElementB", ns.next().getNamespaceURI());
-
assertTrue(eventReader.hasNext());
-
event = eventReader.nextEvent();
-
assertTrue(event.isCharacters());
-
assertEquals("value", event.asCharacters().getData());
-
assertTrue(eventReader.hasNext());
-
event = eventReader.nextEvent();
-
assertTrue(event.isEndElement());
-
assertTrue(eventReader.hasNext());
-
event = eventReader.nextEvent();
-
assertTrue(event.isEndElement());
-
assertTrue(eventReader.hasNext());
-
event = eventReader.nextEvent();
-
assertTrue(event.isEndDocument());
-
assertFalse(eventReader.hasNext());
- }
- }
from jettison.
Related Issues (20)
- improve "A JSONObject text must begin with '{' at character 1 of" error message HOT 3
- Infinite loop in the parser finally leading to Stack Overflow HOT 1
- Question on backward compatibility of 1.5.1 release with 1.4.x and 1.3.7
- If the value in map is the map's self, the new new JSONObject(map) cause StackOverflowError which may lead to dos HOT 2
- Find a StackOverflowError in jettison HOT 2
- BDSA-2022-3278 was detected by security scan tool HOT 1
- Please revert the recursion depth default limit as the retardation it is. HOT 1
- Incorrect recursion depth check in JSONTokener in 1.5.2 causes it to fail for inputs with low recursion depth HOT 1
- Incorrect backslash escaping HOT 3
- If the value inside a Collection is itself, using it with JSONArray causes a StackOverflowError which may lead to DoS HOT 1
- JSON formatter/builders don't support all the data types defined in the JSON spec
- upgraded jettison 1.5.1 to 1.5.2 , throwing error were JSONObject used and force to handle try/catch or surround with JSONException HOT 1
- NoClassDefFoundError Exception with jettison 1.5.3
- Security scan tool (Black Duck Binary Analysis) detected CVE-2022-45688, is the latest version 1.5.4 really affected by this vulnerability? HOT 2
- optXXX() should return default value instead of throwing a RuntimeException
- Jettison serialize array object as like json object when array object contains only one element in it.
- Question on breaking change or incompatibility between version 1.2 and 1.5.4 HOT 1
- Security scan tool (Black Duck Binary Analysis) detected CVE-2023-5072, is the latest version 1.5.4 really affected by this vulnerability? HOT 2
- A OOM problem was found in jettison
- Jettison compatibility with JDK 17
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jettison.