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