PageRenderTime 321ms CodeModel.GetById 308ms app.highlight 10ms RepoModel.GetById 0ms app.codeStats 1ms

/bundles/plugins-trunk/XML/xml/parser/CachedGrammarPool.java

#
Java | 148 lines | 81 code | 16 blank | 51 comment | 14 complexity | b0868e2b82ed33ec1485540678aeb98f MD5 | raw file
  1package xml.parser;
  2
  3import java.util.List;
  4import java.util.ArrayList;
  5
  6import org.apache.xerces.xni.grammars.*;
  7import org.apache.xerces.xni.*;
  8
  9import org.gjt.sp.jedit.Buffer;
 10import org.gjt.sp.util.Log;
 11
 12import xml.cache.Cache;
 13import xml.cache.CacheEntry;
 14import static xml.Debug.*;
 15
 16/**
 17 * FIXME: thread safety
 18 */
 19public class CachedGrammarPool implements XMLGrammarPool {
 20	private Buffer buffer;
 21	
 22	public CachedGrammarPool(Buffer requestingBuffer){
 23		this.buffer = requestingBuffer;
 24	}
 25	
 26    /**
 27     * <p> retrieve the initial known set of grammars. this method is
 28     * called by a validator before the validation starts. the application 
 29     * can provide an initial set of grammars available to the current 
 30     * validation attempt. </p>
 31     * @param grammarType the type of the grammar, from the
 32     *  <code>org.apache.xerces.xni.grammars.Grammar</code> interface.
 33     * @return the set of grammars the validator may put in its "bucket"
 34     */
 35     public Grammar[] retrieveInitialGrammarSet(String grammarType){
 36     	 return new Grammar[0];
 37     }
 38
 39    /** 
 40     * <p>return the final set of grammars that the validator ended up
 41     * with.  
 42     * This method is called after the
 43     * validation finishes. The application may then choose to cache some
 44     * of the returned grammars. </p>
 45     * @param grammarType the type of the grammars being returned;
 46     * @param grammars an array containing the set of grammars being
 47     *  returned; order is not significant.
 48     */
 49     public void cacheGrammars(String grammarType, Grammar[] grammars){
 50     	 if(DEBUG_CACHE)Log.log(Log.DEBUG,CachedGrammarPool.class,"cacheGrammars("+grammarType+")");
 51     	 
 52     	 Cache cache = Cache.instance();
 53     	 List<CacheEntry> myGrammars = new ArrayList<CacheEntry>(grammars.length);
 54     	 for(Grammar g: grammars){
 55     	 	 XMLGrammarDescription desc = g.getGrammarDescription();
 56			 String path = null;
 57			 try{
 58				 path = xml.Resolver.instance().resolveEntityToPath(
 59					 null,//name
 60					 desc.getPublicId(),
 61					 desc.getBaseSystemId(),
 62					 desc.getLiteralSystemId()
 63					 );
 64			 }catch(Exception e){
 65				 //not allowed to rethrow it (or as RuntimeException)
 66				 Log.log(Log.ERROR,CachedGrammarPool.class,"error caching "+desc,e);
 67			 }
 68     	 	 if(DEBUG_CACHE)Log.log(Log.DEBUG,CachedGrammarPool.class,"grammar="+desc.getExpandedSystemId());
 69     	 	 if(DEBUG_CACHE)Log.log(Log.DEBUG,CachedGrammarPool.class,"path="+path);
 70     	 	 // happens for DTDs inside the document, where systemId==publicId==null
 71     	 	 // and that we don't want to cache
 72     	 	 if(path!=null)myGrammars.add(cache.put(path, grammarType, g));
 73     	 }
 74     	 for(CacheEntry en: myGrammars){
 75     	 	 en.getRequestingBuffers().add(buffer);
 76     	 	 en.getRelated().addAll(myGrammars);
 77     	 	 en.getRelated().remove(en);
 78     	 }
 79     }
 80
 81    /** 
 82     * <p> This method requests that the application retrieve a grammar
 83     * corresponding to the given GrammarIdentifier from its cache.
 84     * If it cannot do so it must return null; the parser will then
 85     * call the EntityResolver.  <strong>An application must not call its
 86     * EntityResolver itself from this method; this may result in infinite
 87     * recursions.</strong>
 88     * @param desc The description of the Grammar being requested.
 89     * @return the Grammar corresponding to this description or null if
 90     *  no such Grammar is known.
 91     */
 92     public Grammar retrieveGrammar(XMLGrammarDescription desc){
 93     	 if(DEBUG_CACHE)Log.log(Log.DEBUG,CachedGrammarPool.class,"retrieveGrammar("+desc.getGrammarType()+","+desc+")");
 94
 95     	 // the validator firstly asks for a grammar without location information
 96     	 if(desc.getPublicId()==null && desc.getLiteralSystemId() == null)return null;
 97     	 
 98     	 // don't cache DTD, overwise I don't get the DTD events to construct the CompletionInfo from a DTD !
 99     	 //if(desc.getGrammarType() == desc.XML_DTD)return null;
100     	 Cache cache = Cache.instance();
101     	 String path = null;
102     	 try{
103     	 	 path = xml.Resolver.instance().resolveEntityToPath(
104				 null,//name
105				 desc.getPublicId(),
106				 desc.getBaseSystemId(),
107				 desc.getLiteralSystemId()
108				 );
109		 }catch(Exception e){
110		 	 //not allowed to rethrow it (or as RuntimeException)
111		 	 Log.log(Log.ERROR,CachedGrammarPool.class,"error retrieving "+desc);
112		 	 Log.log(Log.ERROR,CachedGrammarPool.class,e);
113		 }
114		 CacheEntry en = cache.get(path, desc.getGrammarType());
115		 if(en == null){
116		 	 if(DEBUG_CACHE)Log.log(Log.DEBUG,CachedGrammarPool.class,path+" not found in cache");
117		 	 return null;
118		 }else{
119		 	 if(DEBUG_CACHE)Log.log(Log.DEBUG,CachedGrammarPool.class,path+" found in cache");
120		 	 en.getRequestingBuffers().add(buffer);
121		 	 return (Grammar)en.getCachedItem();
122		 }
123     }
124
125    /**
126     * Causes the XMLGrammarPool not to store any grammars when
127     * the cacheGrammars(String, Grammar[[]) method is called.
128     */
129     public void lockPool(){
130     	 throw new UnsupportedOperationException();
131     }
132
133    /**
134     * Allows the XMLGrammarPool to store grammars when its cacheGrammars(String, Grammar[])
135     * method is called.  This is the default state of the object.
136     */
137     public void unlockPool(){
138     	 throw new UnsupportedOperationException();
139     }
140
141    /**
142     * Removes all grammars from the pool.
143     */
144     public void clear(){
145		 throw new UnsupportedOperationException();
146     }
147	
148}