001 /* 002 Copyright (C) 2003-2004 Laurent Martelli <laurent@aopsys.com> 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 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 Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 017 018 package org.objectweb.jac.aspects.export; 019 020 import java.io.File; 021 import java.io.FileOutputStream; 022 import java.io.FileWriter; 023 import java.io.IOException; 024 import java.io.OutputStream; 025 import java.io.Writer; 026 import java.util.HashSet; 027 import org.objectweb.jac.core.AspectComponent; 028 import org.objectweb.jac.core.NameRepository; 029 030 public class ExportAC extends AspectComponent implements ExportConf { 031 HashSet roots = new HashSet(); 032 033 NameRepository nr; 034 public ExportAC() { 035 nr = (NameRepository)NameRepository.get(); 036 } 037 038 /** 039 * Declare some objects as roots to start exporting from. 040 * @param nameExpr a regular expression matching the name of root objects 041 */ 042 public void addRoot(String nameExpr) { 043 roots.add(nameExpr); 044 } 045 046 HashSet allow = new HashSet(); 047 public void allowExport(String classExpr) { 048 allow.add(classExpr); 049 } 050 051 HashSet deny = new HashSet(); 052 public void denyExport(String classExpr) { 053 deny.add(classExpr); 054 } 055 056 public void setDenyPolicy() { 057 } 058 public void setAllowPolicy() { 059 } 060 061 public static final String DEFAULT_ENCODING = "UTF-8"; 062 063 /** 064 * Exports all objects to a stream 065 * 066 * @param out stream to export to 067 * @param encoding charset encoding to use (UTF-8 ,iso-8859-1,...) 068 * 069 * @see #export(OutputStream) 070 * @see #export(File) 071 * @see #export(File,String) 072 */ 073 public void export(OutputStream out, String encoding) throws IOException { 074 Exporter exporter = new Exporter(roots,allow,deny); 075 exporter.export(out, encoding); 076 } 077 078 /** 079 * Exports all objects to a stream with the default charset 080 * encoding, which is UTF-8. 081 * 082 * @param out stream to export to 083 * 084 * @see #export(OutputStream,String) 085 * @see #export(File) 086 * @see #export(File,String) 087 */ 088 public void export(OutputStream out) throws IOException { 089 export(out,DEFAULT_ENCODING); 090 } 091 092 /** 093 * Exports all objects to a file with the default charset 094 * encoding, which is UTF-8. 095 * 096 * @param file file to export to 097 * 098 * @see #export(File,String) 099 * @see #export(OutputStream) 100 * @see #export(OutputStream,String) 101 */ 102 public void export(File file) throws IOException { 103 export(file,DEFAULT_ENCODING); 104 } 105 106 107 /** 108 * Exports all objects to a file with the default charset 109 * encoding, which is UTF-8. 110 * 111 * @param file file to export to 112 * @param encoding charset encoding to use (UTF-8 ,iso-8859-1,...) 113 * 114 * @see #export(File) 115 * @see #export(OutputStream) 116 * @see #export(OutputStream,String) 117 */ 118 public void export(File file, String encoding) throws IOException { 119 FileOutputStream writer = new FileOutputStream(file); 120 try { 121 export(writer,encoding); 122 } finally { 123 writer.close(); 124 } 125 } 126 }