Project Report: fawkez

Packagesummary org.jcoderz.phoenix.report

org.jcoderz.phoenix.report.SyntaxModeCatalogHandler

LineHitsNoteSource
1  /*
2   * $Id: Java2Html.java 1238 2008-11-03 12:37:53Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.phoenix.report;
34  
35  import java.io.IOException;
36  import java.io.InputStream;
37  import java.util.logging.Level;
38  import java.util.logging.Logger;
39  
40  import org.gjt.sp.jedit.Mode;
41  import org.gjt.sp.jedit.jEdit;
42  import org.gjt.sp.jedit.syntax.ModeProvider;
43  import org.gjt.sp.util.XMLUtilities;
44  import org.jcoderz.commons.util.IoUtil;
45  import org.xml.sax.Attributes;
46  import org.xml.sax.InputSource;
47  import org.xml.sax.helpers.DefaultHandler;
48  
49  /**
50   * Responsible to load the catalog file that bundles the 
51   * jEdit mode files.
52   * Inspired by the ModeCatalogHandler that comes with jEdit. 
53   * 
54   * The mode files and the catalog are expected to be packed as 
55   * resources with the jEdit class in a <code>modes</code>
56   * package to be found. There s no additional flexibility to
57   * provide own or modified modes files.
58   * 
59   * Currently with jEdit4.3pre16 there is no way to use a own 
60   * ModeProvider to be able to
61   * overload the <code>loadMode(String)</code> method because the
62   * ModeProvider is hard linked in the Mode class. So we can not 
63   * use our own XModeHandler and overload error handling there.  
64   * 
65   * Use {@link #loadModes()} to load the catalog and all modes.
66   * 
67   * @author Andreas Mandel
68   */
69  public final class SyntaxModeCatalogHandler
70      extends DefaultHandler
71  {
720     private static final String CLASSNAME
73          = SyntaxModeCatalogHandler.class.getName();
740     private static final Logger LOGGER = Logger.getLogger(CLASSNAME);
75      
76      /**
77       * A new SyntaxModeCatalogHandler all mode files are retrieved via 
78       * class path and expected to resist in the modes package.
79       */
80      private SyntaxModeCatalogHandler ()
810     {
82          // use static method to load the data
830     }
84  
85      /**
86       * Load all modes referred by the catalog file provided.
87       */
88      public static void loadModes ()
89      {
900         final InputStream in
91              = jEdit.class.getResourceAsStream("/modes/catalog");
92          try
93          {
940             XMLUtilities.parseXML(in, new SyntaxModeCatalogHandler());
95          }
960         catch (IOException ex)
97          {
980             LOGGER.log(Level.WARNING, "Failed to load modes catalog, "
99                  + "no syntax highlighting will be available in the output.",
100                  ex);
101          }
102          finally
103          {
1040             IoUtil.close(in);
1050         }
1060     }
107  
108      /**
109       * Takes care to find the jEdit catalog.dtd.
110       * {@inheritDoc}
111       */
112      public InputSource resolveEntity (String publicId, String systemId)
113      {
1140         return XMLUtilities.findEntity(systemId, "catalog.dtd", jEdit.class);
115      }
116  
117      /**
118       * Handles the mode elements in the catalog and loads the modes
119       * listed there.
120       *  
121       * {@inheritDoc}
122       */
123      public void startElement (String uri, String localName,
124          String qName, Attributes attrs)
125      {
1260         if ("MODE".equals(qName))
127          {
1280             final String modeName = attrs.getValue("NAME");
1290             final String file = attrs.getValue("FILE");
1300             if (file == null)
131              {
1320                 LOGGER.log(Level.WARNING, "Mode '" + modeName
133                      + "' doesn't have a FILE attribute");
134              }
135  
1360             Mode mode = ModeProvider.instance.getMode(modeName);
1370             if (mode == null)
138              {
1390                 mode = new Mode(modeName);
1400                 ModeProvider.instance.addMode(mode);
141              }
142  
1430             mode.setProperty("file", "/modes/" + file);
144  
1450             final String filenameGlob = attrs.getValue("FILE_NAME_GLOB");
1460             if (filenameGlob != null)
147              {
1480                 mode.setProperty("filenameGlob", filenameGlob);
149              }
150              else
151              {
1520                 mode.unsetProperty("filenameGlob");
153              }
154  
1550             final String firstlineGlob = attrs.getValue("FIRST_LINE_GLOB");
1560             if (firstlineGlob != null)
157              {
1580                 mode.setProperty("firstlineGlob", firstlineGlob);
159              }
160              else
161              {
1620                 mode.unsetProperty("firstlineGlob");
163              }
164  
1650             mode.init();
166          }
1670     }
168  }

Findings in this File