Package ca.uhn.fhir.parser

Source Code of ca.uhn.fhir.parser.BaseParser

package ca.uhn.fhir.parser;

import java.io.Reader;
import java.io.StringReader;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
import ca.uhn.fhir.context.BaseRuntimeDeclaredChildDefinition;
import ca.uhn.fhir.context.ConfigurationException;
import ca.uhn.fhir.context.RuntimeChildChoiceDefinition;
import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.IElement;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt;
import ca.uhn.fhir.model.primitive.IdDt;

public abstract class BaseParser implements IParser {

  private boolean mySuppressNarratives;

  @SuppressWarnings("cast")
  @Override
  public <T extends IResource> T parseResource(Class<T> theResourceType, String theMessageString) {
    StringReader reader = new StringReader(theMessageString);
    return (T) parseResource(theResourceType, reader);
  }

  @Override
  public Bundle parseBundle(String theXml) throws ConfigurationException, DataFormatException {
    StringReader reader = new StringReader(theXml);
    return parseBundle(reader);
  }

  @Override
  public IResource parseResource(String theMessageString) throws ConfigurationException, DataFormatException {
    return parseResource(null, theMessageString);
  }

  @Override
  public IResource parseResource(Reader theReader) throws ConfigurationException, DataFormatException {
    return parseResource(null, theReader);
  }

  public void containResourcesForEncoding(IResource theResource) {
    List<ResourceReferenceDt> allElements = theResource.getAllPopulatedChildElementsOfType(ResourceReferenceDt.class);

    Set<String> allIds = new HashSet<String>();

    for (ResourceReferenceDt next : allElements) {
      IResource resource = next.getResource();
      if (resource != null) {
        if (resource.getId().isEmpty()) {
          resource.setId(new IdDt(UUID.randomUUID().toString()));
        }

        if (!allIds.contains(resource.getId().getValue())) {
          theResource.getContained().getContainedResources().add(resource);
          allIds.add(resource.getId().getValue());
        }

        next.setReference("#" + resource.getId().getValue());
      }
    }

  }

  protected void throwExceptionForUnknownChildType(BaseRuntimeChildDefinition nextChild, Class<? extends IElement> type) {
    if (nextChild instanceof BaseRuntimeDeclaredChildDefinition) {
      StringBuilder b = new StringBuilder();
      b.append(((BaseRuntimeDeclaredChildDefinition) nextChild).getElementName());
      b.append(" has type ");
      b.append(type);
      b.append(" but this is not a valid type for this element");
      if (nextChild instanceof RuntimeChildChoiceDefinition) {
        RuntimeChildChoiceDefinition choice = (RuntimeChildChoiceDefinition) nextChild;
        b.append(" - Expected one of: " + choice.getValidChildTypes());
      }
      throw new DataFormatException(b.toString());
    }
    throw new DataFormatException(nextChild + " has no child of type " + type);
  }

  @Override
  public IParser setSuppressNarratives(boolean theSuppressNarratives) {
    mySuppressNarratives = theSuppressNarratives;
    return this;
  }

  /**
   * If set to <code>true</code> (default is <code>false</code>), narratives
   * will not be included in the encoded values.
   */
  public boolean getSuppressNarratives() {
    return mySuppressNarratives;
  }

}
TOP

Related Classes of ca.uhn.fhir.parser.BaseParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.