001package org.hl7.fhir.r4.model;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032import java.io.IOException;
033import java.util.List;
034
035import org.hl7.fhir.exceptions.FHIRException;
036import org.hl7.fhir.utilities.xhtml.NodeType;
037import org.hl7.fhir.utilities.xhtml.XhtmlComposer;
038import org.hl7.fhir.utilities.xhtml.XhtmlNode;
039
040public class XhtmlType extends Element {
041
042  private Narrative place;
043
044  public XhtmlType(Narrative place) {
045    super();
046    this.place = place;
047  }
048
049  public XhtmlType() {
050    this(new Narrative());
051    // "<div xmlns=\""+FormatUtilities.XHTML_NS+"\"></div>"
052  }
053
054  @Override
055  public String fhirType() {
056    return "xhtml";
057  }
058
059  @Override
060  protected void listChildren(List<Property> result) {
061  }
062
063  @Override
064  public String getIdBase() {
065    return null;
066  }
067
068  @Override
069  public void setIdBase(String value) {
070  }
071
072  @Override
073  public Element copy() {
074    return null;
075  }
076
077  public XhtmlNode getValue() {
078    return place == null ? new XhtmlNode(NodeType.Element, "div") : place.getDiv();
079  }
080
081  @Override
082  public Base setProperty(int hash, String name, Base value) throws FHIRException {
083    if ("value".equals(name)) {
084      if (value instanceof StringType) {
085        // div is already generated with getValue, we cannot just overwrite it
086        place.getDiv().setValueAsString(((StringType) value).asStringValue());
087      } else {
088        place.setDiv(castToXhtml(value));
089      }
090      return value;
091    } else
092      return super.setProperty(hash, name, value);
093  }
094
095  @Override
096  public Base[] getProperty(int hash, String name, boolean checkValid) throws FHIRException {
097    if ("value".equals(name))
098      return new Base[] { this };
099    return super.getProperty(hash, name, checkValid);
100  }
101
102  @Override
103  public String primitiveValue() {
104    try {
105      return new XhtmlComposer(false).compose(getValue());
106    } catch (IOException e) {
107    }
108    return null;
109  }
110
111  @Override
112  public boolean isPrimitive() {
113    return true;
114  }
115
116  @Override
117  public boolean hasPrimitiveValue() {
118    return true;
119  }
120
121}