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
040
041public class XhtmlType extends PrimitiveType<String> {
042
043  private Narrative place;
044  
045  public XhtmlType(Narrative place) {
046    super();
047    this.place = place;
048  }
049
050  public XhtmlType() {
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 PrimitiveType<String> copy() {
074    return null;
075  }
076
077  public String getValue() {
078    return primitiveValue();
079  }
080
081  public XhtmlNode getXhtml() {
082    return place == null ? new XhtmlNode(NodeType.Element, "div") : place.getDiv();
083  }
084
085  @Override
086  public Base setProperty(int hash, String name, Base value) throws FHIRException {
087    if ("value".equals(name)) {
088      if (value instanceof StringType) {
089        // div is already generated with getValue, we cannot just overwrite it
090        place.getDiv().setValueAsString(((StringType) value).asStringValue());
091      } else {
092        place.setDiv(castToXhtml(value));
093      }
094      return value;
095    } else
096      return super.setProperty(hash, name, value);
097  }
098
099  @Override
100  public Base[] getProperty(int hash, String name, boolean checkValid) throws FHIRException {
101    if ("value".equals(name))
102      return new Base[] {this};
103    return super.getProperty(hash, name, checkValid);
104  }
105
106  @Override
107  public String primitiveValue() {
108    try {
109      return new XhtmlComposer(false).compose(getXhtml());
110    } catch (IOException e) {
111    }
112    return null;
113  }  
114  
115  @Override
116  public boolean isPrimitive() {
117    return true;
118  }
119  
120  @Override
121  public boolean hasPrimitiveValue() {
122    return true;
123  }
124
125  @Override
126  protected String encode(String theValue) {
127    return theValue;
128  }
129
130  @Override
131  protected String parse(String theValue) {
132    return theValue;
133  }
134
135  public Narrative getPlace() {
136    return place;
137  }
138
139  public void setPlace(Narrative place) {
140    this.place = place;
141  }
142
143  @Override
144  public Base setXhtml(XhtmlNode node) {
145    return place.setDiv(node);
146  }
147  
148
149}