Skip to content

Commit

Permalink
Remove 'final' from method parameters in pull request 2. Amend name o…
Browse files Browse the repository at this point in the history
…f attribute added to MOTD test element due to naming conflict in JAXB (it is now 'number').
  • Loading branch information
Andrew Taylor committed Jul 5, 2013
1 parent 2c64315 commit fdb6401
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion core/src/test/xsd/TestConfiguration-1.0.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<xsd:element name="Language" type="xsd:language" maxOccurs="1" minOccurs="0"></xsd:element>
<xsd:element name="References" type="st:UUIDType" maxOccurs="unbounded" minOccurs="0"></xsd:element>
</xsd:sequence>
<xsd:attribute type="xsd:token" name="id" />
<xsd:attribute type="xsd:int" name="number" />
</xsd:complexType>
</xsd:element>
<xsd:element name="FeatureFlag" maxOccurs="unbounded" minOccurs="0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,10 @@ protected Object resolveObject(Node node) {
Field[] fieldArr = parentObj.getClass().getDeclaredFields();
for (Field field : fieldArr) {
XmlElement xmlElement = field.getAnnotation(XmlElement.class);
if (node.getLocalName().equals(xmlElement.name())) {
value = extractFieldValue(parentObj, field);
if (xmlElement != null) {
if (node.getLocalName().equals(xmlElement.name())) {
value = extractFieldValue(parentObj, field);
}
}
}
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.brekka.stillingar.xmlbeans;

import static java.lang.String.format;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -70,20 +68,26 @@ public class XmlBeansConfigurationSourceLoader implements ConfigurationSourceLoa
private final DefaultNamespaceContext xpathNamespaces;

private boolean validate = true;

/**
* Options passed to the XmlObject.parse(...) operation. Defaults are to strip comments
* (not used for configuration) but can be overridden via the corresponding setter.
*/
private XmlOptions loadXmlOptions;

public XmlBeansConfigurationSourceLoader() {
this(new ConversionManager(prepareConverters()));
}

public XmlBeansConfigurationSourceLoader(final ConversionManager conversionManager) {
public XmlBeansConfigurationSourceLoader(ConversionManager conversionManager) {
this(conversionManager, new DefaultNamespaceContext());
}

public XmlBeansConfigurationSourceLoader(final DefaultNamespaceContext xpathNamespaces) {
public XmlBeansConfigurationSourceLoader(DefaultNamespaceContext xpathNamespaces) {
this(new ConversionManager(prepareConverters()), xpathNamespaces);
}

public XmlBeansConfigurationSourceLoader(final ConversionManager conversionManager, final DefaultNamespaceContext xpathNamespaces) {
public XmlBeansConfigurationSourceLoader(ConversionManager conversionManager, DefaultNamespaceContext xpathNamespaces) {
if (conversionManager == null) {
throw new IllegalArgumentException("null passed for conversion manager");
}
Expand All @@ -92,6 +96,10 @@ public XmlBeansConfigurationSourceLoader(final ConversionManager conversionManag
throw new IllegalArgumentException("null passed for xpathNamespaces");
}
this.xpathNamespaces = xpathNamespaces;

XmlOptions loadXmlOptions = new XmlOptions();
loadXmlOptions.setLoadStripComments();
this.loadXmlOptions = loadXmlOptions;
}

/*
Expand All @@ -100,35 +108,34 @@ public XmlBeansConfigurationSourceLoader(final ConversionManager conversionManag
* @see org.brekka.stillingar.core.ConfigurationSourceLoader#parse(java.io.InputStream, java.nio.charset.Charset)
*/
@Override
public ConfigurationSource parse(final InputStream sourceStream, final Charset encoding) throws IOException {
public ConfigurationSource parse(InputStream sourceStream, Charset encoding) throws IOException {
if (sourceStream == null) {
throw new IllegalArgumentException("A source stream is required");
}
try {
XmlOptions opts = new XmlOptions();
opts.setLoadStripComments();
XmlObject xmlBean = XmlObject.Factory.parse(sourceStream, opts);

XmlObject xmlBean = XmlObject.Factory.parse(sourceStream, loadXmlOptions);
if (this.validate) {
validate(xmlBean);
}
return new XmlBeansConfigurationSource(xmlBean, this.xpathNamespaces, conversionManager);
} catch (XmlException e) {
throw new ConfigurationException(format(
throw new ConfigurationException(String.format(
"This does not appear to be an XML document"), e);
}
}

protected void validate(final XmlObject bean) {
protected void validate(XmlObject bean) {
List<XmlError> errors = new ArrayList<XmlError>();
XmlOptions validateOptions = new XmlOptions();
validateOptions.setErrorListener(errors);
if (!bean.validate(validateOptions)) {
throw new ConfigurationException(format(
throw new ConfigurationException(String.format(
"Configuration XML does not validate. Errors: %s", errors));
}
}

public void setValidate(final boolean validate) {
public void setValidate(boolean validate) {
this.validate = validate;
}

Expand All @@ -151,4 +158,11 @@ public static List<TypeConverter<?>> prepareConverters() {
.toList();

}

/**
* @param loadXmlOptions the loadXmlOptions to set
*/
public void setLoadXmlOptions(XmlOptions loadXmlOptions) {
this.loadXmlOptions = loadXmlOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ public void testRetrieveXPathAttrSelector() {

@Test
public void testRetrieveXPathAttrSelectorElem() {
String message = configurationSource.retrieve("//c:MOTD[@id='1']//c:Message", String.class);
String message = configurationSource.retrieve("//c:MOTD[@number=1]//c:Message", String.class);
//call twice sometime it behaves differently on the second call
message = configurationSource.retrieve("//c:MOTD[@id='1']//c:Message", String.class);
message = configurationSource.retrieve("//c:MOTD[@number=1]//c:Message", String.class);
assertEquals("Test message", message);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Configuration xmlns="http://brekka.org/xml/stillingar/test/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://brekka.org/xml/stillingar/test/v1 http://brekka.org/schema/stillingar/test/TestConfiguration-1.0.xsd">
<!-- Test Comment -->
<MOTD id="1">
<MOTD number="1">
<ID>64829ee9-d265-47bb-8fb4-4ab4ada0cdfc</ID>
<Message>Test message</Message>
<Expires>2012-12-31T12:00:00</Expires>
Expand Down

0 comments on commit fdb6401

Please sign in to comment.