PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/org/gjt/sp/jedit/ModeCatalogHandler.java

#
Java | 138 lines | 93 code | 19 blank | 26 comment | 25 complexity | 346c809670187bbb1ec4adca4121f584 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. /*
  2. * ModeCatalogHandler.java - XML handler for mode catalog files
  3. * Copyright (C) 2000, 2001 Slava Pestov
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package org.gjt.sp.jedit;
  20. import com.microstar.xml.*;
  21. import java.io.*;
  22. import java.util.Stack;
  23. import org.gjt.sp.util.Log;
  24. class ModeCatalogHandler extends HandlerBase
  25. {
  26. ModeCatalogHandler(String directory, boolean resource)
  27. {
  28. this.directory = directory;
  29. this.resource = resource;
  30. }
  31. public Object resolveEntity(String publicId, String systemId)
  32. {
  33. if("catalog.dtd".equals(systemId))
  34. {
  35. try
  36. {
  37. return new BufferedReader(new InputStreamReader(
  38. getClass().getResourceAsStream("catalog.dtd")));
  39. }
  40. catch(Exception e)
  41. {
  42. Log.log(Log.ERROR,this,"Error while opening"
  43. + " catalog.dtd:");
  44. Log.log(Log.ERROR,this,e);
  45. }
  46. }
  47. return null;
  48. }
  49. public void attribute(String aname, String value, boolean isSpecified)
  50. {
  51. aname = (aname == null) ? null : aname.intern();
  52. if(aname == "NAME")
  53. modeName = value;
  54. else if(aname == "FILE")
  55. {
  56. if(value == null)
  57. {
  58. Log.log(Log.ERROR,this,directory + "catalog:"
  59. + " mode " + modeName + " doesn't have"
  60. + " a FILE attribute");
  61. }
  62. else
  63. file = value;
  64. }
  65. else if(aname == "FILE_NAME_GLOB")
  66. filenameGlob = value;
  67. else if(aname == "FIRST_LINE_GLOB")
  68. firstlineGlob = value;
  69. }
  70. public void doctypeDecl(String name, String publicId,
  71. String systemId) throws Exception
  72. {
  73. // older jEdit versions used a DOCTYPE of CATALOG, which
  74. // is incorrect since the DOCTYPE must be the name of the
  75. // root element, which is MODES.
  76. // so you the avid code reader should use MODES as the
  77. // DOCTYPE instead, but we still let old catalogs through
  78. // to avoid annoying users.
  79. if("CATALOG".equals(name) || "MODES".equals(name))
  80. return;
  81. Log.log(Log.ERROR,this,directory + "catalog: DOCTYPE must be CATALOG");
  82. }
  83. public void endElement(String name)
  84. {
  85. if(name.equals("MODE"))
  86. {
  87. Mode mode = jEdit.getMode(modeName);
  88. if(mode == null)
  89. {
  90. mode = new Mode(modeName);
  91. jEdit.addMode(mode);
  92. }
  93. Object path;
  94. if(resource)
  95. path = jEdit.class.getResource(directory + file);
  96. else
  97. path = MiscUtilities.constructPath(directory,file);
  98. mode.setProperty("file",path);
  99. if(filenameGlob != null)
  100. mode.setProperty("filenameGlob",filenameGlob);
  101. else
  102. mode.unsetProperty("filenameGlob");
  103. if(firstlineGlob != null)
  104. mode.setProperty("firstlineGlob",firstlineGlob);
  105. else
  106. mode.unsetProperty("firstlineGlob");
  107. mode.init();
  108. modeName = file = filenameGlob = firstlineGlob = null;
  109. }
  110. }
  111. // end HandlerBase implementation
  112. // private members
  113. private String directory;
  114. private boolean resource;
  115. private String modeName;
  116. private String file;
  117. private String filenameGlob;
  118. private String firstlineGlob;
  119. }