1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 package com.sun.star.script.framework.container; 28 import com.sun.star.script.framework.log.*; 29 import com.sun.star.script.framework.io.*; 30 import com.sun.star.script.framework.provider.PathUtils; 31 32 import com.sun.star.container.*; 33 import com.sun.star.uno.Type; 34 import com.sun.star.uno.UnoRuntime; 35 36 import com.sun.star.ucb.XSimpleFileAccess; 37 import com.sun.star.ucb.XSimpleFileAccess2; 38 import java.io.*; 39 40 public class Parcel implements XNameContainer 41 { 42 protected ParcelDescriptor m_descriptor; 43 protected String name; 44 protected ParcelContainer parent; 45 protected XSimpleFileAccess m_xSFA; 46 public Parcel( XSimpleFileAccess xSFA, ParcelContainer parent, ParcelDescriptor desc, String parcelName ) 47 { 48 this( parent, desc, parcelName ); 49 this.m_xSFA = xSFA; 50 } 51 52 public Parcel( ParcelContainer parent, ParcelDescriptor desc, String parcelName ) 53 { 54 this.parent = parent; 55 this.m_descriptor = desc; 56 this.name = parcelName; 57 } 58 59 /** 60 * Tests if this <tt>Parcel</tt> is in an UNO package 61 * or within a sub package within an UNO package 62 * 63 * @return <tt>true</tt> if has parent <tt>false</tt> otherwise 64 */ 65 public boolean isUnoPkg() { return parent.isUnoPkg(); } 66 67 public String getName() 68 { 69 return name; 70 } 71 public java.lang.Object getByName( String aName ) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException 72 { 73 LogUtils.DEBUG("** Parcel.getByName for " + aName ); 74 ScriptEntry script = null; 75 try 76 { 77 if ( m_descriptor != null && hasElements() ) 78 { 79 ScriptEntry[] scripts = m_descriptor.getScriptEntries(); 80 if ( scripts.length != 0 ) 81 { 82 for ( int index = 0; index < scripts.length; index++ ) 83 { 84 if ( scripts[ index ].getLanguageName().equals( aName ) ) 85 { 86 script = scripts[ index ]; 87 break; 88 } 89 } 90 } 91 } 92 } 93 // catch unknown or un-checked exceptions 94 catch ( Exception e ) 95 { 96 throw new com.sun.star.lang.WrappedTargetException( e.toString() ); 97 } 98 if ( script == null ) 99 { 100 LogUtils.DEBUG("No script for " + aName ); 101 throw new com.sun.star.container.NoSuchElementException("No script named " + aName ); 102 } 103 ScriptMetaData data = new ScriptMetaData( this, script, null ); 104 105 LogUtils.DEBUG("returning date for " + aName ); 106 return data; 107 } 108 public String[] getElementNames() 109 { 110 String[] results = new String[0]; 111 if ( m_descriptor != null ) 112 { 113 ScriptEntry[] scripts = m_descriptor.getScriptEntries(); 114 results = new String[ scripts.length ]; 115 for ( int index = 0; index < scripts.length; index++ ) 116 { 117 results[ index ] = scripts[ index ].getLanguageName(); 118 } 119 } 120 return results; 121 } 122 public boolean hasByName( String aName ) 123 { 124 125 boolean result = true; 126 Object containee = null; 127 try 128 { 129 containee = getByName( aName ); 130 if ( containee != null ) 131 { 132 result = true; 133 } 134 } 135 catch( Exception e ) 136 { 137 result = false; 138 } 139 return result; 140 } 141 142 public com.sun.star.uno.Type getElementType() { 143 // TODO at the moment this returns void indicating 144 // type is unknown ( from UNO point of view this is correct ) 145 // but, maybe we want to have a private UNO interface 146 // 147 return new Type(); 148 } 149 150 public boolean hasElements() 151 { 152 if ( m_descriptor != null && m_descriptor.getScriptEntries().length > 0 ) 153 { 154 return true; 155 } 156 return false; 157 } 158 159 public void replaceByName( String aName, java.lang.Object aElement ) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException 160 { 161 // TODO check type of aElement 162 // if not ok, throw IllegalArgument 163 if ( m_descriptor != null ) 164 { 165 try 166 { 167 ScriptEntry script = (ScriptEntry)getByName( aName ); 168 if ( script != null ) 169 { 170 //m_descriptor.removeScriptEntry( script ); 171 // TODO needs to create source file ( if there is one ) 172 //m_descriptor.write(); 173 } 174 else 175 { 176 throw new com.sun.star.container.NoSuchElementException("No script named " + aName ); 177 } 178 179 180 } 181 // TO DO should catch specified exceptions 182 catch ( Exception e ) 183 { 184 throw new com.sun.star.lang.WrappedTargetException(); 185 } 186 187 } 188 } 189 190 // create 191 public void insertByName( String aName, java.lang.Object aElement ) throws com.sun.star.lang.IllegalArgumentException, ElementExistException, com.sun.star.lang.WrappedTargetException 192 { 193 // TODO check the type of aElement and throw# 194 // if not appropriate 195 try 196 { 197 if ( hasByName( aName ) ) 198 { 199 throw new ElementExistException( aName ); 200 } 201 ScriptMetaData script = (ScriptMetaData)aElement; 202 203 if ( script.hasSource() ) 204 { 205 LogUtils.DEBUG("Inserting source: " + script.getSource()); 206 if ( !script.writeSourceFile() ) 207 { 208 throw new com.sun.star.lang.WrappedTargetException( "Failed to create source file " + script.getLanguageName() ); 209 } 210 } 211 m_descriptor.addScriptEntry( script ); 212 writeParcelDescriptor(); 213 } 214 catch ( Exception e ) 215 { 216 LogUtils.DEBUG("Failed to insert entry " + aName + ": " + e.getMessage()); 217 throw new com.sun.star.lang.WrappedTargetException( e.toString() ); 218 } 219 } 220 221 222 private void writeParcelDescriptor() 223 throws com.sun.star.ucb.CommandAbortedException, 224 com.sun.star.io.IOException, 225 com.sun.star.uno.Exception, java.io.IOException 226 { 227 String pathToDescriptor = PathUtils.make_url( 228 getPathToParcel(), ParcelDescriptor.PARCEL_DESCRIPTOR_NAME ); 229 230 XSimpleFileAccess2 xSFA2 = ( XSimpleFileAccess2 ) 231 UnoRuntime.queryInterface( XSimpleFileAccess2.class, m_xSFA ); 232 233 if ( xSFA2 != null ) 234 { 235 ByteArrayOutputStream bos = null; 236 ByteArrayInputStream bis = null; 237 XInputStreamImpl xis = null; 238 try 239 { 240 bos = new ByteArrayOutputStream( 1024 ); 241 m_descriptor.write( bos ); 242 bis = new ByteArrayInputStream( bos.toByteArray() ); 243 244 xis = new XInputStreamImpl( bis ); 245 xSFA2.writeFile( pathToDescriptor, xis ); 246 } 247 finally 248 { 249 if (bos != null) bos.close(); 250 if (bis != null) bis.close(); 251 if (xis != null) xis.closeInput(); 252 } 253 } 254 } 255 256 // delete 257 public void removeByName( String Name ) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException 258 { 259 try 260 { 261 ScriptMetaData script = null; 262 if ( ( script = (ScriptMetaData)getByName( Name ) ) != null ) 263 { 264 // if ( script.hasSource() ) 265 { 266 if ( !script.removeSourceFile() ) 267 { 268 LogUtils.DEBUG("** Parcel.removeByName Failed to remove script " + Name ); 269 throw new com.sun.star.lang.WrappedTargetException("Failed to remove script " + Name ); 270 } 271 LogUtils.DEBUG("** Parcel.removeByName have removed script source file " + Name ); 272 } 273 m_descriptor.removeScriptEntry( script ); 274 writeParcelDescriptor(); 275 276 } 277 else 278 { 279 throw new com.sun.star.container.NoSuchElementException( "No script named " + Name ); 280 } 281 282 } 283 catch ( Exception e ) 284 { 285 LogUtils.DEBUG("** Parcel.removeByName Exception: " + e ); 286 throw new com.sun.star.lang.WrappedTargetException( e.toString() ); 287 } 288 289 } 290 // rename parcel 291 public void rename( String name ) throws com.sun.star.lang.WrappedTargetException 292 { 293 this.name = name; 294 } 295 public ParcelContainer getParent() { return parent; } 296 /** 297 * Returns the path of this <tt>Parcel</tt> 298 * 299 * @return <tt>String</tt> path to parcel 300 */ 301 public String getPathToParcel() 302 { 303 String path = parent.getParcelContainerDir() + "/" + name; 304 return path; 305 } 306 307 } 308 309