More work on Atom

This commit is contained in:
jamesagnew 2014-02-27 07:39:36 -05:00
parent 79a48611df
commit 00411b64f6
3 changed files with 37 additions and 22 deletions

View File

@ -23,6 +23,7 @@ import ca.uhn.fhir.model.api.ICompositeDatatype;
import ca.uhn.fhir.model.api.ICompositeElement;
import ca.uhn.fhir.model.api.IElement;
import ca.uhn.fhir.model.api.IPrimitiveDatatype;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.IResourceBlock;
import ca.uhn.fhir.model.api.ISupportsUndeclaredExtensions;
import ca.uhn.fhir.model.api.ResourceReference;
@ -30,14 +31,12 @@ import ca.uhn.fhir.model.api.UndeclaredExtension;
import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.model.primitive.XhtmlDt;
class ParserState {
class ParserState<T extends IElement> {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ParserState.class);
private FhirContext myContext;
private Object myObject;
private T myObject;
private BaseState myState;
private ParserState(FhirContext theContext) {
@ -60,7 +59,7 @@ class ParserState {
myState.enteringNewElementExtension(theElem, theUrlAttr);
}
public Object getObject() {
public T getObject() {
return myObject;
}
@ -82,12 +81,18 @@ class ParserState {
myState = theState;
}
public static ParserState getPreResourceInstance(FhirContext theContext) throws DataFormatException {
ParserState retVal = new ParserState(theContext);
public static ParserState<IResource> getPreResourceInstance(FhirContext theContext) throws DataFormatException {
ParserState<IResource> retVal = new ParserState<IResource>(theContext);
retVal.push(retVal.new PreResourceState());
return retVal;
}
public static ParserState<Bundle> getPreAtomInstance(FhirContext theContext) throws DataFormatException {
ParserState<Bundle> retVal = new ParserState<Bundle>(theContext);
retVal.push(retVal.new PreAtomState());
return retVal;
}
private class AtomPrimitiveState extends BaseState{
private IPrimitiveDatatype<?> myPrimitive;
@ -156,6 +161,10 @@ class ParserState {
public void enteringNewElement(StartElement theElement, String theLocalPart) throws DataFormatException {
if (theLocalPart.equals("title")) {
push(new AtomPrimitiveState(myInstance.getTitle()));
}else if ("id".equals(theLocalPart)) {
push(new AtomPrimitiveState(myInstance.getId()));
}else if ("link".equals(theLocalPart)) {
}
}
@ -204,9 +213,10 @@ class ParserState {
return null;
}
@SuppressWarnings("unchecked")
@Override
public void wereBack() {
myObject = myInstance;
myObject = (T) myInstance;
}
@Override
@ -359,11 +369,12 @@ class ParserState {
ourLog.debug("Ignoring attribute value: {}", theValue);
}
@SuppressWarnings("unchecked")
@Override
public void endingElement(EndElement theElem) {
pop();
if (myState == null) {
myObject = myInstance;
myObject = (T) myInstance;
}
}
@ -559,9 +570,10 @@ class ParserState {
// ignore
}
@SuppressWarnings("unchecked")
@Override
public void wereBack() {
myObject = myInstance;
myObject = (T) myInstance;
}
}

View File

@ -7,6 +7,7 @@ import java.io.StringReader;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.stream.FactoryConfigurationError;
@ -55,6 +56,8 @@ public class XmlParser {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(XmlParser.class);
private static final String XHTML_NS = "http://www.w3.org/1999/xhtml";
// private static final Set<String> RESOURCE_NAMESPACES;
private FhirContext myContext;
private XMLInputFactory myXmlInputFactory;
private XMLOutputFactory myXmlOutputFactory;
@ -128,8 +131,7 @@ public class XmlParser {
return stringWriter.toString();
}
private void encodeChildElementToStreamWriter(XMLStreamWriter theEventWriter, IElement nextValue, String childName, BaseRuntimeElementDefinition<?> childDef, String theExtensionUrl)
throws XMLStreamException, DataFormatException {
private void encodeChildElementToStreamWriter(XMLStreamWriter theEventWriter, IElement nextValue, String childName, BaseRuntimeElementDefinition<?> childDef, String theExtensionUrl) throws XMLStreamException, DataFormatException {
switch (childDef.getChildType()) {
case PRIMITIVE_DATATYPE: {
theEventWriter.writeStartElement(childName);
@ -172,8 +174,7 @@ public class XmlParser {
}
}
private void encodeCompositeElementChildrenToStreamWriter(IElement theElement, XMLStreamWriter theEventWriter, List<? extends BaseRuntimeChildDefinition> children) throws XMLStreamException,
DataFormatException {
private void encodeCompositeElementChildrenToStreamWriter(IElement theElement, XMLStreamWriter theEventWriter, List<? extends BaseRuntimeChildDefinition> children) throws XMLStreamException, DataFormatException {
for (BaseRuntimeChildDefinition nextChild : children) {
List<? extends IElement> values = nextChild.getAccessor().getValues(theElement);
if (values == null || values.isEmpty()) {
@ -204,8 +205,7 @@ public class XmlParser {
}
}
private void encodeCompositeElementToStreamWriter(IElement theElement, XMLStreamWriter theEventWriter, BaseRuntimeElementCompositeDefinition<?> resDef) throws XMLStreamException,
DataFormatException {
private void encodeCompositeElementToStreamWriter(IElement theElement, XMLStreamWriter theEventWriter, BaseRuntimeElementCompositeDefinition<?> resDef) throws XMLStreamException, DataFormatException {
encodeExtensionsIfPresent(theEventWriter, theElement);
encodeCompositeElementChildrenToStreamWriter(theElement, theEventWriter, resDef.getExtensions());
encodeCompositeElementChildrenToStreamWriter(theElement, theEventWriter, resDef.getChildren());
@ -371,15 +371,17 @@ public class XmlParser {
}
private Bundle parseBundle(XMLEventReader theStreamReader) {
// TODO Auto-generated method stub
return null;
ParserState<Bundle> parserState = ParserState.getPreAtomInstance(myContext);
return doXmlLoop(theStreamReader, parserState);
}
public IResource parseResource(XMLEventReader streamReader) {
public IResource parseResource(XMLEventReader theStreamReader) {
ParserState<IResource> parserState = ParserState.getPreResourceInstance(myContext);
return doXmlLoop(theStreamReader, parserState);
}
private <T extends IElement> T doXmlLoop(XMLEventReader streamReader, ParserState<T> parserState) {
try {
ParserState parserState = ParserState.getPreResourceInstance(myContext);
while (streamReader.hasNext()) {
XMLEvent nextEvent = streamReader.nextEvent();
if (nextEvent.isStartElement()) {
@ -426,7 +428,7 @@ public class XmlParser {
parserState.endingElement(elem);
if (parserState.isComplete()) {
return (IResource) parserState.getObject();
return parserState.getObject();
}
} else {
parserState.otherEvent(nextEvent);

View File

@ -82,6 +82,7 @@ public class XmlParserTest {
XmlParser p = new FhirContext(ValueSet.class).newXmlParser();
Bundle bundle = p.parseBundle(msg);
assertEquals("FHIR Core Valuesets", bundle.getTitle().getValue());
}