1366 lines
40 KiB
Java
Raw Normal View History

2014-02-18 08:26:49 -05:00
package ca.uhn.fhir.parser;
/*
* #%L
* HAPI FHIR Library
* %%
* Copyright (C) 2014 University Health Network
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
2014-04-14 08:40:30 -04:00
import static org.apache.commons.lang3.StringUtils.*;
2014-02-22 15:33:02 -05:00
import java.util.ArrayList;
import java.util.HashMap;
2014-02-22 15:33:02 -05:00
import java.util.List;
import java.util.Map;
2014-02-21 21:06:11 -05:00
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
2014-02-27 11:57:50 -05:00
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
2014-02-27 11:57:50 -05:00
2014-02-18 08:26:49 -05:00
import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
2014-02-19 11:59:12 -05:00
import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition;
2014-02-18 08:26:49 -05:00
import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
import ca.uhn.fhir.context.FhirContext;
2014-02-23 18:13:59 -05:00
import ca.uhn.fhir.context.RuntimeChildDeclaredExtensionDefinition;
2014-03-17 08:57:57 -04:00
import ca.uhn.fhir.context.RuntimeElemContainedResources;
2014-02-19 11:59:12 -05:00
import ca.uhn.fhir.context.RuntimePrimitiveDatatypeDefinition;
2014-02-22 15:33:02 -05:00
import ca.uhn.fhir.context.RuntimePrimitiveDatatypeNarrativeDefinition;
2014-02-20 12:13:05 -05:00
import ca.uhn.fhir.context.RuntimeResourceBlockDefinition;
2014-02-18 08:26:49 -05:00
import ca.uhn.fhir.context.RuntimeResourceDefinition;
2014-02-19 17:33:46 -05:00
import ca.uhn.fhir.context.RuntimeResourceReferenceDefinition;
2014-02-27 16:51:43 -05:00
import ca.uhn.fhir.model.api.BaseBundle;
2014-02-26 17:13:49 -05:00
import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.Tag;
2014-02-27 16:51:43 -05:00
import ca.uhn.fhir.model.api.BundleEntry;
2014-03-27 08:51:02 -04:00
import ca.uhn.fhir.model.api.ExtensionDt;
2014-02-19 11:59:12 -05:00
import ca.uhn.fhir.model.api.ICompositeDatatype;
import ca.uhn.fhir.model.api.ICompositeElement;
2014-02-22 15:33:02 -05:00
import ca.uhn.fhir.model.api.IElement;
2014-02-19 11:59:12 -05:00
import ca.uhn.fhir.model.api.IPrimitiveDatatype;
2014-02-27 07:39:36 -05:00
import ca.uhn.fhir.model.api.IResource;
2014-02-20 12:13:05 -05:00
import ca.uhn.fhir.model.api.IResourceBlock;
2014-02-26 17:13:49 -05:00
import ca.uhn.fhir.model.api.ISupportsUndeclaredExtensions;
2014-04-14 08:40:30 -04:00
import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
import ca.uhn.fhir.model.api.TagList;
2014-03-17 08:57:57 -04:00
import ca.uhn.fhir.model.dstu.composite.ContainedDt;
2014-03-10 18:17:30 -04:00
import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt;
import ca.uhn.fhir.model.primitive.IdDt;
2014-02-24 11:46:08 -05:00
import ca.uhn.fhir.model.primitive.XhtmlDt;
2014-04-14 08:40:30 -04:00
import ca.uhn.fhir.rest.server.Constants;
2014-02-18 08:26:49 -05:00
class ParserState<T> {
2014-02-18 08:26:49 -05:00
2014-02-21 21:06:11 -05:00
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ParserState.class);
private FhirContext myContext;
private boolean myJsonMode;
2014-02-27 07:39:36 -05:00
private T myObject;
2014-02-21 21:06:11 -05:00
private BaseState myState;
2014-02-19 17:33:46 -05:00
2014-03-22 12:33:07 -04:00
private ParserState(FhirContext theContext, boolean theJsonMode) {
2014-02-21 21:06:11 -05:00
myContext = theContext;
2014-03-22 12:33:07 -04:00
myJsonMode = theJsonMode;
2014-02-21 21:06:11 -05:00
}
2014-02-19 17:33:46 -05:00
2014-02-27 16:51:43 -05:00
public void attributeValue(String theName, String theValue) throws DataFormatException {
myState.attributeValue(theName, theValue);
2014-02-19 17:33:46 -05:00
}
public void endingElement() throws DataFormatException {
myState.endingElement();
2014-02-21 21:06:11 -05:00
}
2014-02-19 11:59:12 -05:00
2014-02-27 11:57:50 -05:00
public void enteringNewElement(String theNamespaceURI, String theName) throws DataFormatException {
myState.enteringNewElement(theNamespaceURI, theName);
2014-02-21 21:06:11 -05:00
}
2014-02-18 08:26:49 -05:00
2014-03-10 18:17:30 -04:00
public void enteringNewElementExtension(StartElement theElem, String theUrlAttr, boolean theIsModifier) {
myState.enteringNewElementExtension(theElem, theUrlAttr, theIsModifier);
2014-02-22 15:33:02 -05:00
}
2014-03-22 12:33:07 -04:00
@SuppressWarnings("unchecked")
2014-02-27 07:39:36 -05:00
public T getObject() {
2014-03-22 12:33:07 -04:00
return (T) myState.getCurrentElement();
2014-02-19 11:59:12 -05:00
}
2014-02-21 21:06:11 -05:00
public boolean isComplete() {
return myObject != null;
2014-02-19 11:59:12 -05:00
}
public boolean isPreResource() {
return myState.isPreResource();
}
2014-02-27 16:51:43 -05:00
public void string(String theData) {
myState.string(theData);
}
public boolean verifyNamespace(String theExpect, String theActual) {
if (myJsonMode) {
return true;
}
2014-02-27 16:51:43 -05:00
return StringUtils.equals(theExpect, theActual);
}
/**
* Invoked after any new XML event is individually processed, containing a
* copy of the XML event. This is basically intended for embedded XHTML
* content
2014-02-27 16:51:43 -05:00
*/
public void xmlEvent(XMLEvent theNextEvent) {
myState.xmlEvent(theNextEvent);
}
2014-02-21 21:06:11 -05:00
private void pop() {
myState = myState.myStack;
2014-02-26 17:13:49 -05:00
myState.wereBack();
2014-02-19 11:59:12 -05:00
}
private void push(BaseState theState) {
theState.setStack(myState);
myState = theState;
}
public static ParserState<Bundle> getPreAtomInstance(FhirContext theContext, Class<? extends IResource> theResourceType, boolean theJsonMode) throws DataFormatException {
2014-03-22 12:33:07 -04:00
ParserState<Bundle> retVal = new ParserState<Bundle>(theContext, theJsonMode);
retVal.push(retVal.new PreAtomState(theResourceType));
2014-02-27 07:39:36 -05:00
return retVal;
}
2014-03-07 16:23:49 -05:00
/**
* @param theResourceType
* May be null
*/
2014-03-22 12:33:07 -04:00
public static <T extends IResource> ParserState<T> getPreResourceInstance(Class<T> theResourceType, FhirContext theContext, boolean theJsonMode) throws DataFormatException {
ParserState<T> retVal = new ParserState<T>(theContext, theJsonMode);
2014-03-07 16:23:49 -05:00
retVal.push(retVal.new PreResourceState(theResourceType));
2014-02-27 16:51:43 -05:00
return retVal;
}
2014-02-26 17:13:49 -05:00
public static ParserState<TagList> getPreTagListInstance(FhirContext theContext, boolean theJsonMode) {
ParserState<TagList> retVal = new ParserState<TagList>(theContext, theJsonMode);
retVal.push(retVal.new PreTagListState());
return retVal;
}
2014-02-27 16:51:43 -05:00
public class AtomAuthorState extends BaseState {
2014-02-26 17:13:49 -05:00
2014-02-27 16:51:43 -05:00
private BaseBundle myInstance;
public AtomAuthorState(BaseBundle theEntry) {
super(null);
2014-02-27 16:51:43 -05:00
myInstance = theEntry;
2014-02-18 08:26:49 -05:00
}
2014-02-19 11:59:12 -05:00
2014-02-26 17:13:49 -05:00
@Override
public void endingElement() throws DataFormatException {
2014-02-26 17:13:49 -05:00
pop();
}
2014-02-19 11:59:12 -05:00
2014-02-26 17:13:49 -05:00
@Override
2014-02-27 11:57:50 -05:00
public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException {
2014-02-27 16:51:43 -05:00
if ("name".equals(theLocalPart)) {
push(new AtomPrimitiveState(myInstance.getAuthorName()));
} else if ("uri".equals(theLocalPart)) {
push(new AtomPrimitiveState(myInstance.getAuthorUri()));
} else {
throw new DataFormatException("Unexpected element: " + theLocalPart);
}
}
}
2014-03-06 17:12:05 -05:00
public class AtomCategoryState extends BaseState {
private static final int STATE_LABEL = 2;
private static final int STATE_NONE = 0;
private static final int STATE_SCHEME = 3;
private static final int STATE_TERM = 1;
private int myCatState = STATE_NONE;
private Tag myInstance;
2014-03-06 17:12:05 -05:00
public AtomCategoryState(Tag theEntry) {
super(null);
2014-03-06 17:12:05 -05:00
myInstance = theEntry;
}
@Override
public void attributeValue(String theName, String theValue) throws DataFormatException {
if ("term".equals(theName)) {
myInstance.setTerm(theValue);
} else if ("label".equals(theName)) {
myInstance.setLabel(theValue);
} else if ("scheme".equals(theName)) {
myInstance.setScheme(theValue);
} else if ("value".equals(theName)) {
/*
* This handles XML parsing, which is odd for this quasi-resource type,
* since the tag has three values instead of one like everything else.
*/
switch (myCatState) {
case STATE_LABEL:
myInstance.setLabel(theValue);
break;
case STATE_TERM:
myInstance.setTerm(theValue);
break;
case STATE_SCHEME:
myInstance.setScheme(theValue);
break;
default:
super.string(theValue);
break;
}
2014-03-06 17:12:05 -05:00
}
}
@Override
public void endingElement() throws DataFormatException {
if (myCatState != STATE_NONE) {
myCatState = STATE_NONE;
} else {
pop();
}
}
@Override
public void enteringNewElement(String theNamespaceURI, String theName) throws DataFormatException {
if (myCatState != STATE_NONE) {
throw new DataFormatException("Unexpected element in entry: " + theName);
}
if ("term".equals(theName)) {
myCatState = STATE_TERM;
} else if ("label".equals(theName)) {
myCatState = STATE_LABEL;
} else if ("scheme".equals(theName)) {
myCatState = STATE_SCHEME;
}
}
2014-03-06 17:12:05 -05:00
}
2014-02-27 16:51:43 -05:00
public class AtomEntryState extends BaseState {
private BundleEntry myEntry;
private Class<? extends IResource> myResourceType;
2014-02-27 16:51:43 -05:00
public AtomEntryState(Bundle theInstance, Class<? extends IResource> theResourceType) {
super(null);
2014-02-27 16:51:43 -05:00
myEntry = new BundleEntry();
myResourceType = theResourceType;
2014-02-27 16:51:43 -05:00
theInstance.getEntries().add(myEntry);
2014-02-26 17:13:49 -05:00
}
@Override
public void endingElement() throws DataFormatException {
2014-04-14 08:40:30 -04:00
populateResourceMetadata();
2014-02-27 16:51:43 -05:00
pop();
2014-02-26 17:13:49 -05:00
}
@Override
public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException {
if ("title".equals(theLocalPart)) {
push(new AtomPrimitiveState(myEntry.getTitle()));
} else if ("id".equals(theLocalPart)) {
push(new AtomPrimitiveState(myEntry.getId()));
} else if ("link".equals(theLocalPart)) {
push(new AtomLinkState(myEntry));
} else if ("updated".equals(theLocalPart)) {
push(new AtomPrimitiveState(myEntry.getUpdated()));
} else if ("published".equals(theLocalPart)) {
push(new AtomPrimitiveState(myEntry.getPublished()));
} else if ("author".equals(theLocalPart)) {
push(new AtomAuthorState(myEntry));
} else if ("content".equals(theLocalPart)) {
push(new PreResourceState(myEntry, myResourceType));
} else if ("summary".equals(theLocalPart)) {
push(new XhtmlState(getPreResourceState(), myEntry.getSummary(), false));
} else if ("category".equals(theLocalPart)) {
push(new AtomCategoryState(myEntry.addCategory()));
} else {
throw new DataFormatException("Unexpected element in entry: " + theLocalPart);
}
// TODO: handle category
}
2014-04-14 08:40:30 -04:00
private void populateResourceMetadata() {
if (myEntry.getResource() == null) {
return;
}
Map<ResourceMetadataKeyEnum, Object> metadata = myEntry.getResource().getResourceMetadata();
if (myEntry.getPublished().isEmpty() == false) {
metadata.put(ResourceMetadataKeyEnum.PUBLISHED, myEntry.getPublished());
}
if (myEntry.getUpdated().isEmpty() == false) {
metadata.put(ResourceMetadataKeyEnum.UPDATED, myEntry.getUpdated());
}
if (myEntry.getCategories().isEmpty() == false) {
TagList tagList = new TagList();
for (Tag next : myEntry.getCategories()) {
tagList.add(next);
}
metadata.put(ResourceMetadataKeyEnum.TAG_LIST, tagList);
}
2014-04-14 08:40:30 -04:00
if (!myEntry.getLinkSelf().isEmpty()) {
String linkSelfValue = myEntry.getLinkSelf().getValue();
/*
* Find resource ID if it is there
*/
String resNameLc = myContext.getResourceDefinition(myEntry.getResource()).getName().toLowerCase();
String subStrId = "/" + resNameLc + "/";
int idIdx = linkSelfValue.toLowerCase().lastIndexOf(subStrId);
if (idIdx != -1) {
int endIndex = linkSelfValue.indexOf('/', idIdx + subStrId.length());
String id;
if (endIndex == -1) {
id = linkSelfValue.substring(idIdx + subStrId.length());
} else {
id = linkSelfValue.substring(idIdx + subStrId.length(), endIndex);
}
myEntry.getResource().setId(new IdDt(id));
}
/*
* Find resource version ID if it is there
*/
String subStrVid = "/" + Constants.PARAM_HISTORY + "/";
int startIndex = linkSelfValue.indexOf(subStrVid);
2014-04-14 08:40:30 -04:00
if (startIndex > 0) {
startIndex = startIndex + subStrVid.length();
2014-04-14 08:40:30 -04:00
int endIndex = linkSelfValue.indexOf('?', startIndex);
if (endIndex == -1) {
endIndex = linkSelfValue.length();
}
String versionId = linkSelfValue.substring(startIndex, endIndex);
if (isNotBlank(versionId)) {
int idx = versionId.indexOf('/');
if (idx != -1) {
// Just in case
ourLog.warn("Bundle entry link-self contains path information beyond version (this will be ignored): {}", versionId);
versionId = versionId.substring(0, idx);
}
metadata.put(ResourceMetadataKeyEnum.VERSION_ID, new IdDt(versionId));
2014-04-14 08:40:30 -04:00
}
}
}
}
2014-02-18 08:26:49 -05:00
}
2014-02-27 11:57:50 -05:00
private class AtomLinkState extends BaseState {
2014-02-27 16:51:43 -05:00
private BundleEntry myEntry;
2014-02-27 11:57:50 -05:00
private String myHref;
private Bundle myInstance;
2014-02-27 16:51:43 -05:00
private String myRel;
2014-02-27 11:57:50 -05:00
public AtomLinkState(Bundle theInstance) {
super(null);
2014-02-27 11:57:50 -05:00
myInstance = theInstance;
}
2014-02-27 16:51:43 -05:00
public AtomLinkState(BundleEntry theEntry) {
super(null);
2014-02-27 16:51:43 -05:00
myEntry = theEntry;
}
@Override
2014-02-27 16:51:43 -05:00
public void attributeValue(String theName, String theValue) throws DataFormatException {
if ("rel".equals(theName)) {
2014-02-27 11:57:50 -05:00
myRel = theValue;
2014-02-27 16:51:43 -05:00
} else if ("href".equals(theName)) {
2014-02-27 11:57:50 -05:00
myHref = theValue;
}
}
@Override
public void endingElement() throws DataFormatException {
2014-02-27 16:51:43 -05:00
if (myInstance != null) {
if ("self".equals(myRel)) {
myInstance.getLinkSelf().setValueAsString(myHref);
} else if ("first".equals(myRel)) {
myInstance.getLinkFirst().setValueAsString(myHref);
} else if ("previous".equals(myRel)) {
myInstance.getLinkPrevious().setValueAsString(myHref);
} else if ("next".equals(myRel)) {
myInstance.getLinkNext().setValueAsString(myHref);
} else if ("last".equals(myRel)) {
myInstance.getLinkLast().setValueAsString(myHref);
} else if ("fhir-base".equals(myRel)) {
myInstance.getLinkBase().setValueAsString(myHref);
}
} else {
myEntry.getLinkSelf().setValueAsString(myHref);
2014-02-27 11:57:50 -05:00
}
pop();
}
@Override
2014-02-27 11:57:50 -05:00
public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException {
2014-03-19 17:38:31 -04:00
throw new DataFormatException("Found unexpected element content '" + theLocalPart + "' within <link>");
}
}
2014-02-27 16:51:43 -05:00
private class AtomPrimitiveState extends BaseState {
private String myData;
private IPrimitiveDatatype<?> myPrimitive;
public AtomPrimitiveState(IPrimitiveDatatype<?> thePrimitive) {
super(null);
Validate.notNull(thePrimitive, "thePrimitive");
2014-02-27 16:51:43 -05:00
myPrimitive = thePrimitive;
}
@Override
public void endingElement() throws DataFormatException {
2014-02-27 16:51:43 -05:00
myPrimitive.setValueAsString(myData);
pop();
}
@Override
public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException {
throw new DataFormatException("Unexpected nested element in atom tag: " + theLocalPart);
}
@Override
public void string(String theData) {
if (myData == null) {
myData = theData;
} else {
// this shouldn't generally happen so it's ok that it's
// inefficient
myData = myData + theData;
}
}
@Override
protected IElement getCurrentElement() {
return null;
}
}
2014-02-26 17:13:49 -05:00
private class AtomState extends BaseState {
private Bundle myInstance;
private Class<? extends IResource> myResourceType;
2014-02-26 17:13:49 -05:00
public AtomState(Bundle theInstance, Class<? extends IResource> theResourceType) {
super(null);
2014-02-26 17:13:49 -05:00
myInstance = theInstance;
myResourceType = theResourceType;
2014-02-26 17:13:49 -05:00
}
@Override
public void endingElement() throws DataFormatException {
2014-02-26 17:13:49 -05:00
pop();
}
@Override
2014-02-27 11:57:50 -05:00
public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException {
2014-02-27 12:04:19 -05:00
if ("entry".equals(theLocalPart) && verifyNamespace(XmlParser.ATOM_NS, theNamespaceURI)) {
push(new AtomEntryState(myInstance, myResourceType));
2014-03-04 16:41:18 -05:00
} else if (theLocalPart.equals("published")) {
push(new AtomPrimitiveState(myInstance.getPublished()));
2014-02-27 12:04:19 -05:00
} else if (theLocalPart.equals("title")) {
2014-02-26 17:13:49 -05:00
push(new AtomPrimitiveState(myInstance.getTitle()));
} else if ("id".equals(theLocalPart)) {
push(new AtomPrimitiveState(myInstance.getBundleId()));
} else if ("link".equals(theLocalPart)) {
2014-02-27 11:57:50 -05:00
push(new AtomLinkState(myInstance));
} else if ("totalResults".equals(theLocalPart) && (verifyNamespace(XmlParser.OPENSEARCH_NS, theNamespaceURI) || verifyNamespace(Constants.OPENSEARCH_NS_OLDER, theNamespaceURI))) {
2014-02-27 11:57:50 -05:00
push(new AtomPrimitiveState(myInstance.getTotalResults()));
} else if ("updated".equals(theLocalPart)) {
push(new AtomPrimitiveState(myInstance.getUpdated()));
} else if ("author".equals(theLocalPart)) {
push(new AtomAuthorState(myInstance));
2014-02-27 16:51:43 -05:00
} else {
if (theNamespaceURI != null) {
throw new DataFormatException("Unexpected element: {" + theNamespaceURI + "}" + theLocalPart);
} else {
throw new DataFormatException("Unexpected element: " + theLocalPart);
}
2014-02-27 12:04:19 -05:00
}
2014-02-27 11:57:50 -05:00
// TODO: handle category and DSig
2014-02-26 17:13:49 -05:00
}
@Override
protected IElement getCurrentElement() {
return myInstance;
2014-02-26 17:13:49 -05:00
}
}
2014-02-20 12:13:05 -05:00
private abstract class BaseState {
2014-02-26 17:13:49 -05:00
private PreResourceState myPreResourceState;
2014-02-20 12:13:05 -05:00
private BaseState myStack;
2014-02-18 08:26:49 -05:00
public BaseState(PreResourceState thePreResourceState) {
super();
myPreResourceState = thePreResourceState;
}
2014-02-27 11:57:50 -05:00
@SuppressWarnings("unused")
2014-02-27 16:51:43 -05:00
public void attributeValue(String theName, String theValue) throws DataFormatException {
2014-02-27 11:57:50 -05:00
// ignore by default
}
2014-02-21 21:06:11 -05:00
public void endingElement() throws DataFormatException {
2014-02-27 11:57:50 -05:00
// ignore by default
}
2014-02-19 11:59:12 -05:00
2014-02-27 11:57:50 -05:00
@SuppressWarnings("unused")
public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException {
// ignore by default
}
2014-02-18 08:26:49 -05:00
2014-02-23 18:13:59 -05:00
/**
* Default implementation just handles undeclared extensions
*/
2014-03-10 18:17:30 -04:00
public void enteringNewElementExtension(@SuppressWarnings("unused") StartElement theElement, String theUrlAttr, boolean theIsModifier) {
if (myPreResourceState != null && getCurrentElement() instanceof ISupportsUndeclaredExtensions) {
2014-03-27 08:51:02 -04:00
ExtensionDt newExtension = new ExtensionDt(theIsModifier, theUrlAttr);
2014-03-10 18:17:30 -04:00
ISupportsUndeclaredExtensions elem = (ISupportsUndeclaredExtensions) getCurrentElement();
if (theIsModifier) {
elem.getUndeclaredModifierExtensions().add(newExtension);
} else {
elem.getUndeclaredExtensions().add(newExtension);
}
ExtensionState newState = new ExtensionState(myPreResourceState, newExtension);
2014-02-22 15:33:02 -05:00
push(newState);
2014-02-28 13:27:35 -05:00
} else {
2014-03-10 18:17:30 -04:00
throw new DataFormatException("Type " + getCurrentElement() + " does not support undeclared extentions, and found an extension with URL: " + theUrlAttr);
2014-02-22 15:33:02 -05:00
}
}
public PreResourceState getPreResourceState() {
return myPreResourceState;
}
public boolean isPreResource() {
return false;
}
2014-02-20 12:13:05 -05:00
public void setStack(BaseState theState) {
myStack = theState;
}
2014-02-27 11:57:50 -05:00
public void string(@SuppressWarnings("unused") String theData) {
// ignore by default
}
2014-02-27 16:51:43 -05:00
public void wereBack() {
// allow an implementor to override
}
2014-02-27 11:57:50 -05:00
public void xmlEvent(@SuppressWarnings("unused") XMLEvent theNextEvent) {
// ignore
}
protected Object getCurrentElement() {
2014-02-27 16:51:43 -05:00
return null;
}
2014-02-26 17:13:49 -05:00
}
2014-03-22 12:33:07 -04:00
private class ContainedResourcesState extends PreResourceState {
public ContainedResourcesState(PreResourceState thePreResourcesState) {
super(thePreResourcesState);
}
@Override
public void endingElement() throws DataFormatException {
pop();
}
@Override
public void wereBack() {
2014-03-17 08:57:57 -04:00
IResource res = getCurrentElement();
assert res != null;
2014-03-22 12:33:07 -04:00
if (res.getId() == null || res.getId().isEmpty()) {
2014-03-17 08:57:57 -04:00
ourLog.debug("Discarding contained resource with no ID!");
} else {
getPreResourceState().getContainedResources().put(res.getId().getValueAsString(), res);
}
2014-03-17 08:57:57 -04:00
getPreResourceState().getCurrentElement().getContained().getContainedResources().add(res);
}
}
2014-02-26 17:13:49 -05:00
private class DeclaredExtensionState extends BaseState {
private IElement myChildInstance;
private RuntimeChildDeclaredExtensionDefinition myDefinition;
private IElement myParentInstance;
private PreResourceState myPreResourceState;
2014-02-26 17:13:49 -05:00
public DeclaredExtensionState(PreResourceState thePreResourceState, RuntimeChildDeclaredExtensionDefinition theDefinition, IElement theParentInstance) {
super(thePreResourceState);
2014-03-22 12:33:07 -04:00
myPreResourceState = thePreResourceState;
2014-02-26 17:13:49 -05:00
myDefinition = theDefinition;
myParentInstance = theParentInstance;
}
@Override
public void endingElement() throws DataFormatException {
2014-02-26 17:13:49 -05:00
pop();
}
@Override
2014-02-27 11:57:50 -05:00
public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException {
2014-02-26 17:13:49 -05:00
BaseRuntimeElementDefinition<?> target = myDefinition.getChildByName(theLocalPart);
if (target == null) {
throw new DataFormatException("Unknown extension element name: " + theLocalPart);
}
switch (target.getChildType()) {
case COMPOSITE_DATATYPE: {
BaseRuntimeElementCompositeDefinition<?> compositeTarget = (BaseRuntimeElementCompositeDefinition<?>) target;
ICompositeDatatype newChildInstance = (ICompositeDatatype) compositeTarget.newInstance();
myDefinition.getMutator().addValue(myParentInstance, newChildInstance);
ElementCompositeState newState = new ElementCompositeState(myPreResourceState, compositeTarget, newChildInstance);
2014-02-26 17:13:49 -05:00
push(newState);
return;
}
case PRIMITIVE_DATATYPE: {
RuntimePrimitiveDatatypeDefinition primitiveTarget = (RuntimePrimitiveDatatypeDefinition) target;
IPrimitiveDatatype<?> newChildInstance = primitiveTarget.newInstance();
myDefinition.getMutator().addValue(myParentInstance, newChildInstance);
PrimitiveState newState = new PrimitiveState(getPreResourceState(), newChildInstance);
2014-02-26 17:13:49 -05:00
push(newState);
return;
}
case RESOURCE_REF: {
2014-03-10 18:17:30 -04:00
ResourceReferenceDt newChildInstance = new ResourceReferenceDt();
2014-02-26 17:13:49 -05:00
myDefinition.getMutator().addValue(myParentInstance, newChildInstance);
ResourceReferenceState newState = new ResourceReferenceState(getPreResourceState(), newChildInstance);
2014-02-26 17:13:49 -05:00
push(newState);
return;
}
case PRIMITIVE_XHTML:
case RESOURCE:
case RESOURCE_BLOCK:
case UNDECL_EXT:
case EXTENSION_DECLARED:
default:
break;
}
}
@Override
2014-03-10 18:17:30 -04:00
public void enteringNewElementExtension(StartElement theElement, String theUrlAttr, boolean theIsModifier) {
2014-02-26 17:13:49 -05:00
RuntimeChildDeclaredExtensionDefinition declaredExtension = myDefinition.getChildExtensionForUrl(theUrlAttr);
if (declaredExtension != null) {
if (myChildInstance == null) {
myChildInstance = myDefinition.newInstance();
myDefinition.getMutator().addValue(myParentInstance, myChildInstance);
}
BaseState newState = new DeclaredExtensionState(getPreResourceState(), declaredExtension, myChildInstance);
2014-02-26 17:13:49 -05:00
push(newState);
} else {
2014-03-10 18:17:30 -04:00
super.enteringNewElementExtension(theElement, theUrlAttr, theIsModifier);
2014-02-26 17:13:49 -05:00
}
}
@Override
protected IElement getCurrentElement() {
return myParentInstance;
}
2014-02-20 12:13:05 -05:00
}
2014-02-19 11:59:12 -05:00
2014-02-26 17:13:49 -05:00
private class ElementCompositeState extends BaseState {
2014-02-19 11:59:12 -05:00
private BaseRuntimeElementCompositeDefinition<?> myDefinition;
private ICompositeElement myInstance;
2014-02-18 08:26:49 -05:00
public ElementCompositeState(PreResourceState thePreResourceState, BaseRuntimeElementCompositeDefinition<?> theDef, ICompositeElement theInstance) {
super(thePreResourceState);
2014-02-19 11:59:12 -05:00
myDefinition = theDef;
2014-02-18 08:26:49 -05:00
myInstance = theInstance;
}
@Override
public void attributeValue(String theName, String theValue) throws DataFormatException {
if ("id".equals(theName)) {
myInstance.setId(new IdDt(theValue));
}
}
2014-02-27 07:39:36 -05:00
@SuppressWarnings("unchecked")
2014-02-18 08:26:49 -05:00
@Override
public void endingElement() {
2014-02-21 21:06:11 -05:00
pop();
if (myState == null) {
2014-02-27 07:39:36 -05:00
myObject = (T) myInstance;
2014-02-21 21:06:11 -05:00
}
}
@Override
2014-02-27 11:57:50 -05:00
public void enteringNewElement(String theNamespace, String theChildName) throws DataFormatException {
2014-03-28 17:11:56 -04:00
BaseRuntimeChildDefinition child;
try {
child = myDefinition.getChildByNameOrThrowDataFormatException(theChildName);
} catch (DataFormatException e) {
if (false) {// TODO: make this configurable
throw e;
}
ourLog.warn(e.getMessage());
push(new SwallowChildrenWholeState(getPreResourceState()));
return;
}
2014-02-19 11:59:12 -05:00
BaseRuntimeElementDefinition<?> target = child.getChildByName(theChildName);
2014-02-26 09:03:43 -05:00
if (target == null) {
throw new DataFormatException("Found unexpected element '" + theChildName + "' in parent element '" + myDefinition.getName() + "'. Valid names are: " + child.getValidChildNames());
}
2014-02-26 17:13:49 -05:00
2014-02-19 11:59:12 -05:00
switch (target.getChildType()) {
case COMPOSITE_DATATYPE: {
BaseRuntimeElementCompositeDefinition<?> compositeTarget = (BaseRuntimeElementCompositeDefinition<?>) target;
2014-03-10 18:17:30 -04:00
ICompositeDatatype newChildInstance = (ICompositeDatatype) compositeTarget.newInstance(child.getInstanceConstructorArguments());
2014-02-19 11:59:12 -05:00
child.getMutator().addValue(myInstance, newChildInstance);
ElementCompositeState newState = new ElementCompositeState(getPreResourceState(), compositeTarget, newChildInstance);
2014-02-19 11:59:12 -05:00
push(newState);
2014-02-20 12:13:05 -05:00
return;
2014-02-19 11:59:12 -05:00
}
case PRIMITIVE_DATATYPE: {
RuntimePrimitiveDatatypeDefinition primitiveTarget = (RuntimePrimitiveDatatypeDefinition) target;
IPrimitiveDatatype<?> newChildInstance;
newChildInstance = primitiveTarget.newInstance(child.getInstanceConstructorArguments());
2014-02-19 11:59:12 -05:00
child.getMutator().addValue(myInstance, newChildInstance);
PrimitiveState newState = new PrimitiveState(getPreResourceState(), newChildInstance);
2014-02-19 11:59:12 -05:00
push(newState);
2014-02-20 12:13:05 -05:00
return;
2014-02-19 11:59:12 -05:00
}
2014-02-19 17:33:46 -05:00
case RESOURCE_REF: {
RuntimeResourceReferenceDefinition resourceRefTarget = (RuntimeResourceReferenceDefinition) target;
2014-03-10 18:17:30 -04:00
ResourceReferenceDt newChildInstance = new ResourceReferenceDt();
getPreResourceState().getResourceReferences().add(newChildInstance);
2014-02-19 17:33:46 -05:00
child.getMutator().addValue(myInstance, newChildInstance);
ResourceReferenceState newState = new ResourceReferenceState(getPreResourceState(), newChildInstance);
2014-02-19 17:33:46 -05:00
push(newState);
2014-02-20 12:13:05 -05:00
return;
}
case RESOURCE_BLOCK: {
RuntimeResourceBlockDefinition blockTarget = (RuntimeResourceBlockDefinition) target;
IResourceBlock newBlockInstance = blockTarget.newInstance();
child.getMutator().addValue(myInstance, newBlockInstance);
ElementCompositeState newState = new ElementCompositeState(getPreResourceState(), blockTarget, newBlockInstance);
2014-02-20 12:13:05 -05:00
push(newState);
return;
2014-02-19 11:59:12 -05:00
}
2014-02-22 15:33:02 -05:00
case PRIMITIVE_XHTML: {
RuntimePrimitiveDatatypeNarrativeDefinition xhtmlTarget = (RuntimePrimitiveDatatypeNarrativeDefinition) target;
XhtmlDt newDt = xhtmlTarget.newInstance();
child.getMutator().addValue(myInstance, newDt);
XhtmlState state = new XhtmlState(getPreResourceState(), newDt, true);
2014-02-22 15:33:02 -05:00
push(state);
return;
}
2014-03-17 08:57:57 -04:00
case CONTAINED_RESOURCES: {
RuntimeElemContainedResources targetElem = (RuntimeElemContainedResources) target;
2014-03-22 12:33:07 -04:00
List<? extends IElement> values = child.getAccessor().getValues(myInstance);
ContainedDt newDt;
if (values == null || values.isEmpty() || values.get(0) == null) {
newDt = targetElem.newInstance();
child.getMutator().addValue(myInstance, newDt);
2014-04-14 08:40:30 -04:00
} else {
2014-03-22 12:33:07 -04:00
newDt = (ContainedDt) values.get(0);
}
2014-03-17 08:57:57 -04:00
ContainedResourcesState state = new ContainedResourcesState(getPreResourceState());
push(state);
return;
}
2014-02-22 15:33:02 -05:00
case UNDECL_EXT:
2014-02-21 21:06:11 -05:00
case RESOURCE: {
2014-02-20 12:13:05 -05:00
// Throw an exception because this shouldn't happen here
break;
2014-02-19 11:59:12 -05:00
}
2014-02-22 15:33:02 -05:00
}
throw new DataFormatException("Illegal resource position: " + target.getChildType());
}
@Override
2014-03-10 18:17:30 -04:00
public void enteringNewElementExtension(StartElement theElement, String theUrlAttr, boolean theIsModifier) {
2014-02-26 17:13:49 -05:00
RuntimeChildDeclaredExtensionDefinition declaredExtension = myDefinition.getDeclaredExtension(theUrlAttr);
if (declaredExtension != null) {
BaseState newState = new DeclaredExtensionState(getPreResourceState(), declaredExtension, myInstance);
2014-02-26 17:13:49 -05:00
push(newState);
} else {
2014-03-10 18:17:30 -04:00
super.enteringNewElementExtension(theElement, theUrlAttr, theIsModifier);
2014-02-26 17:13:49 -05:00
}
2014-02-22 15:33:02 -05:00
}
2014-02-19 11:59:12 -05:00
2014-02-22 15:33:02 -05:00
@Override
protected IElement getCurrentElement() {
return myInstance;
}
}
private class ExtensionState extends BaseState {
2014-03-27 08:51:02 -04:00
private ExtensionDt myExtension;
2014-02-22 15:33:02 -05:00
2014-03-27 08:51:02 -04:00
public ExtensionState(PreResourceState thePreResourceState, ExtensionDt theExtension) {
super(thePreResourceState);
2014-02-22 15:33:02 -05:00
myExtension = theExtension;
}
@Override
public void endingElement() throws DataFormatException {
2014-02-22 15:33:02 -05:00
if (myExtension.getValue() != null && myExtension.getUndeclaredExtensions().size() > 0) {
throw new DataFormatException("Extension must not have both a value and other contained extensions");
2014-02-18 18:10:50 -05:00
}
2014-02-22 15:33:02 -05:00
pop();
}
@Override
2014-02-27 11:57:50 -05:00
public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException {
2014-02-22 15:33:02 -05:00
BaseRuntimeElementDefinition<?> target = myContext.getRuntimeChildUndeclaredExtensionDefinition().getChildByName(theLocalPart);
if (target == null) {
throw new DataFormatException("Unknown extension element name: " + theLocalPart);
2014-02-21 21:06:11 -05:00
}
2014-02-22 15:33:02 -05:00
switch (target.getChildType()) {
case COMPOSITE_DATATYPE: {
BaseRuntimeElementCompositeDefinition<?> compositeTarget = (BaseRuntimeElementCompositeDefinition<?>) target;
ICompositeDatatype newChildInstance = (ICompositeDatatype) compositeTarget.newInstance();
myExtension.setValue(newChildInstance);
ElementCompositeState newState = new ElementCompositeState(getPreResourceState(), compositeTarget, newChildInstance);
2014-02-22 15:33:02 -05:00
push(newState);
return;
}
case PRIMITIVE_DATATYPE: {
RuntimePrimitiveDatatypeDefinition primitiveTarget = (RuntimePrimitiveDatatypeDefinition) target;
IPrimitiveDatatype<?> newChildInstance = primitiveTarget.newInstance();
myExtension.setValue(newChildInstance);
PrimitiveState newState = new PrimitiveState(getPreResourceState(), newChildInstance);
2014-02-22 15:33:02 -05:00
push(newState);
return;
}
case RESOURCE_REF: {
2014-03-10 18:17:30 -04:00
ResourceReferenceDt newChildInstance = new ResourceReferenceDt();
2014-02-22 15:33:02 -05:00
myExtension.setValue(newChildInstance);
ResourceReferenceState newState = new ResourceReferenceState(getPreResourceState(), newChildInstance);
2014-02-22 15:33:02 -05:00
push(newState);
return;
}
case PRIMITIVE_XHTML:
case RESOURCE:
case RESOURCE_BLOCK:
case UNDECL_EXT:
break;
}
}
2014-02-26 17:13:49 -05:00
@Override
protected IElement getCurrentElement() {
return myExtension;
}
}
2014-02-27 16:51:43 -05:00
private class PreAtomState extends BaseState {
private Bundle myInstance;
private Class<? extends IResource> myResourceType;
2014-02-26 17:13:49 -05:00
public PreAtomState(Class<? extends IResource> theResourceType) {
super(null);
myResourceType = theResourceType;
}
2014-02-26 17:13:49 -05:00
@Override
public void endingElement() throws DataFormatException {
2014-02-26 17:13:49 -05:00
// ignore
}
2014-02-27 16:51:43 -05:00
@Override
public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException {
if (!"feed".equals(theLocalPart)) {
throw new DataFormatException("Expecting outer element called 'feed', found: " + theLocalPart);
}
myInstance = new Bundle();
push(new AtomState(myInstance, myResourceType));
2014-02-27 16:51:43 -05:00
}
@SuppressWarnings("unchecked")
@Override
public void wereBack() {
myObject = (T) myInstance;
}
@Override
protected IElement getCurrentElement() {
return myInstance;
2014-02-27 16:51:43 -05:00
}
}
2014-03-22 12:33:07 -04:00
2014-02-27 16:51:43 -05:00
private class PreResourceState extends BaseState {
private Map<String, IResource> myContainedResources = new HashMap<String, IResource>();
2014-02-27 16:51:43 -05:00
private BundleEntry myEntry;
private IResource myInstance;
private List<ResourceReferenceDt> myResourceReferences = new ArrayList<ResourceReferenceDt>();
2014-03-22 12:33:07 -04:00
2014-03-07 16:23:49 -05:00
private Class<? extends IResource> myResourceType;
2014-02-27 16:51:43 -05:00
public PreResourceState(BundleEntry theEntry, Class<? extends IResource> theResourceType) {
super(null);
myEntry = theEntry;
myResourceType = theResourceType;
}
2014-03-07 16:23:49 -05:00
/**
* @param theResourceType
* May be null
*/
public PreResourceState(Class<? extends IResource> theResourceType) {
super(null);
2014-03-07 16:23:49 -05:00
myResourceType = theResourceType;
2014-02-27 16:51:43 -05:00
}
public PreResourceState(PreResourceState thePreResourcesState) {
super(thePreResourcesState);
2014-02-27 16:51:43 -05:00
}
2014-02-26 17:13:49 -05:00
@Override
public void endingElement() throws DataFormatException {
2014-02-27 16:51:43 -05:00
pop();
2014-02-26 17:13:49 -05:00
}
2014-03-22 12:33:07 -04:00
2014-02-26 17:13:49 -05:00
@Override
2014-02-27 11:57:50 -05:00
public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException {
2014-03-07 16:23:49 -05:00
BaseRuntimeElementDefinition<?> definition;
if (myResourceType == null) {
definition = myContext.getResourceDefinition(theLocalPart);
if (!(definition instanceof RuntimeResourceDefinition)) {
throw new DataFormatException("Element '" + theLocalPart + "' is not a resource, expected a resource at this position");
}
} else {
definition = myContext.getResourceDefinition(myResourceType);
if (!StringUtils.equals(theLocalPart, definition.getName())) {
throw new DataFormatException("Incorrect resource root element '" + theLocalPart + "', expected: '" + definition.getName() + "'");
}
2014-02-26 17:13:49 -05:00
}
RuntimeResourceDefinition def = (RuntimeResourceDefinition) definition;
myInstance = def.newInstance();
2014-02-27 16:51:43 -05:00
if (myEntry != null) {
myEntry.setResource(myInstance);
}
2014-02-26 17:13:49 -05:00
2014-03-17 08:57:57 -04:00
push(new ElementCompositeState(this, def, myInstance));
}
public Map<String, IResource> getContainedResources() {
return myContainedResources;
}
public List<ResourceReferenceDt> getResourceReferences() {
return myResourceReferences;
2014-02-26 17:13:49 -05:00
}
@Override
public boolean isPreResource() {
return true;
}
2014-02-27 16:51:43 -05:00
@SuppressWarnings("unchecked")
2014-02-22 15:33:02 -05:00
@Override
2014-02-27 16:51:43 -05:00
public void wereBack() {
if (myEntry == null) {
myObject = (T) myInstance;
}
2014-03-22 12:33:07 -04:00
for (ResourceReferenceDt nextRef : myResourceReferences) {
String ref = nextRef.getReference().getValue();
2014-03-22 12:33:07 -04:00
if (isNotBlank(ref)) {
if (ref.startsWith("#")) {
IResource target = myContainedResources.get(ref.substring(1));
if (target != null) {
nextRef.setResource(target);
} else {
ourLog.warn("Resource contains unknown local ref: " + ref);
}
}
}
}
2014-03-22 12:33:07 -04:00
2014-02-26 17:13:49 -05:00
}
@Override
2014-03-17 08:57:57 -04:00
protected IResource getCurrentElement() {
2014-02-27 16:51:43 -05:00
return myInstance;
2014-02-18 08:26:49 -05:00
}
2014-02-19 11:59:12 -05:00
2014-02-18 08:26:49 -05:00
}
private class PreTagListState extends BaseState {
private TagList myTagList;
public PreTagListState() {
super(null);
myTagList = new TagList();
}
@Override
public void endingElement() throws DataFormatException {
pop();
}
@Override
public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException {
if (!TagList.ELEMENT_NAME_LC.equals(theLocalPart.toLowerCase())) {
throw new DataFormatException("resourceType does not appear to be 'TagList', found: " + theLocalPart);
}
push(new TagListState(myTagList));
}
@Override
public boolean isPreResource() {
return true;
}
@SuppressWarnings("unchecked")
@Override
public void wereBack() {
myObject = (T) myTagList;
}
@Override
protected TagList getCurrentElement() {
return myTagList;
}
}
2014-02-19 11:59:12 -05:00
private class PrimitiveState extends BaseState {
private IPrimitiveDatatype<?> myInstance;
public PrimitiveState(PreResourceState thePreResourceState, IPrimitiveDatatype<?> theInstance) {
super(thePreResourceState);
2014-02-19 11:59:12 -05:00
myInstance = theInstance;
}
@Override
2014-02-27 16:51:43 -05:00
public void attributeValue(String theName, String theValue) throws DataFormatException {
if ("value".equals(theName)) {
myInstance.setValueAsString(theValue);
2014-03-22 12:33:07 -04:00
} else if ("id".equals(theName)) {
myInstance.setId(new IdDt(theValue));
2014-02-27 16:51:43 -05:00
}
2014-02-19 11:59:12 -05:00
}
@Override
public void endingElement() {
2014-02-21 21:06:11 -05:00
pop();
2014-02-19 11:59:12 -05:00
}
2014-03-06 17:12:05 -05:00
// @Override
// public void enteringNewElementExtension(StartElement theElement,
// String theUrlAttr) {
2014-03-06 17:12:05 -05:00
// if (myInstance instanceof ISupportsUndeclaredExtensions) {
// UndeclaredExtension ext = new UndeclaredExtension(theUrlAttr);
// ((ISupportsUndeclaredExtensions)
// myInstance).getUndeclaredExtensions().add(ext);
2014-03-06 17:12:05 -05:00
// push(new ExtensionState(ext));
// }
// }
2014-02-28 13:27:35 -05:00
2014-02-19 11:59:12 -05:00
@Override
2014-02-27 11:57:50 -05:00
public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException {
2014-03-22 12:33:07 -04:00
throw new Error("Element " + theLocalPart + " in primitive!"); // TODO:
// can
// this
// happen?
2014-02-19 11:59:12 -05:00
}
2014-02-22 15:33:02 -05:00
@Override
2014-02-26 17:13:49 -05:00
protected IElement getCurrentElement() {
return myInstance;
2014-02-22 15:33:02 -05:00
}
2014-02-19 11:59:12 -05:00
}
2014-02-21 21:06:11 -05:00
private class ResourceReferenceState extends BaseState {
2014-03-10 18:17:30 -04:00
private ResourceReferenceDt myInstance;
2014-02-21 21:06:11 -05:00
private ResourceReferenceSubState mySubState;
public ResourceReferenceState(PreResourceState thePreResourceState, ResourceReferenceDt theInstance) {
super(thePreResourceState);
2014-02-21 21:06:11 -05:00
myInstance = theInstance;
mySubState = ResourceReferenceSubState.INITIAL;
}
@Override
2014-02-27 16:51:43 -05:00
public void attributeValue(String theName, String theValue) throws DataFormatException {
if (!"value".equals(theName)) {
return;
}
2014-03-06 17:12:05 -05:00
2014-02-21 21:06:11 -05:00
switch (mySubState) {
case DISPLAY:
myInstance.setDisplay(theValue);
break;
case INITIAL:
throw new DataFormatException("Unexpected attribute: " + theValue);
case REFERENCE:
myInstance.setReference(theValue);
break;
}
}
@Override
public void endingElement() {
2014-02-21 21:06:11 -05:00
switch (mySubState) {
case INITIAL:
pop();
break;
case DISPLAY:
case REFERENCE:
mySubState = ResourceReferenceSubState.INITIAL;
}
}
@Override
2014-02-27 11:57:50 -05:00
public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException {
2014-02-21 21:06:11 -05:00
switch (mySubState) {
case INITIAL:
if ("display".equals(theLocalPart)) {
mySubState = ResourceReferenceSubState.DISPLAY;
break;
} else if ("reference".equals(theLocalPart)) {
mySubState = ResourceReferenceSubState.REFERENCE;
break;
2014-03-22 12:33:07 -04:00
} else if ("resource".equals(theLocalPart)) {
mySubState = ResourceReferenceSubState.REFERENCE;
break;
2014-02-21 21:06:11 -05:00
}
//$FALL-THROUGH$
case DISPLAY:
case REFERENCE:
throw new DataFormatException("Unexpected element: " + theLocalPart);
}
}
2014-02-22 15:33:02 -05:00
@Override
2014-02-26 17:13:49 -05:00
protected IElement getCurrentElement() {
return myInstance;
2014-02-22 15:33:02 -05:00
}
2014-02-18 08:26:49 -05:00
}
2014-02-19 11:59:12 -05:00
2014-02-21 21:06:11 -05:00
private enum ResourceReferenceSubState {
DISPLAY, INITIAL, REFERENCE
2014-02-19 11:59:12 -05:00
}
private class SwallowChildrenWholeState extends BaseState {
private int myDepth;
public SwallowChildrenWholeState(PreResourceState thePreResourceState) {
super(thePreResourceState);
}
@Override
public void endingElement() throws DataFormatException {
myDepth--;
if (myDepth < 0) {
pop();
}
}
@Override
public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException {
myDepth++;
}
}
private class TagListState extends BaseState {
private TagList myTagList;
public TagListState(TagList theTagList) {
super(null);
myTagList = theTagList;
}
@Override
public void endingElement() throws DataFormatException {
pop();
}
@Override
public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException {
if (TagList.ATTR_CATEGORY.equals(theLocalPart)) {
push(new TagState(myTagList.addTag()));
} else {
throw new DataFormatException("Unexpected element: " + theLocalPart);
}
}
}
private class TagState extends BaseState {
private static final int LABEL = 2;
private static final int NONE = 0;
private static final int SCHEME = 3;
private static final int TERM = 1;
private int mySubState = 0;
private Tag myTag;
public TagState(Tag theTag) {
super(null);
myTag = theTag;
}
@Override
public void attributeValue(String theName, String theValue) throws DataFormatException {
String value = defaultIfBlank(theValue, null);
switch (mySubState) {
case TERM:
myTag.setTerm(value);
break;
case LABEL:
myTag.setLabel(value);
break;
case SCHEME:
myTag.setScheme(value);
break;
case NONE:
// This handles JSON encoding, which is a bit weird
enteringNewElement(null, theName);
attributeValue(null, value);
endingElement();
break;
}
}
@Override
public void endingElement() throws DataFormatException {
if (mySubState != NONE) {
mySubState = NONE;
} else {
pop();
}
}
@Override
public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException {
if (Tag.ATTR_TERM.equals(theLocalPart)) {
mySubState = TERM;
} else if (Tag.ATTR_SCHEME.equals(theLocalPart)) {
mySubState = SCHEME;
} else if (Tag.ATTR_LABEL.equals(theLocalPart)) {
mySubState = LABEL;
} else {
throw new DataFormatException("Unexpected element: " + theLocalPart);
}
}
}
2014-02-21 21:06:11 -05:00
private class XhtmlState extends BaseState {
2014-02-22 15:33:02 -05:00
private int myDepth;
private XhtmlDt myDt;
private List<XMLEvent> myEvents = new ArrayList<XMLEvent>();
2014-02-27 16:51:43 -05:00
private boolean myIncludeOuterEvent;
2014-02-22 15:33:02 -05:00
private XhtmlState(PreResourceState thePreResourceState, XhtmlDt theXhtmlDt, boolean theIncludeOuterEvent) throws DataFormatException {
super(thePreResourceState);
2014-02-27 16:51:43 -05:00
myDepth = 0;
2014-02-22 15:33:02 -05:00
myDt = theXhtmlDt;
2014-02-27 16:51:43 -05:00
myIncludeOuterEvent = theIncludeOuterEvent;
2014-02-21 21:06:11 -05:00
}
2014-03-22 12:33:07 -04:00
@Override
public void attributeValue(String theName, String theValue) throws DataFormatException {
if (myJsonMode) {
myDt.setValueAsString(theValue);
return;
}
super.attributeValue(theName, theValue);
}
@Override
public void endingElement() throws DataFormatException {
2014-03-22 12:33:07 -04:00
if (myJsonMode) {
pop();
return;
}
super.endingElement();
2014-03-22 12:33:07 -04:00
}
2014-02-21 21:06:11 -05:00
@Override
2014-02-27 11:57:50 -05:00
public void xmlEvent(XMLEvent theEvent) {
2014-02-27 16:51:43 -05:00
if (theEvent.isEndElement()) {
myDepth--;
}
if (myIncludeOuterEvent || myDepth > 0) {
myEvents.add(theEvent);
}
2014-02-22 15:33:02 -05:00
2014-02-27 11:57:50 -05:00
if (theEvent.isStartElement()) {
myDepth++;
}
2014-03-06 17:12:05 -05:00
2014-02-27 11:57:50 -05:00
if (theEvent.isEndElement()) {
if (myDepth == 0) {
myDt.setValue(myEvents);
pop();
}
2014-02-21 21:06:11 -05:00
}
}
2014-02-27 16:51:43 -05:00
@Override
protected IElement getCurrentElement() {
return myDt;
}
2014-02-27 11:57:50 -05:00
2014-02-19 11:59:12 -05:00
}
2014-02-18 08:26:49 -05:00
}