001package org.hl7.fhir.dstu2.formats;
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
032/*
033Copyright (c) 2011+, HL7, Inc
034All rights reserved.
035
036Redistribution and use in source and binary forms, with or without modification, 
037are permitted provided that the following conditions are met:
038
039 * Redistributions of source code must retain the above copyright notice, this 
040   list of conditions and the following disclaimer.
041 * Redistributions in binary form must reproduce the above copyright notice, 
042   this list of conditions and the following disclaimer in the documentation 
043   and/or other materials provided with the distribution.
044 * Neither the name of HL7 nor the names of its contributors may be used to 
045   endorse or promote products derived from this software without specific 
046   prior written permission.
047
048THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
049ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
050WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
051IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
052INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
053NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
054PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
055WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
056ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
057POSSIBILITY OF SUCH DAMAGE.
058
059*/
060
061import java.math.BigDecimal;
062import java.net.URI;
063
064import org.apache.commons.codec.binary.Base64;
065
066public abstract class FormatUtilities {
067  public static final String ID_REGEX = "[A-Za-z0-9\\-\\.]{1,64}";
068  public static final String FHIR_NS = "http://hl7.org/fhir";
069  public static final String XHTML_NS = "http://www.w3.org/1999/xhtml";
070
071  protected String toString(String value) {
072    return value;
073  }
074
075  protected String toString(int value) {
076    return java.lang.Integer.toString(value);
077  }
078
079  protected String toString(boolean value) {
080    return java.lang.Boolean.toString(value);
081  }
082
083  protected String toString(BigDecimal value) {
084    return value.toString();
085  }
086
087  protected String toString(URI value) {
088    return value.toString();
089  }
090
091  public static String toString(byte[] value) {
092    byte[] encodeBase64 = Base64.encodeBase64(value);
093    return new String(encodeBase64);
094  }
095
096  public static boolean isValidId(String tail) {
097    return tail.matches(ID_REGEX);
098  }
099
100  public static String makeId(String candidate) {
101    StringBuilder b = new StringBuilder();
102    for (char c : candidate.toCharArray())
103      if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.' || c == '-')
104        b.append(c);
105    return b.toString();
106  }
107
108  public static ParserBase makeParser(FhirFormat format) {
109    switch (format) {
110    case XML:
111      return new XmlParser();
112    case JSON:
113      return new JsonParser();
114    case TURTLE:
115      throw new Error("unsupported Format " + format.toString()); // return new TurtleParser();
116    case VBAR:
117      throw new Error("unsupported Format " + format.toString()); //
118    case TEXT:
119      throw new Error("unsupported Format " + format.toString()); //
120    }
121    throw new Error("unsupported Format " + format.toString());
122  }
123
124  public static ParserBase makeParser(String format) {
125    if ("XML".equalsIgnoreCase(format))
126      return new XmlParser();
127    if ("JSON".equalsIgnoreCase(format))
128      return new JsonParser();
129    if ("TURTLE".equalsIgnoreCase(format))
130      throw new Error("unsupported Format " + format.toString()); // return new TurtleParser();
131    if ("JSONLD".equalsIgnoreCase(format))
132      throw new Error("unsupported Format " + format.toString()); // return new JsonLdParser();
133    if ("VBAR".equalsIgnoreCase(format))
134      throw new Error("unsupported Format " + format.toString()); //
135    if ("TEXT".equalsIgnoreCase(format))
136      throw new Error("unsupported Format " + format.toString()); //
137    throw new Error("unsupported Format " + format);
138  }
139
140}