View Javadoc

1   /* ==========================================================================
2    * Copyright 2003-2004 Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   * =========================================================================
16   */
17  
18  package org.mevenide.netbeans.grammar;
19  
20  import java.io.File;
21  import java.util.Collection;
22  import java.util.HashSet;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.mevenide.environment.ConfigUtils;
26  import org.mevenide.grammar.AttrCompletionProvider;
27  import org.mevenide.grammar.AttributeCompletion;
28  import org.mevenide.grammar.TagLib;
29  import org.mevenide.grammar.TagLibProvider;
30  import org.mevenide.grammar.impl.EmptyAttributeCompletionImpl;
31  import org.mevenide.grammar.impl.GoalsAttributeCompletionImpl;
32  import org.mevenide.grammar.impl.MavenTagLibProvider;
33  import org.mevenide.grammar.impl.PluginDefaultsCompletionImpl;
34  import org.mevenide.grammar.impl.StaticTagLibImpl;
35  import org.openide.filesystems.FileObject;
36  import org.openide.filesystems.Repository;
37  
38  /***
39   * Netbeans provider of TagLib instances.
40   * @author  Milos Kleint (ca206216@tiscali.cz)
41   */
42  
43  public class NbTagLibProvider implements TagLibProvider, AttrCompletionProvider {
44      
45      private static Log logger = LogFactory.getLog(NbTagLibProvider.class);
46      
47      private File dynaTagFile;
48      private MavenTagLibProvider mavenProvider;
49      /*** Creates a new instance of NbTagLibProvider */
50      public NbTagLibProvider() {
51          dynaTagFile = new File(ConfigUtils.getDefaultLocationFinder().getMavenPluginsDir(), "dynatag.cache");
52          mavenProvider = new MavenTagLibProvider(dynaTagFile) {
53  			protected ClassLoader getMavenClassLoader() {
54  				return MavenGrammarModule.getMavenClassLoader();
55  			}
56          };
57      }
58      
59      public String[] getAvailableTags() {
60          // retrieve the tags anew each time, maybe can be cached to improve performance.
61          FileObject tagLibFolder = Repository.getDefault().getDefaultFileSystem().findResource("Plugins/Mevenide-Grammar");
62          FileObject[] libFOs = tagLibFolder.getChildren();
63          Collection toReturn = new HashSet();
64          if (libFOs != null)
65          {
66              for (int i = 0; i < libFOs.length; i++)
67              {
68                  if (libFOs[i].getExt().equals("xml") 
69                   && !libFOs[i].getName().startsWith("default")) { //kind of hack not to show the default code completion..
70                      // kind of weird condition however for now it's ok.. maybe we can live without any condition or make it mimetype one.
71                       
72                       //additional hack replacing - with : in name.
73                      toReturn.add(libFOs[i].getName().replace('-',':'));
74                  }
75              }
76          }
77          String[] mavenTags = mavenProvider.getAvailableTags();
78          for (int i = 0; i < mavenTags.length; i++) 
79          {
80              toReturn.add(mavenTags[i]);
81          }
82          String[] str = new String[toReturn.size()];
83          str = (String[])toReturn.toArray(str);
84          return str;
85      }
86      
87      public TagLib retrieveTagLib(String name) {
88          TagLib toReturn = null;
89          toReturn = mavenProvider.retrieveTagLib(name);
90          if (toReturn != null) {
91              return toReturn;
92          }
93          name = name.replace(':', '-');
94          FileObject tagLib = Repository.getDefault().getDefaultFileSystem().findResource("Plugins/Mevenide-Grammar/" + name + ".xml");
95          if (tagLib == null) {
96                  logger.error("cannot find taglib with name=" + name + "  (no fileobject found)");
97                  return null;
98          }
99          try {
100             toReturn = new StaticTagLibImpl(tagLib.getInputStream());
101         } catch (Exception exc) {
102             logger.error("cannot retrieve the taglibrary=" + name, exc);
103         }
104         return toReturn;
105         
106     }
107     
108     public AttributeCompletion retrieveAttributeCompletion(String name)
109     {
110         AttributeCompletion completion = null;
111         if ("goal".equals(name)) {
112             try {
113                 completion = new GoalsAttributeCompletionImpl();
114             } catch (Exception exc) {
115                 logger.error("Cannot create new instance of GoalsAttributeCompletionImpl", exc);
116             }
117         }
118         if ("pluginDefaults".equals(name)) {
119             completion = new PluginDefaultsCompletionImpl();
120         }
121         if (completion == null) {
122             // fallback implementation.
123             logger.warn("AttributeCompletion: using a fallback implementation, no impl for type=" + name);
124             completion = new EmptyAttributeCompletionImpl(name);
125         }
126         return completion;
127     }
128     
129 }