More work on Atom
This commit is contained in:
parent
79a48611df
commit
00411b64f6
|
@ -23,6 +23,7 @@ import ca.uhn.fhir.model.api.ICompositeDatatype;
|
||||||
import ca.uhn.fhir.model.api.ICompositeElement;
|
import ca.uhn.fhir.model.api.ICompositeElement;
|
||||||
import ca.uhn.fhir.model.api.IElement;
|
import ca.uhn.fhir.model.api.IElement;
|
||||||
import ca.uhn.fhir.model.api.IPrimitiveDatatype;
|
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.IResourceBlock;
|
||||||
import ca.uhn.fhir.model.api.ISupportsUndeclaredExtensions;
|
import ca.uhn.fhir.model.api.ISupportsUndeclaredExtensions;
|
||||||
import ca.uhn.fhir.model.api.ResourceReference;
|
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.StringDt;
|
||||||
import ca.uhn.fhir.model.primitive.XhtmlDt;
|
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 static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ParserState.class);
|
||||||
|
|
||||||
private FhirContext myContext;
|
private FhirContext myContext;
|
||||||
|
private T myObject;
|
||||||
private Object myObject;
|
|
||||||
|
|
||||||
private BaseState myState;
|
private BaseState myState;
|
||||||
|
|
||||||
private ParserState(FhirContext theContext) {
|
private ParserState(FhirContext theContext) {
|
||||||
|
@ -60,7 +59,7 @@ class ParserState {
|
||||||
myState.enteringNewElementExtension(theElem, theUrlAttr);
|
myState.enteringNewElementExtension(theElem, theUrlAttr);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object getObject() {
|
public T getObject() {
|
||||||
return myObject;
|
return myObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,12 +81,18 @@ class ParserState {
|
||||||
myState = theState;
|
myState = theState;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ParserState getPreResourceInstance(FhirContext theContext) throws DataFormatException {
|
public static ParserState<IResource> getPreResourceInstance(FhirContext theContext) throws DataFormatException {
|
||||||
ParserState retVal = new ParserState(theContext);
|
ParserState<IResource> retVal = new ParserState<IResource>(theContext);
|
||||||
retVal.push(retVal.new PreResourceState());
|
retVal.push(retVal.new PreResourceState());
|
||||||
return retVal;
|
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 class AtomPrimitiveState extends BaseState{
|
||||||
|
|
||||||
private IPrimitiveDatatype<?> myPrimitive;
|
private IPrimitiveDatatype<?> myPrimitive;
|
||||||
|
@ -156,6 +161,10 @@ class ParserState {
|
||||||
public void enteringNewElement(StartElement theElement, String theLocalPart) throws DataFormatException {
|
public void enteringNewElement(StartElement theElement, String theLocalPart) throws DataFormatException {
|
||||||
if (theLocalPart.equals("title")) {
|
if (theLocalPart.equals("title")) {
|
||||||
push(new AtomPrimitiveState(myInstance.getTitle()));
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public void wereBack() {
|
public void wereBack() {
|
||||||
myObject = myInstance;
|
myObject = (T) myInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -359,11 +369,12 @@ class ParserState {
|
||||||
ourLog.debug("Ignoring attribute value: {}", theValue);
|
ourLog.debug("Ignoring attribute value: {}", theValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public void endingElement(EndElement theElem) {
|
public void endingElement(EndElement theElem) {
|
||||||
pop();
|
pop();
|
||||||
if (myState == null) {
|
if (myState == null) {
|
||||||
myObject = myInstance;
|
myObject = (T) myInstance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -559,9 +570,10 @@ class ParserState {
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public void wereBack() {
|
public void wereBack() {
|
||||||
myObject = myInstance;
|
myObject = (T) myInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import java.io.StringReader;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.xml.namespace.QName;
|
import javax.xml.namespace.QName;
|
||||||
import javax.xml.stream.FactoryConfigurationError;
|
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 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 String XHTML_NS = "http://www.w3.org/1999/xhtml";
|
||||||
|
|
||||||
|
// private static final Set<String> RESOURCE_NAMESPACES;
|
||||||
|
|
||||||
private FhirContext myContext;
|
private FhirContext myContext;
|
||||||
private XMLInputFactory myXmlInputFactory;
|
private XMLInputFactory myXmlInputFactory;
|
||||||
private XMLOutputFactory myXmlOutputFactory;
|
private XMLOutputFactory myXmlOutputFactory;
|
||||||
|
@ -128,8 +131,7 @@ public class XmlParser {
|
||||||
return stringWriter.toString();
|
return stringWriter.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void encodeChildElementToStreamWriter(XMLStreamWriter theEventWriter, IElement nextValue, String childName, BaseRuntimeElementDefinition<?> childDef, String theExtensionUrl)
|
private void encodeChildElementToStreamWriter(XMLStreamWriter theEventWriter, IElement nextValue, String childName, BaseRuntimeElementDefinition<?> childDef, String theExtensionUrl) throws XMLStreamException, DataFormatException {
|
||||||
throws XMLStreamException, DataFormatException {
|
|
||||||
switch (childDef.getChildType()) {
|
switch (childDef.getChildType()) {
|
||||||
case PRIMITIVE_DATATYPE: {
|
case PRIMITIVE_DATATYPE: {
|
||||||
theEventWriter.writeStartElement(childName);
|
theEventWriter.writeStartElement(childName);
|
||||||
|
@ -172,8 +174,7 @@ public class XmlParser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void encodeCompositeElementChildrenToStreamWriter(IElement theElement, XMLStreamWriter theEventWriter, List<? extends BaseRuntimeChildDefinition> children) throws XMLStreamException,
|
private void encodeCompositeElementChildrenToStreamWriter(IElement theElement, XMLStreamWriter theEventWriter, List<? extends BaseRuntimeChildDefinition> children) throws XMLStreamException, DataFormatException {
|
||||||
DataFormatException {
|
|
||||||
for (BaseRuntimeChildDefinition nextChild : children) {
|
for (BaseRuntimeChildDefinition nextChild : children) {
|
||||||
List<? extends IElement> values = nextChild.getAccessor().getValues(theElement);
|
List<? extends IElement> values = nextChild.getAccessor().getValues(theElement);
|
||||||
if (values == null || values.isEmpty()) {
|
if (values == null || values.isEmpty()) {
|
||||||
|
@ -204,8 +205,7 @@ public class XmlParser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void encodeCompositeElementToStreamWriter(IElement theElement, XMLStreamWriter theEventWriter, BaseRuntimeElementCompositeDefinition<?> resDef) throws XMLStreamException,
|
private void encodeCompositeElementToStreamWriter(IElement theElement, XMLStreamWriter theEventWriter, BaseRuntimeElementCompositeDefinition<?> resDef) throws XMLStreamException, DataFormatException {
|
||||||
DataFormatException {
|
|
||||||
encodeExtensionsIfPresent(theEventWriter, theElement);
|
encodeExtensionsIfPresent(theEventWriter, theElement);
|
||||||
encodeCompositeElementChildrenToStreamWriter(theElement, theEventWriter, resDef.getExtensions());
|
encodeCompositeElementChildrenToStreamWriter(theElement, theEventWriter, resDef.getExtensions());
|
||||||
encodeCompositeElementChildrenToStreamWriter(theElement, theEventWriter, resDef.getChildren());
|
encodeCompositeElementChildrenToStreamWriter(theElement, theEventWriter, resDef.getChildren());
|
||||||
|
@ -371,15 +371,17 @@ public class XmlParser {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Bundle parseBundle(XMLEventReader theStreamReader) {
|
private Bundle parseBundle(XMLEventReader theStreamReader) {
|
||||||
// TODO Auto-generated method stub
|
ParserState<Bundle> parserState = ParserState.getPreAtomInstance(myContext);
|
||||||
return null;
|
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 {
|
try {
|
||||||
ParserState parserState = ParserState.getPreResourceInstance(myContext);
|
|
||||||
|
|
||||||
while (streamReader.hasNext()) {
|
while (streamReader.hasNext()) {
|
||||||
XMLEvent nextEvent = streamReader.nextEvent();
|
XMLEvent nextEvent = streamReader.nextEvent();
|
||||||
if (nextEvent.isStartElement()) {
|
if (nextEvent.isStartElement()) {
|
||||||
|
@ -426,7 +428,7 @@ public class XmlParser {
|
||||||
|
|
||||||
parserState.endingElement(elem);
|
parserState.endingElement(elem);
|
||||||
if (parserState.isComplete()) {
|
if (parserState.isComplete()) {
|
||||||
return (IResource) parserState.getObject();
|
return parserState.getObject();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
parserState.otherEvent(nextEvent);
|
parserState.otherEvent(nextEvent);
|
||||||
|
|
|
@ -82,6 +82,7 @@ public class XmlParserTest {
|
||||||
XmlParser p = new FhirContext(ValueSet.class).newXmlParser();
|
XmlParser p = new FhirContext(ValueSet.class).newXmlParser();
|
||||||
Bundle bundle = p.parseBundle(msg);
|
Bundle bundle = p.parseBundle(msg);
|
||||||
|
|
||||||
|
assertEquals("FHIR Core Valuesets", bundle.getTitle().getValue());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue