001package org.hl7.fhir.dstu3.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; 036 037public abstract class FormatUtilities { 038 public static final String ID_REGEX = "[A-Za-z0-9\\-\\.]{1,64}"; 039 public static final String FHIR_NS = "http://hl7.org/fhir"; 040 public static final String XHTML_NS = "http://www.w3.org/1999/xhtml"; 041 public static final String NS_XSI = "http://www.w3.org/2001/XMLSchema-instance"; 042 043 protected String toString(String value) { 044 return value; 045 } 046 047 protected String toString(int value) { 048 return java.lang.Integer.toString(value); 049 } 050 051 protected String toString(boolean value) { 052 return java.lang.Boolean.toString(value); 053 } 054 055 protected String toString(BigDecimal value) { 056 return value.toString(); 057 } 058 059 protected String toString(URI value) { 060 return value.toString(); 061 } 062 063 public static String toString(byte[] value) { 064 byte[] encodeBase64 = Base64.encodeBase64(value); 065 return new String(encodeBase64); 066 } 067 068 public static boolean isValidId(String tail) { 069 return tail.matches(ID_REGEX); 070 } 071 072 public static String makeId(String candidate) { 073 StringBuilder b = new StringBuilder(); 074 for (char c : candidate.toCharArray()) 075 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.' || c == '-') 076 b.append(c); 077 return b.toString(); 078 } 079 080 081 082 083}