View Javadoc

1   /*
2    * $Id: LibraryEntryDescriptor.java,v 1.12 2005/06/01 19:55:51 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8    
9   package com.bbn.swede.core.libraries;
10  
11  import java.io.File;
12  import java.io.InputStream;
13  import java.util.zip.ZipEntry;
14  import java.util.zip.ZipOutputStream;
15  
16  import com.bbn.swede.core.OWLCore;
17  import com.bbn.swede.core.dom.IOWLAbstractSyntaxTree;
18  
19  /***
20   * The main interface for operations on the LibraryEntry.  This class
21   * implements ILibraryEntryDescriptor.
22   * 
23   * 
24   * @author aperezlo
25   */
26  public class LibraryEntryDescriptor implements ILibraryEntryDescriptor
27  {
28     private LibraryEntryConfiguration _configuration;
29     private LibraryEntry _entry;
30     private String _filename;
31     
32     /***
33      * Creates a descriptor for a library entry.
34      * @param entry The entry
35      */
36     LibraryEntryDescriptor(LibraryEntry entry)
37     {
38        _entry = entry;
39        _filename = _entry.getFilename();
40        _configuration = new LibraryEntryConfiguration();
41        _configuration.setProperties(_entry.getProperties());
42        _configuration.setName(_filename);
43     }
44     
45     /* (non-Javadoc)
46      * @see com.bbn.swede.core.libraries.ILibraryEntryDescriptor#getEntry()
47      */
48     public ILibraryEntry getEntry()
49     {
50        return _entry;
51     }
52  
53     
54     /***
55      * Retrieves the properties validator from the entry's configuration.
56      * @return the properties validator
57      * @see IConfiguration#getPropertiesValidator()
58      */
59     public IConfigurationValidator getPropertiesValidator()
60     {
61        return _configuration.getPropertiesValidator();
62     }
63     
64     /* (non-Javadoc)
65      * @see com.bbn.swede.core.libraries.ILibraryEntryDescriptor#getConfiguration()
66      */
67     public synchronized IConfiguration getConfiguration()
68     {
69        return _configuration;
70     }
71     
72     /* (non-Javadoc)
73      * @see com.bbn.swede.core.libraries.ILibraryEntryDescriptor#hasOAST()
74      */
75     public synchronized boolean hasOAST()
76     {
77        return (_entry.getOAST() != null);
78     }
79  
80     /* (non-Javadoc)
81      * @see com.bbn.swede.core.libraries.ILibraryEntryDescriptor#getFileInputStream()
82      */
83     public synchronized InputStream getFileInputStream()
84     {
85        return _entry.getFile();
86     }
87  
88     /* (non-Javadoc)
89      * @see com.bbn.swede.core.libraries.ILibraryEntryDescriptor#close()
90      */
91     public synchronized boolean close()
92     {
93        return _entry.close();
94     }
95     
96     /***
97      * Indicates whether this descriptor has a URI.
98      * @return <code>true</code> if this 'Descriptor has a URI, 
99      *         <code>false</code> otherwise
100     */
101    public synchronized boolean hasURI()
102    {
103       return _configuration.getURI() != null;
104    }
105    
106    /***
107     * Retrieves the entry's name from this 'Descriptor's 
108     * LibraryEntryConfiguration object.
109     * @return The entry name
110     * @see IConfiguration#getName()
111     */
112    public synchronized String getName()
113    {
114       return _configuration.getName();
115    }
116    
117    /***
118     * Retrieves the entry's URI from this 'Descriptor's 
119     * LibraryEntryConfiguration object.
120     * @return The entry URI
121     * @see LibraryEntryConfiguration#getURI()
122     */
123    public synchronized String getURI()
124    {
125       return _configuration.getURI();
126    }
127 
128    /* (non-Javadoc)
129     * @see com.bbn.swede.core.libraries.ILibraryEntryDescriptor#getOAST()
130     */
131    public synchronized IOWLAbstractSyntaxTree getOAST()
132    {
133       return _entry.getOAST();
134    }
135 
136    /***
137     * Writes this 'Descriptor's LibraryEntry to a ZipOutputStream.
138     * <br /><br />
139     * This method creates two entries in the zip file (OAST support is 
140     * currently not yet implemented), one for this 'Descriptor's 
141     * LibraryEntry's file, and one for the associated metadata.  For an 
142     * explanation of how library files are structured, see {@link Library}.  
143     * <br /> <br />
144     * This method does not close the ZipOutputStream.
145     * @param out the ZipOutputStream to which this 'Descriptor's 'Entry should be written
146     */
147    synchronized void write(ZipOutputStream out)
148    {
149       String path = null;
150       ZipEntry fileEntry = null; 
151       ZipEntry metaEntry = null;
152       ZipEntry oastEntry = null;
153       
154       try
155       {
156          path = LibraryDescriptor.METADATA_PATH + File.separator + _filename + LibraryDescriptor.METADATA_EXTENSION;
157          _entry.setProperties(_configuration.getProperties());
158          metaEntry = new ZipEntry(path);
159          out.putNextEntry(metaEntry);
160          _entry.writeProperties(out);
161          out.closeEntry();
162          
163          path = LibraryDescriptor.FILE_PATH + File.separator + _filename + LibraryDescriptor.FILE_EXTENSION;
164          fileEntry = new ZipEntry(path);
165          out.putNextEntry(fileEntry);
166          _entry.writeFile(out);
167          out.closeEntry();
168          
169          if(hasOAST())
170          {
171             path = LibraryDescriptor.OAST_PATH + File.separator + _filename + LibraryDescriptor.OAST_EXTENSION;
172             oastEntry = new ZipEntry(path);
173             out.putNextEntry(oastEntry);
174             _entry.writeOAST(out);
175             out.closeEntry();
176          }
177       }
178       catch(Exception e)
179       {
180          OWLCore.logWarning(OWLCore.getID(), "Couldn't write entry for '" + _filename + "'...", e);   
181       }
182    }
183 
184 }