View Javadoc

1   /*
2    * $Id: EntryMetadataSuggester.java,v 1.3 2005/06/01 17:47:43 aperezlo 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.ui.wizards;
10  
11  import java.io.BufferedReader;
12  import java.io.File;
13  import java.io.FileInputStream;
14  import java.io.FileNotFoundException;
15  import java.io.InputStream;
16  import java.io.InputStreamReader;
17  import java.text.SimpleDateFormat;
18  import java.util.Properties;
19  import java.util.regex.Matcher;
20  import java.util.regex.Pattern;
21  
22  import org.eclipse.core.resources.IFile;
23  import org.eclipse.core.runtime.CoreException;
24  
25  import com.bbn.swede.core.libraries.LibraryEntryConfiguration;
26  
27  /***
28   * This class is used to suggest library metadata for a file.
29   * 
30   * In particular it will attempt to find a base URI for an OWL file if 
31   * one is declared.
32   * 
33   * @author aperezlo
34   */
35  public class EntryMetadataSuggester
36  {
37  
38     private final Pattern _xmlBasePattern = Pattern
39        .compile("xml:base//s*?=//s*?//\"//w*:/+?([a-zA-Z]:)?/+?([//w//-]++//.?)+?([//w//-]++)?/([//w//-//.]++/?)*?[//w//-]*?(//.[//w//-]++)*?//\"");
40  
41     private final Pattern _urlPattern = Pattern
42        .compile("//w*:/+?([a-zA-Z]:)?/+?([//w//-]++//.?)+?([//w//-]++)?/([//w//-//.]++/?)*?[//w//-]*?(//.[//w//-]++)*?");
43     
44     private String _name;
45     private String _uri;
46     
47     /***
48      * findURI().
49      * 
50      * @param doc the IFile which represents the OWL document
51      * @return the URI, if one could be found
52      */
53     protected String findURI(IFile doc)
54     {
55        String toReturn = null;
56        try
57        {
58           toReturn = extractURI(doc.getContents());
59        }
60        catch (CoreException e)
61        {
62           e.printStackTrace();
63        }
64        return toReturn;
65     }
66     
67     private Properties getNewDefaults()
68     {
69        Properties toReturn = new Properties();
70        SimpleDateFormat sdf = new SimpleDateFormat(LibraryEntryConfiguration.ENTRY_DATE_FORMAT_STR);
71  
72  //      toReturn.setProperty(LibraryEntryConfiguration.ENTRY_CREATION_DATE_PROPERTY, sdf.format(new Date()));
73  //      toReturn.setProperty(
74  //         LibraryEntryConfiguration.ENTRY_UPDATE_SCHEDULE_PROPERTY, 
75  //         LibraryEntryConfiguration.ENTRY_UPDATE_SCHEDULE_MANUAL_STR);
76        return toReturn;
77     }
78     
79     private String extractURI(InputStream is)
80     {
81        String toReturn = null;
82        BufferedReader in = null;
83        String tempLine = null;
84      
85        try
86        {
87           in = new BufferedReader(new InputStreamReader(is));
88           while((tempLine = in.readLine()) != null)
89           {
90              toReturn = extractURI(tempLine);
91              if(toReturn != null)
92              {
93                 break;
94              }
95           }
96        }
97        catch(Exception e)
98        {
99           //do nothing
100       }
101       finally
102       {
103          try
104          {
105             in.close();
106          }
107          catch(Exception e)
108          {
109             //do nothing
110          }
111       }
112 
113       return toReturn;
114   
115    }
116    
117    /***
118     * findURI().
119     * 
120     * @param doc the java.io.File which is a handle to the OWL document
121     * @return the base URI, if one could be found 
122     */
123    protected String findURI(File doc)
124    {
125       String toReturn = null;
126       
127       try
128       {
129          toReturn = extractURI(new FileInputStream(doc));
130       }
131       catch (FileNotFoundException e)
132       {
133          //do nothing
134       }
135       
136       return toReturn;
137    }
138    
139    
140    /***
141     * Primary interface for file metadata default suggestions.
142     * 
143     * @param doc the IFile handle to the OWL document
144     * @return a java.util.Properties object filled with the suggested metadata
145     */
146    public Properties suggestDefaults(IFile doc)
147    {
148       Properties toReturn = getNewDefaults();
149       String sUri = null;
150          
151       if(doc != null)
152       {
153          _name = doc.getName();
154          try
155          {
156             sUri = extractURI(doc.getContents());
157          }
158          catch (CoreException e)
159          {
160             //do nothing
161          }
162          if(sUri != null)
163          {
164             _uri = sUri;
165          }
166       }
167       return toReturn;
168    }
169       
170    /***
171     * Returns default suggested name.
172     * 
173     * @return the name found, or null if none was
174     */
175    public String getName()
176    {
177       return _name;
178    }
179    
180    /***
181     * Returns the default suggested URI.
182     * 
183     * @return the name URI, or null if none was
184     */
185    public String getURI()
186    {
187       return _uri;
188    }
189    
190    /***
191     * Primary interface for file metadata default suggestions.
192     * 
193     * @param doc the File handle to the OWL document
194     * @return a java.util.Properties object filled with the suggested metadata
195     */
196    public Properties suggestDefaults(File doc)
197    {
198       Properties toReturn = getNewDefaults();
199       String sUri = null;
200       if(doc != null)
201       {
202          _name = doc.getName();
203          try
204          {
205             sUri = extractURI(new FileInputStream(doc));
206          }
207          catch (FileNotFoundException e)
208          {
209             e.printStackTrace();
210          }
211          if(sUri != null)
212          {
213             _uri = sUri;
214          }
215       }
216       return toReturn;
217    }
218    
219    private String extractURI(String testString)
220    {
221       String toReturn = null;
222       String tempString = null;
223       Matcher baseMatcher = _xmlBasePattern.matcher(testString);
224       Matcher uriMatcher = null;
225       if(baseMatcher.find())
226       {
227          tempString = baseMatcher.group();
228          uriMatcher = _urlPattern.matcher(tempString);
229          uriMatcher.find();
230          tempString = tempString.substring(uriMatcher.start(), tempString.length() - 1);
231 
232          toReturn = tempString;
233       }
234 
235       return toReturn;
236    }
237    
238 }