1cd519653SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3cd519653SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4cd519653SAndrew Rist * or more contributor license agreements. See the NOTICE file
5cd519653SAndrew Rist * distributed with this work for additional information
6cd519653SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7cd519653SAndrew Rist * to you under the Apache License, Version 2.0 (the
8cd519653SAndrew Rist * "License"); you may not use this file except in compliance
9cd519653SAndrew Rist * with the License. You may obtain a copy of the License at
10cd519653SAndrew Rist *
11cd519653SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cd519653SAndrew Rist *
13cd519653SAndrew Rist * Unless required by applicable law or agreed to in writing,
14cd519653SAndrew Rist * software distributed under the License is distributed on an
15cd519653SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16cd519653SAndrew Rist * KIND, either express or implied. See the License for the
17cd519653SAndrew Rist * specific language governing permissions and limitations
18cd519653SAndrew Rist * under the License.
19cd519653SAndrew Rist *
20cd519653SAndrew Rist *************************************************************/
21cd519653SAndrew Rist
22cd519653SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir package com.sun.star.script.framework.container;
25cdf0e10cSrcweir
26cdf0e10cSrcweir import com.sun.star.script.framework.log.*;
27cdf0e10cSrcweir import com.sun.star.script.framework.io.*;
28cdf0e10cSrcweir import com.sun.star.script.framework.provider.PathUtils;
29cdf0e10cSrcweir
30cdf0e10cSrcweir import com.sun.star.container.*;
31cdf0e10cSrcweir import com.sun.star.uno.Type;
32cdf0e10cSrcweir import com.sun.star.lang.*;
33cdf0e10cSrcweir import com.sun.star.io.*;
34cdf0e10cSrcweir import java.io.*;
35cdf0e10cSrcweir import java.util.*;
36cdf0e10cSrcweir import com.sun.star.ucb.XSimpleFileAccess;
37cdf0e10cSrcweir import com.sun.star.ucb.XSimpleFileAccess2;
38cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
39cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
40cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
41cdf0e10cSrcweir
42cdf0e10cSrcweir import com.sun.star.uri.XUriReference;
43cdf0e10cSrcweir import com.sun.star.uri.XUriReferenceFactory;
44cdf0e10cSrcweir import com.sun.star.uri.XVndSunStarScriptUrl;
45cdf0e10cSrcweir
46cdf0e10cSrcweir /**
47cdf0e10cSrcweir * The <code>ParcelContainer</code> object is used to store the
48cdf0e10cSrcweir * ScripingFramework specific Libraries.
49cdf0e10cSrcweir *
50cdf0e10cSrcweir * @author
51cdf0e10cSrcweir * @created
52cdf0e10cSrcweir */
53cdf0e10cSrcweir
54cdf0e10cSrcweir public class ParcelContainer implements XNameAccess
55cdf0e10cSrcweir {
56cdf0e10cSrcweir protected String language;
57cdf0e10cSrcweir protected String containerUrl;
58cdf0e10cSrcweir protected Collection parcels = new ArrayList(10);
59cdf0e10cSrcweir static protected XSimpleFileAccess m_xSFA;
60cdf0e10cSrcweir protected XComponentContext m_xCtx;
61cdf0e10cSrcweir private ParcelContainer parent = null;
62cdf0e10cSrcweir private Collection childContainers = new ArrayList(10);;
63cdf0e10cSrcweir private boolean isPkgContainer = false;
64cdf0e10cSrcweir
65cdf0e10cSrcweir /**
66cdf0e10cSrcweir * Tests if this <tt>ParcelContainer</tt> represents an UNO package
67cdf0e10cSrcweir * or sub package within an UNO package
68cdf0e10cSrcweir *
69cdf0e10cSrcweir * @return <tt>true</tt> if has parent <tt>false</tt> otherwise
70cdf0e10cSrcweir */
isUnoPkg()71cdf0e10cSrcweir public boolean isUnoPkg() { return isPkgContainer; }
72cdf0e10cSrcweir /**
73cdf0e10cSrcweir * Tests if this <tt>ParcelContainer</tt> has a parent
74cdf0e10cSrcweir * a parent.
75cdf0e10cSrcweir *
76cdf0e10cSrcweir * @return <tt>true</tt> if has parent <tt>false</tt> otherwise
77cdf0e10cSrcweir */
hasParent()78cdf0e10cSrcweir public boolean hasParent()
79cdf0e10cSrcweir {
80cdf0e10cSrcweir if ( parent == null )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir return false;
83cdf0e10cSrcweir }
84cdf0e10cSrcweir return true;
85cdf0e10cSrcweir }
86cdf0e10cSrcweir
87cdf0e10cSrcweir /**
88cdf0e10cSrcweir * Returns this <tt>ParcelContainer</tt>'s parent
89cdf0e10cSrcweir *
90cdf0e10cSrcweir * @return <tt>ParcelContainer</tt> if has parent null otherwise
91cdf0e10cSrcweir */
parent()92cdf0e10cSrcweir public ParcelContainer parent()
93cdf0e10cSrcweir {
94cdf0e10cSrcweir return parent;
95cdf0e10cSrcweir }
96cdf0e10cSrcweir
97cdf0e10cSrcweir /**
98cdf0e10cSrcweir * Returns all child <tt>ParcelContainer</tt>
99cdf0e10cSrcweir * this instance of <tt>ParcelContainer</tt>
100cdf0e10cSrcweir *
101cdf0e10cSrcweir * @return a new array of ParcelContainers. A zero
102cdf0e10cSrcweir * length array is returned if no child ParcelContainers.
103cdf0e10cSrcweir */
getChildContainers()104cdf0e10cSrcweir public ParcelContainer[] getChildContainers()
105cdf0e10cSrcweir {
106cdf0e10cSrcweir if ( childContainers.isEmpty() )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir return new ParcelContainer[0];
109cdf0e10cSrcweir }
110cdf0e10cSrcweir return (ParcelContainer[]) childContainers.toArray( new ParcelContainer[0] );
111cdf0e10cSrcweir
112cdf0e10cSrcweir }
113cdf0e10cSrcweir /**
114cdf0e10cSrcweir * Removes a child <tt>ParcelContainer</tt>
115cdf0e10cSrcweir * from this instance.
116cdf0e10cSrcweir * @param child <tt>ParcelContainer</tt> to be added.
117cdf0e10cSrcweir *
118cdf0e10cSrcweir * @return <tt>true</tt> if child successfully removed
119cdf0e10cSrcweir */
removeChildContainer( ParcelContainer child )120cdf0e10cSrcweir public boolean removeChildContainer( ParcelContainer child )
121cdf0e10cSrcweir {
122cdf0e10cSrcweir return childContainers.remove( child );
123cdf0e10cSrcweir }
124cdf0e10cSrcweir
125cdf0e10cSrcweir /**
126cdf0e10cSrcweir * Adds a new child <tt>ParcelContainer</tt>
127cdf0e10cSrcweir * to this instance.
128cdf0e10cSrcweir * @param child <tt>ParcelContainer</tt> to be added.
129cdf0e10cSrcweir *
130cdf0e10cSrcweir */
addChildContainer( ParcelContainer child )131cdf0e10cSrcweir public void addChildContainer( ParcelContainer child )
132cdf0e10cSrcweir {
133cdf0e10cSrcweir childContainers.add( child );
134cdf0e10cSrcweir }
135cdf0e10cSrcweir
136cdf0e10cSrcweir /**
137cdf0e10cSrcweir * Returns a child <tt>ParcelContainer</tt> whose location
138cdf0e10cSrcweir * matches the <tt>location</tt> argument passed to this method.
139cdf0e10cSrcweir * @param key the <tt>location</tt> which is to
140cdf0e10cSrcweir * be matched.
141cdf0e10cSrcweir *
142cdf0e10cSrcweir * @return child <tt>ParcelContainer</tt> or <null> if none
143cdf0e10cSrcweir * found.
144cdf0e10cSrcweir */
145cdf0e10cSrcweir
getChildContainer( String key )146cdf0e10cSrcweir public ParcelContainer getChildContainer( String key )
147cdf0e10cSrcweir {
148cdf0e10cSrcweir ParcelContainer result = null;
149cdf0e10cSrcweir Iterator iter = childContainers.iterator();
150cdf0e10cSrcweir while ( iter.hasNext() )
151cdf0e10cSrcweir {
152cdf0e10cSrcweir ParcelContainer c = (ParcelContainer) iter.next();
153cdf0e10cSrcweir String location = ScriptMetaData.getLocationPlaceHolder(
154cdf0e10cSrcweir c.containerUrl, c.getName());
155cdf0e10cSrcweir
156cdf0e10cSrcweir if ( key.equals( location ) )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir result = c;
159cdf0e10cSrcweir break;
160cdf0e10cSrcweir }
161cdf0e10cSrcweir }
162cdf0e10cSrcweir return result;
163cdf0e10cSrcweir }
164cdf0e10cSrcweir
165cdf0e10cSrcweir /**
166cdf0e10cSrcweir * Returns a child <tt>ParcelContainer</tt> whose member
167cdf0e10cSrcweir * <tt>containerUrl</tt> matches the <tt>containerUrl</tt>
168cdf0e10cSrcweir * argument passed to this method.
169cdf0e10cSrcweir * @param containerUrl the <tt>containerUrl</tt> which is to
170cdf0e10cSrcweir * be matched.
171cdf0e10cSrcweir *
172cdf0e10cSrcweir * @return child <tt>ParcelContainer</tt> or <null> if none
173cdf0e10cSrcweir * found.
174cdf0e10cSrcweir */
175cdf0e10cSrcweir
getChildContainerForURL( String containerUrl )176cdf0e10cSrcweir public ParcelContainer getChildContainerForURL( String containerUrl )
177cdf0e10cSrcweir {
178cdf0e10cSrcweir ParcelContainer result = null;
179cdf0e10cSrcweir Iterator iter = childContainers.iterator();
180cdf0e10cSrcweir while ( iter.hasNext() )
181cdf0e10cSrcweir {
182cdf0e10cSrcweir ParcelContainer c = (ParcelContainer) iter.next();
183cdf0e10cSrcweir if ( containerUrl.equals( c.containerUrl ) )
184cdf0e10cSrcweir {
185cdf0e10cSrcweir result = c;
186cdf0e10cSrcweir break;
187cdf0e10cSrcweir }
188cdf0e10cSrcweir }
189cdf0e10cSrcweir return result;
190cdf0e10cSrcweir }
191cdf0e10cSrcweir
192cdf0e10cSrcweir /**
193cdf0e10cSrcweir * Returns Name of this container. Name of this <tt>ParcelContainer</tt>
194cdf0e10cSrcweir * is determined from the <tt>containerUrl</tt> as the last portion
195cdf0e10cSrcweir * of the URL after the last forward slash.
196cdf0e10cSrcweir * @return name of <tt>ParcelContainer</tt>
197cdf0e10cSrcweir * found.
198cdf0e10cSrcweir */
getName()199cdf0e10cSrcweir public String getName()
200cdf0e10cSrcweir {
201cdf0e10cSrcweir String name = null;
202cdf0e10cSrcweir // TODO handler package ParcelContainer?
203cdf0e10cSrcweir if ( !containerUrl.startsWith( "vnd.sun.star.tdoc:" ) )
204cdf0e10cSrcweir {
205cdf0e10cSrcweir // return name
206cdf0e10cSrcweir String decodedUrl = java.net.URLDecoder.decode( containerUrl );
207cdf0e10cSrcweir int indexOfSlash = decodedUrl.lastIndexOf( "/" );
208cdf0e10cSrcweir if ( indexOfSlash != -1 )
209cdf0e10cSrcweir {
210cdf0e10cSrcweir name = decodedUrl.substring( indexOfSlash + 1 );
211cdf0e10cSrcweir }
212cdf0e10cSrcweir }
213cdf0e10cSrcweir else
214cdf0e10cSrcweir {
215cdf0e10cSrcweir name = "document";
216cdf0e10cSrcweir }
217cdf0e10cSrcweir return name;
218cdf0e10cSrcweir }
219cdf0e10cSrcweir
220cdf0e10cSrcweir /**
221cdf0e10cSrcweir * Initializes a newly created <code>ParcelContainer</code> object.
222cdf0e10cSrcweir * @param xCtx UNO component context
223cdf0e10cSrcweir * @param containerUrl location of this container.
224cdf0e10cSrcweir * @param language language for which entries are stored
225cdf0e10cSrcweir * @return name of <tt>ParcelContainer</tt>
226cdf0e10cSrcweir * @throws IllegalArgumentException
227cdf0e10cSrcweir * @throws WrappedTargetException
228cdf0e10cSrcweir */
229cdf0e10cSrcweir
ParcelContainer( XComponentContext xCtx, String containerUrl, String language )230cdf0e10cSrcweir public ParcelContainer( XComponentContext xCtx, String containerUrl, String language ) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.lang.WrappedTargetException
231cdf0e10cSrcweir
232cdf0e10cSrcweir {
233cdf0e10cSrcweir this( null, xCtx, containerUrl, language, true );
234cdf0e10cSrcweir }
235cdf0e10cSrcweir
236cdf0e10cSrcweir /**
237cdf0e10cSrcweir * Initializes a newly created <code>ParcelContainer</code> object.
238cdf0e10cSrcweir * @param xCtx UNO component context
239cdf0e10cSrcweir * @param containerUrl location of this container.
240cdf0e10cSrcweir * @param language language for which entries are stored
241cdf0e10cSrcweir * @param loadParcels set to <tt>true</tt> if parcels are to be loaded
242cdf0e10cSrcweir * on construction.
243*e6b649b5SPedro Giffuni * @throws com.sun.star.lang.IllegalArgumentException
244cdf0e10cSrcweir * @throws WrappedTargetException
245cdf0e10cSrcweir */
ParcelContainer( XComponentContext xCtx, String containerUrl, String language, boolean loadParcels )246cdf0e10cSrcweir public ParcelContainer( XComponentContext xCtx, String containerUrl, String language, boolean loadParcels ) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.lang.WrappedTargetException
247cdf0e10cSrcweir {
248cdf0e10cSrcweir this( null, xCtx, containerUrl, language, loadParcels );
249cdf0e10cSrcweir }
250cdf0e10cSrcweir
251cdf0e10cSrcweir /**
252cdf0e10cSrcweir * Initializes a newly created <code>ParcelContainer</code> object.
253cdf0e10cSrcweir * @param parent parent ParcelContainer
254cdf0e10cSrcweir * @param xCtx UNO component context
255cdf0e10cSrcweir * @param containerUrl location of this container.
256cdf0e10cSrcweir * @param language language for which entries are stored
257cdf0e10cSrcweir * @param loadParcels set to <tt>true</tt> if parcels are to be loaded
258cdf0e10cSrcweir * on construction.
259cdf0e10cSrcweir * @return name of <tt>ParcelContainer</tt>
260cdf0e10cSrcweir * @throws IllegalArgumentException
261cdf0e10cSrcweir */
262cdf0e10cSrcweir
ParcelContainer( ParcelContainer parent, XComponentContext xCtx, String containerUrl, String language, boolean loadParcels )263cdf0e10cSrcweir public ParcelContainer( ParcelContainer parent, XComponentContext xCtx, String containerUrl, String language, boolean loadParcels ) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.lang.WrappedTargetException
264cdf0e10cSrcweir
265cdf0e10cSrcweir {
266cdf0e10cSrcweir LogUtils.DEBUG("Creating ParcelContainer for " + containerUrl + " loadParcels = " + loadParcels + " language = " + language );
267cdf0e10cSrcweir this.m_xCtx = xCtx;
268cdf0e10cSrcweir this.language = language;
269cdf0e10cSrcweir this.parent = parent;
270cdf0e10cSrcweir this.containerUrl = containerUrl;
271cdf0e10cSrcweir
272cdf0e10cSrcweir initSimpleFileAccess();
273cdf0e10cSrcweir boolean parentIsPkgContainer = false;
274cdf0e10cSrcweir
275cdf0e10cSrcweir if ( parent != null )
276cdf0e10cSrcweir {
277cdf0e10cSrcweir parentIsPkgContainer = parent.isUnoPkg();
278cdf0e10cSrcweir }
279cdf0e10cSrcweir
280cdf0e10cSrcweir if ( containerUrl.endsWith("uno_packages") || parentIsPkgContainer )
281cdf0e10cSrcweir {
282cdf0e10cSrcweir isPkgContainer = true;
283cdf0e10cSrcweir }
284cdf0e10cSrcweir
285cdf0e10cSrcweir if ( loadParcels )
286cdf0e10cSrcweir {
287cdf0e10cSrcweir loadParcels();
288cdf0e10cSrcweir }
289cdf0e10cSrcweir }
290cdf0e10cSrcweir
291cdf0e10cSrcweir
getContainerURL()292cdf0e10cSrcweir public String getContainerURL() { return this.containerUrl; }
293cdf0e10cSrcweir
initSimpleFileAccess()294cdf0e10cSrcweir private synchronized void initSimpleFileAccess()
295cdf0e10cSrcweir {
296cdf0e10cSrcweir if ( m_xSFA != null )
297cdf0e10cSrcweir {
298cdf0e10cSrcweir return;
299cdf0e10cSrcweir }
300cdf0e10cSrcweir try
301cdf0e10cSrcweir {
302cdf0e10cSrcweir m_xSFA = ( XSimpleFileAccess )UnoRuntime.queryInterface(
303cdf0e10cSrcweir XSimpleFileAccess.class,
304cdf0e10cSrcweir m_xCtx.getServiceManager().createInstanceWithContext(
305cdf0e10cSrcweir "com.sun.star.ucb.SimpleFileAccess", m_xCtx ) );
306cdf0e10cSrcweir }
307cdf0e10cSrcweir catch ( Exception e )
308cdf0e10cSrcweir {
309cdf0e10cSrcweir // TODO should throw
310cdf0e10cSrcweir LogUtils.DEBUG("Error instantiating simplefile access ");
311cdf0e10cSrcweir LogUtils.DEBUG( LogUtils.getTrace( e ) );
312cdf0e10cSrcweir }
313cdf0e10cSrcweir }
314cdf0e10cSrcweir
getParcelContainerDir()315cdf0e10cSrcweir public String getParcelContainerDir()
316cdf0e10cSrcweir {
317cdf0e10cSrcweir // If this container does not represent an uno-package
318cdf0e10cSrcweir // then then it is a document, user or share
319cdf0e10cSrcweir // in each case the convention is to have a Scripts/[language]
320cdf0e10cSrcweir // dir where scripts reside
321cdf0e10cSrcweir if ( !isUnoPkg() )
322cdf0e10cSrcweir {
323cdf0e10cSrcweir return PathUtils.make_url( containerUrl , "Scripts/" + language.toLowerCase() );
324cdf0e10cSrcweir }
325cdf0e10cSrcweir return containerUrl;
326cdf0e10cSrcweir }
getByName( String aName )327cdf0e10cSrcweir public Object getByName( String aName ) throws com.sun.star.container.NoSuchElementException, WrappedTargetException
328cdf0e10cSrcweir {
329cdf0e10cSrcweir Parcel parcel = null;
330cdf0e10cSrcweir try
331cdf0e10cSrcweir {
332cdf0e10cSrcweir if ( hasElements() )
333cdf0e10cSrcweir {
334cdf0e10cSrcweir Iterator iter = parcels.iterator();
335cdf0e10cSrcweir while ( iter.hasNext() )
336cdf0e10cSrcweir {
337cdf0e10cSrcweir Parcel parcelToCheck = (Parcel)iter.next();
338cdf0e10cSrcweir
339cdf0e10cSrcweir if ( parcelToCheck.getName().equals( aName ) )
340cdf0e10cSrcweir {
341cdf0e10cSrcweir parcel = parcelToCheck;
342cdf0e10cSrcweir break;
343cdf0e10cSrcweir }
344cdf0e10cSrcweir }
345cdf0e10cSrcweir }
346cdf0e10cSrcweir }
347cdf0e10cSrcweir catch ( Exception e)
348cdf0e10cSrcweir {
349cdf0e10cSrcweir throw new WrappedTargetException( e.toString() );
350cdf0e10cSrcweir }
351cdf0e10cSrcweir if ( parcel == null )
352cdf0e10cSrcweir {
353cdf0e10cSrcweir throw new com.sun.star.container.NoSuchElementException( "Macro Library " + aName + " not found" );
354cdf0e10cSrcweir }
355cdf0e10cSrcweir return parcel;
356cdf0e10cSrcweir }
getElementNames()357cdf0e10cSrcweir public String[] getElementNames()
358cdf0e10cSrcweir {
359cdf0e10cSrcweir if ( hasElements() )
360cdf0e10cSrcweir {
361cdf0e10cSrcweir Parcel[] theParcels = (Parcel[])parcels.toArray( new Parcel[0] );
362cdf0e10cSrcweir String[] names = new String[ theParcels.length ];
363cdf0e10cSrcweir for ( int count = 0; count < names.length; count++ )
364cdf0e10cSrcweir {
365cdf0e10cSrcweir names[count] = theParcels[ count ].getName();
366cdf0e10cSrcweir }
367cdf0e10cSrcweir return names;
368cdf0e10cSrcweir }
369cdf0e10cSrcweir
370cdf0e10cSrcweir return new String[0];
371cdf0e10cSrcweir }
hasByName( String aName )372cdf0e10cSrcweir public boolean hasByName( String aName )
373cdf0e10cSrcweir {
374cdf0e10cSrcweir boolean isFound = false;
375cdf0e10cSrcweir try
376cdf0e10cSrcweir {
377cdf0e10cSrcweir if ( getByName( aName ) != null )
378cdf0e10cSrcweir {
379cdf0e10cSrcweir isFound = true;
380cdf0e10cSrcweir }
381cdf0e10cSrcweir
382cdf0e10cSrcweir }
383cdf0e10cSrcweir catch ( Exception e )
384cdf0e10cSrcweir {
385cdf0e10cSrcweir //TODO - handle trace
386cdf0e10cSrcweir }
387cdf0e10cSrcweir return isFound;
388cdf0e10cSrcweir }
getElementType()389cdf0e10cSrcweir public Type getElementType()
390cdf0e10cSrcweir {
391cdf0e10cSrcweir return new Type();
392cdf0e10cSrcweir }
hasElements()393cdf0e10cSrcweir public boolean hasElements()
394cdf0e10cSrcweir {
395cdf0e10cSrcweir if ( parcels == null || parcels.isEmpty() )
396cdf0e10cSrcweir {
397cdf0e10cSrcweir return false;
398cdf0e10cSrcweir }
399cdf0e10cSrcweir return true;
400cdf0e10cSrcweir }
401cdf0e10cSrcweir
loadParcels()402cdf0e10cSrcweir private void loadParcels() throws com.sun.star.lang.IllegalArgumentException, com.sun.star.lang.WrappedTargetException
403cdf0e10cSrcweir {
404cdf0e10cSrcweir try
405cdf0e10cSrcweir {
406cdf0e10cSrcweir LogUtils.DEBUG("About to load parcels from " + containerUrl );
407cdf0e10cSrcweir if ( m_xSFA.isFolder( getParcelContainerDir() ) )
408cdf0e10cSrcweir {
409cdf0e10cSrcweir LogUtils.DEBUG( getParcelContainerDir() + " is a folder " );
410cdf0e10cSrcweir String[] children = m_xSFA.getFolderContents( getParcelContainerDir(), true );
411cdf0e10cSrcweir parcels = new ArrayList(children.length);
412cdf0e10cSrcweir for ( int i = 0; i < children.length; i++)
413cdf0e10cSrcweir {
414cdf0e10cSrcweir LogUtils.DEBUG("Processing " + children[ i ] );
415cdf0e10cSrcweir try
416cdf0e10cSrcweir {
417cdf0e10cSrcweir loadParcel( children[ i ] );
418cdf0e10cSrcweir }
419cdf0e10cSrcweir catch (java.lang.Exception e)
420cdf0e10cSrcweir {
421cdf0e10cSrcweir // print an error message and move on to
422cdf0e10cSrcweir // the next parcel
423cdf0e10cSrcweir LogUtils.DEBUG("ParcelContainer.loadParcels caught " + e.getClass().getName() + " exception loading parcel " + children[i] + ": " + e.getMessage() );
424cdf0e10cSrcweir }
425cdf0e10cSrcweir }
426cdf0e10cSrcweir }
427cdf0e10cSrcweir else
428cdf0e10cSrcweir {
429cdf0e10cSrcweir LogUtils.DEBUG(" ParcelCOntainer.loadParcels " + getParcelContainerDir() + " is not a folder ");
430cdf0e10cSrcweir }
431cdf0e10cSrcweir
432cdf0e10cSrcweir }
433cdf0e10cSrcweir catch ( com.sun.star.ucb.CommandAbortedException e )
434cdf0e10cSrcweir {
435cdf0e10cSrcweir LogUtils.DEBUG("ParcelContainer.loadParcels caught exception processing folders for " + getParcelContainerDir() );
436cdf0e10cSrcweir LogUtils.DEBUG("TRACE: " + LogUtils.getTrace(e) );
437cdf0e10cSrcweir throw new com.sun.star.lang.WrappedTargetException( e.toString() );
438cdf0e10cSrcweir }
439cdf0e10cSrcweir catch ( com.sun.star.uno.Exception e )
440cdf0e10cSrcweir {
441cdf0e10cSrcweir LogUtils.DEBUG("ParcelContainer.loadParcels caught exception processing folders for " + getParcelContainerDir() );
442cdf0e10cSrcweir LogUtils.DEBUG("TRACE: " + LogUtils.getTrace(e) );
443cdf0e10cSrcweir throw new com.sun.star.lang.WrappedTargetException( e.toString() );
444cdf0e10cSrcweir }
445cdf0e10cSrcweir }
446cdf0e10cSrcweir
createParcel(String name)447cdf0e10cSrcweir public XNameContainer createParcel(String name) throws ElementExistException, com.sun.star.lang.WrappedTargetException
448cdf0e10cSrcweir {
449cdf0e10cSrcweir Parcel p = null;
450cdf0e10cSrcweir if ( hasByName( name ) )
451cdf0e10cSrcweir {
452cdf0e10cSrcweir throw new ElementExistException( "Parcel " + name + " already exists" );
453cdf0e10cSrcweir }
454cdf0e10cSrcweir String pathToParcel = PathUtils.make_url( getParcelContainerDir() , name );
455cdf0e10cSrcweir
456cdf0e10cSrcweir try
457cdf0e10cSrcweir {
458cdf0e10cSrcweir LogUtils.DEBUG("ParcelContainer.createParcel, creating folder " + pathToParcel );
459cdf0e10cSrcweir m_xSFA.createFolder( pathToParcel );
460cdf0e10cSrcweir
461cdf0e10cSrcweir LogUtils.DEBUG("ParcelContainer.createParcel, folder " + pathToParcel + " created ");
462cdf0e10cSrcweir
463cdf0e10cSrcweir ParcelDescriptor pd = new ParcelDescriptor();
464cdf0e10cSrcweir pd.setLanguage( language );
465cdf0e10cSrcweir String parcelDesc = PathUtils.make_url( pathToParcel, ParcelDescriptor.PARCEL_DESCRIPTOR_NAME );
466cdf0e10cSrcweir XSimpleFileAccess2 xSFA2 = ( XSimpleFileAccess2 )
467cdf0e10cSrcweir UnoRuntime.queryInterface( XSimpleFileAccess2.class, m_xSFA );
468cdf0e10cSrcweir if ( xSFA2 != null )
469cdf0e10cSrcweir {
470cdf0e10cSrcweir LogUtils.DEBUG("createParcel() Using XSIMPLEFILEACCESS2 " + parcelDesc );
471cdf0e10cSrcweir ByteArrayOutputStream bos = new ByteArrayOutputStream( 1024 );
472cdf0e10cSrcweir pd.write( bos );
473cdf0e10cSrcweir bos.close();
474cdf0e10cSrcweir ByteArrayInputStream bis = new ByteArrayInputStream( bos.toByteArray() );
475cdf0e10cSrcweir XInputStreamImpl xis = new XInputStreamImpl( bis );
476cdf0e10cSrcweir xSFA2.writeFile( parcelDesc, xis );
477cdf0e10cSrcweir xis.closeInput();
478cdf0e10cSrcweir p = loadParcel( pathToParcel );
479cdf0e10cSrcweir }
480cdf0e10cSrcweir }
481cdf0e10cSrcweir catch ( Exception e )
482cdf0e10cSrcweir {
483cdf0e10cSrcweir
484cdf0e10cSrcweir LogUtils.DEBUG("createParcel() Exception while attempting to create = " + name );
485cdf0e10cSrcweir throw new com.sun.star.lang.WrappedTargetException( e.toString() );
486cdf0e10cSrcweir }
487cdf0e10cSrcweir return p;
488cdf0e10cSrcweir }
489cdf0e10cSrcweir
loadParcel( String parcelUrl )490cdf0e10cSrcweir public Parcel loadParcel( String parcelUrl ) throws com.sun.star.lang.WrappedTargetException, com.sun.star.lang.IllegalArgumentException
491cdf0e10cSrcweir {
492cdf0e10cSrcweir
493cdf0e10cSrcweir String name = null;
494cdf0e10cSrcweir
495cdf0e10cSrcweir String parcelDescUrl = PathUtils.make_url( parcelUrl, ParcelDescriptor.PARCEL_DESCRIPTOR_NAME );
496cdf0e10cSrcweir Parcel parcel = null;
497cdf0e10cSrcweir
498cdf0e10cSrcweir XInputStream xis = null;
499cdf0e10cSrcweir InputStream is = null;
500cdf0e10cSrcweir
501cdf0e10cSrcweir try
502cdf0e10cSrcweir {
503cdf0e10cSrcweir if ( m_xSFA.exists( parcelDescUrl ) )
504cdf0e10cSrcweir {
505cdf0e10cSrcweir LogUtils.DEBUG("ParcelContainer.loadParcel opening " + parcelDescUrl );
506cdf0e10cSrcweir xis = m_xSFA.openFileRead( parcelDescUrl );
507cdf0e10cSrcweir is = new XInputStreamWrapper( xis );
508cdf0e10cSrcweir
509cdf0e10cSrcweir ParcelDescriptor pd = new ParcelDescriptor(is) ;
510cdf0e10cSrcweir try
511cdf0e10cSrcweir {
512cdf0e10cSrcweir is.close();
513cdf0e10cSrcweir is = null;
514cdf0e10cSrcweir }
515cdf0e10cSrcweir catch ( Exception e )
516cdf0e10cSrcweir {
517cdf0e10cSrcweir LogUtils.DEBUG("ParcelContainer.loadParcel Exception when closing stream for " + parcelDescUrl + " :" + e );
518cdf0e10cSrcweir }
519cdf0e10cSrcweir LogUtils.DEBUG("ParcelContainer.loadParcel closed " + parcelDescUrl );
520cdf0e10cSrcweir if ( !pd.getLanguage().equals( language ) )
521cdf0e10cSrcweir {
522cdf0e10cSrcweir LogUtils.DEBUG("ParcelContainer.loadParcel Language of Parcel does not match this container ");
523cdf0e10cSrcweir return null;
524cdf0e10cSrcweir }
525cdf0e10cSrcweir LogUtils.DEBUG("Processing " + parcelDescUrl + " closed " );
526cdf0e10cSrcweir
527cdf0e10cSrcweir int indexOfSlash = parcelUrl.lastIndexOf("/");
528cdf0e10cSrcweir name = parcelUrl.substring( indexOfSlash + 1 );
529cdf0e10cSrcweir
530cdf0e10cSrcweir parcel = new Parcel( m_xSFA, this, pd, name );
531cdf0e10cSrcweir
532cdf0e10cSrcweir LogUtils.DEBUG(" ParcelContainer.loadParcel created parcel for " + parcelDescUrl + " for language " + language );
533cdf0e10cSrcweir parcels.add( parcel );
534cdf0e10cSrcweir }
535cdf0e10cSrcweir else
536cdf0e10cSrcweir {
537cdf0e10cSrcweir throw new java.io.IOException( parcelDescUrl + " does NOT exist!");
538cdf0e10cSrcweir }
539cdf0e10cSrcweir }
540cdf0e10cSrcweir catch ( com.sun.star.ucb.CommandAbortedException e )
541cdf0e10cSrcweir {
542cdf0e10cSrcweir
543cdf0e10cSrcweir LogUtils.DEBUG("loadParcel() Exception while accessing filesystem url = " + parcelDescUrl + e );
544cdf0e10cSrcweir throw new com.sun.star.lang.WrappedTargetException( e.toString() );
545cdf0e10cSrcweir }
546cdf0e10cSrcweir catch ( java.io.IOException e )
547cdf0e10cSrcweir {
548cdf0e10cSrcweir LogUtils.DEBUG("ParcelContainer.loadParcel() caught IOException while accessing " + parcelDescUrl + ": " + e );
549cdf0e10cSrcweir throw new com.sun.star.lang.WrappedTargetException( e.toString() );
550cdf0e10cSrcweir }
551cdf0e10cSrcweir catch ( com.sun.star.uno.Exception e )
552cdf0e10cSrcweir {
553cdf0e10cSrcweir
554cdf0e10cSrcweir LogUtils.DEBUG("loadParcel() Exception while accessing filesystem url = " + parcelDescUrl + e );
555cdf0e10cSrcweir throw new com.sun.star.lang.WrappedTargetException( e.toString() );
556cdf0e10cSrcweir }
557cdf0e10cSrcweir
558cdf0e10cSrcweir finally
559cdf0e10cSrcweir {
560cdf0e10cSrcweir if ( is != null )
561cdf0e10cSrcweir {
562cdf0e10cSrcweir try
563cdf0e10cSrcweir {
564cdf0e10cSrcweir is.close(); // is will close xis
565cdf0e10cSrcweir }
566cdf0e10cSrcweir catch ( Exception ignore )
567cdf0e10cSrcweir {
568cdf0e10cSrcweir }
569cdf0e10cSrcweir }
570cdf0e10cSrcweir else if ( xis != null )
571cdf0e10cSrcweir {
572cdf0e10cSrcweir try
573cdf0e10cSrcweir {
574cdf0e10cSrcweir xis.closeInput();
575cdf0e10cSrcweir }
576cdf0e10cSrcweir catch ( Exception ignore )
577cdf0e10cSrcweir {
578cdf0e10cSrcweir }
579cdf0e10cSrcweir }
580cdf0e10cSrcweir }
581cdf0e10cSrcweir return parcel;
582cdf0e10cSrcweir }
renameParcel(String oldName, String newName)583cdf0e10cSrcweir public void renameParcel(String oldName, String newName) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException
584cdf0e10cSrcweir {
585cdf0e10cSrcweir LogUtils.DEBUG(" ** ParcelContainer Renaming parcel " + oldName + " to " + newName );
586cdf0e10cSrcweir LogUtils.DEBUG(" ** ParcelContainer is " + this );
587cdf0e10cSrcweir Parcel p = (Parcel)getByName( oldName );
588cdf0e10cSrcweir if ( p == null )
589cdf0e10cSrcweir {
590cdf0e10cSrcweir throw new com.sun.star.container.NoSuchElementException( "No parcel named " + oldName );
591cdf0e10cSrcweir }
592cdf0e10cSrcweir String oldParcelDirUrl = PathUtils.make_url( getParcelContainerDir(),
593cdf0e10cSrcweir oldName );
594cdf0e10cSrcweir String newParcelDirUrl = PathUtils.make_url( getParcelContainerDir(),
595cdf0e10cSrcweir newName );
596cdf0e10cSrcweir try
597cdf0e10cSrcweir {
598cdf0e10cSrcweir if (!m_xSFA.isFolder( oldParcelDirUrl ) )
599cdf0e10cSrcweir {
600cdf0e10cSrcweir Exception e = new com.sun.star.io.IOException("Invalid Parcel directory: " + oldName );
601cdf0e10cSrcweir throw new com.sun.star.lang.WrappedTargetException( e.toString() );
602cdf0e10cSrcweir }
603cdf0e10cSrcweir LogUtils.DEBUG(" ** ParcelContainer Renaming folder " + oldParcelDirUrl + " to " + newParcelDirUrl );
604cdf0e10cSrcweir m_xSFA.move( oldParcelDirUrl, newParcelDirUrl );
605cdf0e10cSrcweir }
606cdf0e10cSrcweir catch ( com.sun.star.ucb.CommandAbortedException ce )
607cdf0e10cSrcweir {
608cdf0e10cSrcweir LogUtils.DEBUG(" ** ParcelContainer Renaming failed with " + ce );
609cdf0e10cSrcweir throw new com.sun.star.lang.WrappedTargetException( ce.toString() );
610cdf0e10cSrcweir }
611cdf0e10cSrcweir catch ( com.sun.star.uno.Exception e )
612cdf0e10cSrcweir {
613cdf0e10cSrcweir LogUtils.DEBUG(" ** ParcelContainer Renaming failed with " + e );
614cdf0e10cSrcweir throw new com.sun.star.lang.WrappedTargetException( e.toString() );
615cdf0e10cSrcweir }
616cdf0e10cSrcweir
617cdf0e10cSrcweir p.rename( newName );
618cdf0e10cSrcweir }
619cdf0e10cSrcweir // removes but doesn't physically delele parcel from container
removeParcel(String name)620cdf0e10cSrcweir public boolean removeParcel(String name) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException
621cdf0e10cSrcweir {
622cdf0e10cSrcweir boolean result = false;
623cdf0e10cSrcweir Parcel p = (Parcel)getByName( name );
624cdf0e10cSrcweir if ( p == null )
625cdf0e10cSrcweir {
626cdf0e10cSrcweir throw new com.sun.star.container.NoSuchElementException("No parcel named " + name );
627cdf0e10cSrcweir }
628cdf0e10cSrcweir
629cdf0e10cSrcweir result = parcels.remove( p );
630cdf0e10cSrcweir return result;
631cdf0e10cSrcweir }
deleteParcel(String name)632cdf0e10cSrcweir public boolean deleteParcel(String name) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException
633cdf0e10cSrcweir {
634cdf0e10cSrcweir LogUtils.DEBUG( "deleteParcel for containerURL " + containerUrl + " name = " + name + " Langueg = " + language );
635cdf0e10cSrcweir boolean result = false;
636cdf0e10cSrcweir
637cdf0e10cSrcweir Parcel p = (Parcel)getByName( name );
638cdf0e10cSrcweir if ( p == null )
639cdf0e10cSrcweir {
640cdf0e10cSrcweir throw new com.sun.star.container.NoSuchElementException("No parcel named " + name );
641cdf0e10cSrcweir }
642cdf0e10cSrcweir
643cdf0e10cSrcweir try
644cdf0e10cSrcweir {
645cdf0e10cSrcweir String pathToParcel = PathUtils.make_url( getParcelContainerDir(), name );
646cdf0e10cSrcweir m_xSFA.kill( pathToParcel );
647cdf0e10cSrcweir }
648cdf0e10cSrcweir catch( Exception e )
649cdf0e10cSrcweir {
650cdf0e10cSrcweir LogUtils.DEBUG("Error deleteing parcel " + name );
651cdf0e10cSrcweir throw new com.sun.star.lang.WrappedTargetException( e.toString() );
652cdf0e10cSrcweir }
653cdf0e10cSrcweir
654cdf0e10cSrcweir result = parcels.remove( p );
655cdf0e10cSrcweir return result;
656cdf0e10cSrcweir }
657cdf0e10cSrcweir
getLanguage()658cdf0e10cSrcweir public String getLanguage() { return language; }
659cdf0e10cSrcweir
findScript( ParsedScriptUri parsedUri )660cdf0e10cSrcweir public ScriptMetaData findScript( ParsedScriptUri parsedUri ) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException
661cdf0e10cSrcweir {
662cdf0e10cSrcweir ScriptMetaData scriptData = null;
663cdf0e10cSrcweir Parcel p = null;
664cdf0e10cSrcweir p = (Parcel)getByName( parsedUri.parcel);
665cdf0e10cSrcweir scriptData = (ScriptMetaData)p.getByName( parsedUri.function);
666cdf0e10cSrcweir LogUtils.DEBUG("** found script data for " + parsedUri.function + " script is " + scriptData );
667cdf0e10cSrcweir return scriptData;
668cdf0e10cSrcweir
669cdf0e10cSrcweir }
parseScriptUri( String scriptURI )670cdf0e10cSrcweir public ParsedScriptUri parseScriptUri( String scriptURI ) throws com.sun.star.lang.IllegalArgumentException
671cdf0e10cSrcweir {
672cdf0e10cSrcweir
673cdf0e10cSrcweir XMultiComponentFactory xMcFac = null;
674cdf0e10cSrcweir XUriReferenceFactory xFac = null;
675cdf0e10cSrcweir
676cdf0e10cSrcweir try
677cdf0e10cSrcweir {
678cdf0e10cSrcweir xMcFac = m_xCtx.getServiceManager();
679cdf0e10cSrcweir xFac = ( XUriReferenceFactory )
680cdf0e10cSrcweir UnoRuntime.queryInterface( XUriReferenceFactory.class,
681cdf0e10cSrcweir xMcFac.createInstanceWithContext(
682cdf0e10cSrcweir "com.sun.star.uri.UriReferenceFactory", m_xCtx ) );
683cdf0e10cSrcweir }
684cdf0e10cSrcweir catch( com.sun.star.uno.Exception e )
685cdf0e10cSrcweir {
686cdf0e10cSrcweir LogUtils.DEBUG("Problems parsing URL:" + e.toString() );
687cdf0e10cSrcweir throw new com.sun.star.lang.IllegalArgumentException( "Problems parsing URL reason: " + e.toString() );
688cdf0e10cSrcweir }
689cdf0e10cSrcweir if ( xFac == null )
690cdf0e10cSrcweir {
691cdf0e10cSrcweir LogUtils.DEBUG("Failed to create UrlReference factory");
692cdf0e10cSrcweir throw new com.sun.star.lang.IllegalArgumentException( "Failed to create UrlReference factory for url " + scriptURI );
693cdf0e10cSrcweir }
694cdf0e10cSrcweir
695cdf0e10cSrcweir XUriReference uriRef = xFac.parse( scriptURI );
696cdf0e10cSrcweir XVndSunStarScriptUrl sfUri = ( XVndSunStarScriptUrl )
697cdf0e10cSrcweir UnoRuntime.queryInterface( XVndSunStarScriptUrl.class, uriRef );
698cdf0e10cSrcweir
699cdf0e10cSrcweir if ( sfUri == null )
700cdf0e10cSrcweir {
701cdf0e10cSrcweir LogUtils.DEBUG("Failed to parse url");
702cdf0e10cSrcweir throw new com.sun.star.lang.IllegalArgumentException( "Failed to parse url " + scriptURI );
703cdf0e10cSrcweir }
704cdf0e10cSrcweir
705cdf0e10cSrcweir ParsedScriptUri parsedUri = new ParsedScriptUri();
706cdf0e10cSrcweir // parse language
707cdf0e10cSrcweir parsedUri.language = sfUri.getParameter("language");
708cdf0e10cSrcweir parsedUri.function= sfUri.getName();
709cdf0e10cSrcweir parsedUri.parcel = "";
710cdf0e10cSrcweir
711cdf0e10cSrcweir // parse parcel name;
712cdf0e10cSrcweir StringTokenizer tokenizer = new StringTokenizer( parsedUri.function, "." );
713cdf0e10cSrcweir
714cdf0e10cSrcweir if ( tokenizer.hasMoreElements() )
715cdf0e10cSrcweir {
716cdf0e10cSrcweir parsedUri.parcel = (String)tokenizer.nextElement();
717cdf0e10cSrcweir LogUtils.DEBUG("** parcelName = " + parsedUri.parcel );
718cdf0e10cSrcweir }
719cdf0e10cSrcweir
720cdf0e10cSrcweir if ( parsedUri.function != null && ( parsedUri.function.length() > 0 ) )
721cdf0e10cSrcweir {
722cdf0e10cSrcweir // strip out parcel name
723cdf0e10cSrcweir parsedUri.function = parsedUri.function.substring( parsedUri.parcel.length() + 1);
724cdf0e10cSrcweir }
725cdf0e10cSrcweir
726cdf0e10cSrcweir // parse location
727cdf0e10cSrcweir parsedUri.location = sfUri.getParameter("location");
728cdf0e10cSrcweir
729cdf0e10cSrcweir // TODO basic sanity check on language, location, functioname, parcel
730cdf0e10cSrcweir // should be correct e.g. verified by MSP and LangProvider by the
731cdf0e10cSrcweir // time its got to here
732cdf0e10cSrcweir
733cdf0e10cSrcweir LogUtils.DEBUG("** location = " + parsedUri.location +
734cdf0e10cSrcweir "\nfunction = " + parsedUri.function +
735cdf0e10cSrcweir "\nparcel = " + parsedUri.parcel +
736cdf0e10cSrcweir "\nlocation = " + parsedUri.location );
737cdf0e10cSrcweir return parsedUri;
738cdf0e10cSrcweir }
739cdf0e10cSrcweir
740cdf0e10cSrcweir
741cdf0e10cSrcweir }
742