001package org.hl7.fhir.instance.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 042 protected String toString(String value) { 043 return value; 044 } 045 046 protected String toString(int value) { 047 return java.lang.Integer.toString(value); 048 } 049 050 protected String toString(boolean value) { 051 return java.lang.Boolean.toString(value); 052 } 053 054 protected String toString(BigDecimal value) { 055 return value.toString(); 056 } 057 058 protected String toString(URI value) { 059 return value.toString(); 060 } 061 062 public static String toString(byte[] value) { 063 byte[] encodeBase64 = Base64.encodeBase64(value); 064 return new String(encodeBase64); 065 } 066 067 public static boolean isValidId(String tail) { 068 return tail.matches(ID_REGEX); 069 } 070 071 public static String makeId(String candidate) { 072 StringBuilder b = new StringBuilder(); 073 for (char c : candidate.toCharArray()) 074 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.' || c == '-') 075 b.append(c); 076 return b.toString(); 077 } 078 079 080 081 082}