001/* 002 GRANITE DATA SERVICES 003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S. 004 005 This file is part of Granite Data Services. 006 007 Granite Data Services is free software; you can redistribute it and/or modify 008 it under the terms of the GNU Library General Public License as published by 009 the Free Software Foundation; either version 2 of the License, or (at your 010 option) any later version. 011 012 Granite Data Services is distributed in the hope that it will be useful, but 013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License 015 for more details. 016 017 You should have received a copy of the GNU Library General Public License 018 along with this library; if not, see <http://www.gnu.org/licenses/>. 019*/ 020 021package org.granite.builder.util; 022 023import java.io.BufferedReader; 024import java.io.BufferedWriter; 025import java.io.File; 026import java.io.FileInputStream; 027import java.io.FileOutputStream; 028import java.io.FileReader; 029import java.io.IOException; 030import java.io.InputStreamReader; 031import java.io.OutputStreamWriter; 032import java.io.Reader; 033import java.io.StringWriter; 034 035import com.thoughtworks.xstream.XStream; 036import com.thoughtworks.xstream.converters.reflection.Sun14ReflectionProvider; 037 038/** 039 * @author Franck WOLFF 040 */ 041public class XStreamUtil { 042 043 public static final String DEFAULT_CHARSET = "UTF-8"; 044 private static final String ENCODING_ATTR = "encoding=\""; 045 046 public static <T> T load(File file, Class<T> clazz) throws IOException { 047 String charset = null; 048 049 String xmlDeclaration = null; 050 BufferedReader reader = null; 051 try { 052 reader = new BufferedReader(new FileReader(file)); 053 xmlDeclaration = reader.readLine(); 054 } finally { 055 if (reader != null) 056 reader.close(); 057 } 058 059 if (xmlDeclaration != null && xmlDeclaration.startsWith("<?xml") && xmlDeclaration.endsWith("?>")) { 060 int iEncoding = xmlDeclaration.indexOf(ENCODING_ATTR); 061 if (iEncoding != -1) { 062 int iEndEncoding = xmlDeclaration.indexOf('"', iEncoding + ENCODING_ATTR.length()); 063 if (iEndEncoding != -1) 064 charset = xmlDeclaration.substring(iEncoding + ENCODING_ATTR.length(), iEndEncoding); 065 } 066 } 067 068 return load(file, clazz, charset); 069 } 070 071 @SuppressWarnings("unchecked") 072 public static <T> T load(File file, Class<T> clazz, String charset) throws IOException { 073 Reader reader = null; 074 try { 075 if (charset != null) 076 reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset)); 077 else 078 reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); 079 080 XStream xs = new XStream(new Sun14ReflectionProvider()); 081 xs.processAnnotations(clazz); 082 return (T)xs.fromXML(reader); 083 } finally { 084 if (reader != null) 085 reader.close(); 086 } 087 } 088 089 public static void save(File file, Object o) throws IOException { 090 save(file, o, null); 091 } 092 093 public static void save(File file, Object o, String charset) throws IOException { 094 if (charset == null) 095 charset = DEFAULT_CHARSET; 096 097 BufferedWriter writer = null; 098 try { 099 writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)); 100 writer.write("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>"); 101 writer.newLine(); 102 103 XStream xs = new XStream(new Sun14ReflectionProvider()); 104 xs.processAnnotations(o.getClass()); 105 xs.toXML(o, writer); 106 } finally { 107 if (writer != null) 108 writer.close(); 109 } 110 } 111 112 public static String toString(Object o) { 113 StringWriter writer = new StringWriter(); 114 XStream xs = new XStream(new Sun14ReflectionProvider()); 115 xs.processAnnotations(o.getClass()); 116 xs.toXML(o, writer); 117 return writer.toString(); 118 } 119}