001 /*----------------------------------------------------------------------- 002 * Copyright (C) 2005 003 * Associated Universities, Inc. Washington DC, USA. 004 * This program is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU General Public License as 006 * published by the Free Software Foundation; either version 2 of 007 * the License, or (at your option) any later version. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU General Public License for more details. 013 * 014 * Correspondence concerning this software should be addressed as follows: 015 * Internet email: switz@nrao.edu 016 * Postal address: User Database 017 * National Radio Astronomy Observatory 018 * Post Office Box 0 019 * Socorro, NM 87801 USA 020 *-----------------------------------------------------------------------*/ 021 package edu.nrao.sss.xml; 022 023 import java.text.SimpleDateFormat; 024 import java.util.Date; 025 026 /** 027 * Class composed of static methods that encode or decode the five meta XML 028 * characters. 029 * 030 * @author stephan 031 */ 032 public class XMLTransform { 033 034 /** 035 * Encode a String, that is, transform it so the five XML meta characters 036 * are escaped properly. 037 * 038 * @param in 039 * the String to encode 040 * @return the resulting encoded String, null if the input String was 041 */ 042 public static String encode(String in) { 043 if (in == null) 044 return null; 045 String out = in; 046 out = out.replaceAll("&", "&"); 047 out = out.replaceAll("<", "<"); 048 out = out.replaceAll(">", ">"); 049 out = out.replaceAll("'", "'"); 050 out = out.replaceAll("\"", """); 051 return out; 052 } 053 054 /** 055 * 056 * Decode a String, that is, unescape any occurence of one of the five 057 * encoded meta characters. 058 * 059 * @param in 060 * the String to decode 061 * @return the resulting decoded String, null if the input String was null 062 */ 063 public static String decode(String in) { 064 if (in == null) 065 return null; 066 String out = in; 067 out = out.replaceAll("&", "&"); 068 out = out.replaceAll("<", "<"); 069 out = out.replaceAll(">", ">"); 070 out = out.replaceAll("'", "'"); 071 out = out.replaceAll(""", "\""); 072 return out; 073 } 074 075 /** 076 * Format a date as a standard XML DateTime. 077 * 078 * @param date the date to be formatted. 079 * @return date formatted as a standard XML DateTime. 080 */ 081 public static String dateToXMLDateTime(Date date) { 082 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH':'mm':'ss"); 083 return sdf.format(date); 084 } 085 } 086