Skip to content

Commit 7df1d64

Browse files
authored
Merge pull request #232 from Falland/master
Adding writeRawValue method support to ToXmlGenerator.
2 parents e6ada96 + 756e4f7 commit 7df1d64

File tree

2 files changed

+191
-1
lines changed

2 files changed

+191
-1
lines changed

src/main/java/com/fasterxml/jackson/dataformat/xml/ser/ToXmlGenerator.java

+76
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,82 @@ public void writeUTF8String(byte[] text, int offset, int length) throws IOExcept
654654
/**********************************************************
655655
*/
656656

657+
@Override
658+
public void writeRawValue(String text) throws IOException {
659+
// [dataformat-xml#39]
660+
if (_stax2Emulation) {
661+
_reportUnimplementedStax2("writeRawValue");
662+
}
663+
try {
664+
_verifyValueWrite("write raw value");
665+
if (_nextName == null) {
666+
handleMissingName();
667+
}
668+
669+
if (_nextIsAttribute) {
670+
_xmlWriter.writeAttribute(_nextName.getNamespaceURI(), _nextName.getLocalPart(), text);
671+
} else {
672+
_xmlWriter.writeStartElement(_nextName.getNamespaceURI(), _nextName.getLocalPart());
673+
_xmlWriter.writeRaw(text);
674+
_xmlWriter.writeEndElement();
675+
}
676+
} catch (XMLStreamException e) {
677+
StaxUtil.throwXmlAsIOException(e);
678+
}
679+
}
680+
681+
@Override
682+
public void writeRawValue(String text, int offset, int len) throws IOException {
683+
// [dataformat-xml#39]
684+
if (_stax2Emulation) {
685+
_reportUnimplementedStax2("writeRawValue");
686+
}
687+
try {
688+
_verifyValueWrite("write raw value");
689+
if (_nextName == null) {
690+
handleMissingName();
691+
}
692+
693+
if (_nextIsAttribute) {
694+
_xmlWriter.writeAttribute(_nextName.getNamespaceURI(), _nextName.getLocalPart(), text.substring(offset, offset + len));
695+
} else {
696+
_xmlWriter.writeStartElement(_nextName.getNamespaceURI(), _nextName.getLocalPart());
697+
_xmlWriter.writeRaw(text, offset, len);
698+
_xmlWriter.writeEndElement();
699+
}
700+
} catch (XMLStreamException e) {
701+
StaxUtil.throwXmlAsIOException(e);
702+
}
703+
}
704+
705+
@Override
706+
public void writeRawValue(char[] text, int offset, int len) throws IOException {
707+
// [dataformat-xml#39]
708+
if (_stax2Emulation) {
709+
_reportUnimplementedStax2("writeRawValue");
710+
}
711+
_verifyValueWrite("write raw value");
712+
if (_nextName == null) {
713+
handleMissingName();
714+
}
715+
try {
716+
if (_nextIsAttribute) {
717+
_xmlWriter.writeAttribute(_nextName.getNamespaceURI(), _nextName.getLocalPart(), new String(text, offset, len));
718+
} else {
719+
_xmlWriter.writeStartElement(_nextName.getNamespaceURI(), _nextName.getLocalPart());
720+
_xmlWriter.writeRaw(text, offset, len);
721+
_xmlWriter.writeEndElement();
722+
}
723+
} catch (XMLStreamException e) {
724+
StaxUtil.throwXmlAsIOException(e);
725+
}
726+
}
727+
728+
@Override
729+
public void writeRawValue(SerializableString text) throws IOException {
730+
_reportUnsupportedOperation();
731+
}
732+
657733
@Override
658734
public void writeRaw(String text) throws IOException
659735
{

src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlGeneratorTest.java

+115-1
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,119 @@ public void testWriteToFile() throws Exception
105105

106106
assertEquals("<IntWrapper><i>42</i></IntWrapper>", xml);
107107
f.delete();
108-
}
108+
}
109+
110+
public void testRawSimpleValue() throws Exception
111+
{
112+
XmlFactory f = new XmlFactory();
113+
StringWriter out = new StringWriter();
114+
ToXmlGenerator gen = f.createGenerator(out);
115+
// root name is special, need to be fed first:
116+
gen.setNextName(new QName("root"));
117+
gen.writeStartObject();
118+
gen.writeFieldName("elem");
119+
gen.writeRawValue("value");
120+
gen.writeEndObject();
121+
gen.close();
122+
String xml = out.toString();
123+
// one more thing: remove that annoying 'xmlns' decl, if it's there:
124+
xml = removeSjsxpNamespace(xml);
125+
assertEquals("<root><elem>value</elem></root>", xml);
126+
}
127+
128+
public void testRawOffsetValue() throws Exception
129+
{
130+
XmlFactory f = new XmlFactory();
131+
StringWriter out = new StringWriter();
132+
ToXmlGenerator gen = f.createGenerator(out);
133+
// root name is special, need to be fed first:
134+
gen.setNextName(new QName("root"));
135+
gen.writeStartObject();
136+
gen.writeFieldName("elem");
137+
gen.writeRawValue("NotAValue_value_NotAValue", 10, 5);
138+
gen.writeEndObject();
139+
gen.close();
140+
String xml = out.toString();
141+
// one more thing: remove that annoying 'xmlns' decl, if it's there:
142+
xml = removeSjsxpNamespace(xml);
143+
assertEquals("<root><elem>value</elem></root>", xml);
144+
}
145+
146+
public void testRawCharArrayValue() throws Exception
147+
{
148+
XmlFactory f = new XmlFactory();
149+
StringWriter out = new StringWriter();
150+
ToXmlGenerator gen = f.createGenerator(out);
151+
// root name is special, need to be fed first:
152+
gen.setNextName(new QName("root"));
153+
gen.writeStartObject();
154+
gen.writeFieldName("elem");
155+
gen.writeRawValue(new char[] {'!', 'v', 'a', 'l', 'u', 'e', '!'}, 1, 5);
156+
gen.writeEndObject();
157+
gen.close();
158+
String xml = out.toString();
159+
// one more thing: remove that annoying 'xmlns' decl, if it's there:
160+
xml = removeSjsxpNamespace(xml);
161+
assertEquals("<root><elem>value</elem></root>", xml);
162+
}
163+
164+
public void testRawSimpleAttribute() throws Exception
165+
{
166+
XmlFactory f = new XmlFactory();
167+
StringWriter out = new StringWriter();
168+
ToXmlGenerator gen = f.createGenerator(out);
169+
// root name is special, need to be fed first:
170+
gen.setNextName(new QName("root"));
171+
gen.writeStartObject();
172+
// and also need to force attribute
173+
gen.setNextIsAttribute(true);
174+
gen.writeFieldName("attr");
175+
gen.writeRawValue("value");
176+
gen.writeEndObject();
177+
gen.close();
178+
String xml = out.toString();
179+
// one more thing: remove that annoying 'xmlns' decl, if it's there:
180+
xml = removeSjsxpNamespace(xml);
181+
assertEquals("<root attr=\"value\"/>", xml);
182+
}
183+
184+
public void testRawOffsetAttribute() throws Exception
185+
{
186+
XmlFactory f = new XmlFactory();
187+
StringWriter out = new StringWriter();
188+
ToXmlGenerator gen = f.createGenerator(out);
189+
// root name is special, need to be fed first:
190+
gen.setNextName(new QName("root"));
191+
gen.writeStartObject();
192+
// and also need to force attribute
193+
gen.setNextIsAttribute(true);
194+
gen.writeFieldName("attr");
195+
gen.writeRawValue("NotAValue_value_NotAValue", 10, 5);
196+
gen.writeEndObject();
197+
gen.close();
198+
String xml = out.toString();
199+
// one more thing: remove that annoying 'xmlns' decl, if it's there:
200+
xml = removeSjsxpNamespace(xml);
201+
assertEquals("<root attr=\"value\"/>", xml);
202+
}
203+
204+
public void testRawCharArratAttribute() throws Exception
205+
{
206+
XmlFactory f = new XmlFactory();
207+
StringWriter out = new StringWriter();
208+
ToXmlGenerator gen = f.createGenerator(out);
209+
// root name is special, need to be fed first:
210+
gen.setNextName(new QName("root"));
211+
gen.writeStartObject();
212+
// and also need to force attribute
213+
gen.setNextIsAttribute(true);
214+
gen.writeFieldName("attr");
215+
gen.writeRawValue(new char[]{'!', 'v', 'a', 'l', 'u', 'e', '!'}, 1, 5);
216+
gen.writeEndObject();
217+
gen.close();
218+
String xml = out.toString();
219+
// one more thing: remove that annoying 'xmlns' decl, if it's there:
220+
xml = removeSjsxpNamespace(xml);
221+
assertEquals("<root attr=\"value\"/>", xml);
222+
}
109223
}

0 commit comments

Comments
 (0)