001package org.hl7.fhir.r4.formats; 002 003/* 004Copyright (c) 2011+, HL7, Inc 005All rights reserved. 006 007Redistribution and use in source and binary forms, with or without modification, 008are 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 019THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 022IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 024NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 025PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 027ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 028POSSIBILITY OF SUCH DAMAGE. 029 030*/ 031 032import java.math.BigDecimal; 033import java.net.URI; 034 035import org.apache.commons.codec.binary.Base64; 036import org.hl7.fhir.exceptions.FHIRException; 037import org.hl7.fhir.r4.elementmodel.Manager.FhirFormat; 038 039public abstract class FormatUtilities { 040 public static final String ID_REGEX = "[A-Za-z0-9\\-\\.]{1,64}"; 041 public static final String FHIR_NS = "http://hl7.org/fhir"; 042 public static final String XHTML_NS = "http://www.w3.org/1999/xhtml"; 043 public static final String NS_XSI = "http://www.w3.org/2001/XMLSchema-instance"; 044 private static final int MAX_SCAN_LENGTH = 1000; // how many characters to scan into content when autodetermining format 045 046 protected String toString(String value) { 047 return value; 048 } 049 050 protected String toString(int value) { 051 return java.lang.Integer.toString(value); 052 } 053 054 protected String toString(boolean value) { 055 return java.lang.Boolean.toString(value); 056 } 057 058 protected String toString(BigDecimal value) { 059 return value.toString(); 060 } 061 062 protected String toString(URI value) { 063 return value.toString(); 064 } 065 066 public static String toString(byte[] value) { 067 byte[] encodeBase64 = Base64.encodeBase64(value); 068 return new String(encodeBase64); 069 } 070 071 public static boolean isValidId(String tail) { 072 return tail.matches(ID_REGEX); 073 } 074 075 public static String makeId(String candidate) { 076 StringBuilder b = new StringBuilder(); 077 for (char c : candidate.toCharArray()) 078 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.' || c == '-') 079 b.append(c); 080 return b.toString(); 081 } 082 083 public static ParserBase makeParser(FhirFormat format) { 084 switch (format) { 085 case XML : return new XmlParser(); 086 case JSON : return new JsonParser(); 087 case TURTLE : throw new Error("unsupported Format "+format.toString()); // return new TurtleParser(); 088 case VBAR : throw new Error("unsupported Format "+format.toString()); // 089 case TEXT : throw new Error("unsupported Format "+format.toString()); // 090 } 091 throw new Error("unsupported Format "+format.toString()); 092 } 093 094 public static ParserBase makeParser(String format) { 095 if ("XML".equalsIgnoreCase(format)) return new XmlParser(); 096 if ("JSON".equalsIgnoreCase(format)) return new JsonParser(); 097 if ("TURTLE".equalsIgnoreCase(format)) throw new Error("unsupported Format "+format.toString()); // return new TurtleParser(); 098 if ("JSONLD".equalsIgnoreCase(format)) throw new Error("unsupported Format "+format.toString()); // return new JsonLdParser(); 099 if ("VBAR".equalsIgnoreCase(format)) throw new Error("unsupported Format "+format.toString()); // 100 if ("TEXT".equalsIgnoreCase(format)) throw new Error("unsupported Format "+format.toString()); // 101 throw new Error("unsupported Format "+format); 102 } 103 104 public static FhirFormat determineFormat(byte[] source) throws FHIRException { 105 return determineFormat(source, MAX_SCAN_LENGTH); 106 } 107 108 public static FhirFormat determineFormat(byte[] source, int scanLength) throws FHIRException { 109 if (scanLength == -1) 110 scanLength = source.length; 111 int lt = firstIndexOf(source, '<', scanLength); 112 int ps = firstIndexOf(source, '{', scanLength); 113 int at = firstIndexOf(source, '@', scanLength); 114 if (at < ps && at < lt) return FhirFormat.TURTLE; 115 if (ps < lt) return FhirFormat.JSON; 116 if (lt < ps) return FhirFormat.XML; 117 throw new FHIRException("unable to determine format"); 118 } 119 120 private static int firstIndexOf(byte[] source, char c, int scanLength) { 121 for (int i = 0; i < Math.max(source.length, scanLength); i++) { 122 if (source[i] == c) 123 return i; 124 } 125 return Integer.MAX_VALUE; 126 } 127 128 129}