5050public class JAXBDecoder implements Decoder {
5151
5252 private final JAXBContextFactory jaxbContextFactory ;
53+ private final boolean namespaceAware ;
5354
5455 public JAXBDecoder (JAXBContextFactory jaxbContextFactory ) {
5556 this .jaxbContextFactory = jaxbContextFactory ;
57+ this .namespaceAware = true ;
58+ }
59+
60+ private JAXBDecoder (Builder builder ) {
61+ this .jaxbContextFactory = builder .jaxbContextFactory ;
62+ this .namespaceAware = builder .namespaceAware ;
5663 }
5764
5865 @ Override
@@ -72,6 +79,7 @@ public Object decode(Response response, Type type) throws IOException {
7279 saxParserFactory .setFeature ("http://xml.org/sax/features/external-parameter-entities" , false );
7380 saxParserFactory .setFeature ("http://apache.org/xml/features/disallow-doctype-decl" , false );
7481 saxParserFactory .setFeature ("http://apache.org/xml/features/nonvalidating/load-external-dtd" , false );
82+ saxParserFactory .setNamespaceAware (namespaceAware );
7583
7684 Source source = new SAXSource (saxParserFactory .newSAXParser ().getXMLReader (), new InputSource (response .body ().asInputStream ()));
7785 Unmarshaller unmarshaller = jaxbContextFactory .createUnmarshaller ((Class ) type );
@@ -88,4 +96,30 @@ public Object decode(Response response, Type type) throws IOException {
8896 }
8997 }
9098 }
99+
100+ public static class Builder {
101+ private boolean namespaceAware = true ;
102+ private JAXBContextFactory jaxbContextFactory ;
103+
104+ /**
105+ * Controls whether the underlying XML parser is namespace aware.
106+ * Default is true.
107+ */
108+ public Builder withNamespaceAware (boolean namespaceAware ) {
109+ this .namespaceAware = namespaceAware ;
110+ return this ;
111+ }
112+
113+ public Builder withJAXBContextFactory (JAXBContextFactory jaxbContextFactory ) {
114+ this .jaxbContextFactory = jaxbContextFactory ;
115+ return this ;
116+ }
117+
118+ public JAXBDecoder build () {
119+ if (jaxbContextFactory == null ) {
120+ throw new IllegalStateException ("JAXBContextFactory must be non-null" );
121+ }
122+ return new JAXBDecoder (this );
123+ }
124+ }
91125}
0 commit comments