PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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