MegaPov1dot2dot1DocExtractor.java from PovClipse at Krugle
Show MegaPov1dot2dot1DocExtractor.java syntax highlighted
/*
* PovClipse - Eclipse plugin for editing and rendering Povray sceene files.
* Copyright (C) 2006-2007 Wolfgang Moestl wmoestl@web.de
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package com.wm.povclipse.megapov.docs.one_two_one.help;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.eclipse.jface.resource.ImageRegistry;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.xpath.XPath;
import com.wm.povclipse.LoggerProvider;
import com.wm.povclipse.PovClipseEditorPlugin;
import com.wm.povclipse.editors.help.AbstractPovrayDocExtractor;
import com.wm.povclipse.editors.help.HelpKeywordUrl;
import com.wm.povclipse.utils.StringUtils;
/**
* Concrete implementation for presessing the
* Povray help index file of version 3.6.1.
*
* @author Wolfgang Möstl
*/
public class MegaPov1dot2dot1DocExtractor extends AbstractPovrayDocExtractor {
private static final String TAG_DL = "dl";
private static final String TAG_DT = "dt";
private static final String TAG_DD = "dd";
private static final String TAG_A = "a";
/**
* Logger
*/
static private Logger logger = LoggerProvider.getLogger(MegaPov1dot2dot1DocExtractor.class.getName());
private String pluginId;
private int refCounter = 0;
public MegaPov1dot2dot1DocExtractor(String pluginId) {
this.pluginId = pluginId;
readIndexFileAsynchronous();
}
/**
* @return Always "help/megapov/1.2.1".
* @see com.wm.povclipse.editors.help.AbstractPovrayDocExtractor#getBasePath()
*/
public String getBasePath() {
return "help/megapov/1.2.1";
}
/**
* @return the absolute path into the eclipse help system for the
* Povray help documentation root folder.
* @see com.wm.povclipse.editors.help.AbstractPovrayDocExtractor#getHelpRoot()
*/
public String getHelpRoot() {
return "/" + pluginId + "/help/megapov/1.2.1/html/";
}
/**
* @return Always "1.2.1"
* @see com.wm.povclipse.editors.help.PovrayDocExtractor#getPovrayVersion()
*/
public String getPovrayVersion() {
return "MegaPOV 1.2.1";
}
/**
* Initializes the keyword map and starts the processing of the
* Povray help index file in an asynchronous way.
* @see com.wm.povclipse.editors.help.AbstractPovrayDocExtractor#initKeywordMap()
*/
protected void initKeywordMap() {
super.initKeywordMap();
HelpExtractor helpExtractor = new HelpExtractor();
helpExtractor.start();
}
/**
* Thread for building the keyword map from the MegaPOV help index file.
* <p>The index file is transformed prior to parsing.</p>
* @author Wolfgang Möstl
*/
private class HelpExtractor extends Thread {
ImageRegistry imgReg = PovClipseEditorPlugin.getDefault().getImageRegistry();
/**
* @see java.lang.Thread#run()
*/
public void run() {
String doc = getKeywordIndexFile();
if (null != doc) {
process(doc);
}
}
/**
* @return The content of the Povray help index file between
* the <code><dl></code> and <code></dl></code> tags.
*/
public String getKeywordIndexFile() {
String fileName = getBasePath() + "/html/megapov_index.html";
InputStream inStream = this.getClass().getClassLoader().getResourceAsStream(fileName);
// fast exit if the doc plugin is not installed
if (null == inStream) {
logger.fatal("The MegaPOV index file " + fileName + " can not be found!");
return null;
}
StringBuffer sb = StringUtils.readInputStream(new InputStreamReader(inStream));
if (sb.length() == 0) {
logger.fatal("The MegaPOV index file " + fileName + " is empty!");
return null;
}
int startIndex = sb.indexOf("<body");
int endIndex = sb.lastIndexOf("</body>");
if (startIndex < 0 ) return null;
// startIndex += 5; // cut '<h3>'
if (endIndex < 0)
endIndex = sb.length()-1;
return sb.substring(startIndex, endIndex+7);
}
/**
* Processes the <code><dd></code> tag representing the list of all
* links for the given keyword.
* @param keyword The keyword who's link list is being processed
* @param ddElement The link list for the given keyword
*/
@SuppressWarnings("unchecked")
private void processKeyword(String keyword, Element ddElement) {
/* Example for the ddElement:
<dd>
<dl>
<dt>Reference, Adaptive radiosity pretrace, <a href="global_settings.html#radiosity_adaptive_pretrace">Adaptive radiosity pretrace</a></dt>
<dt>Reference, Radiosity adaptive error_bound, <a href="global_settings.html#radiosity_error_bound_adaptive">Adaptive radiosity error_bound</a></dt>
</dl>
</dd>
*/
// logger.trace("Processing list for keyword [" + keyword + "]");
ArrayList<HelpKeywordUrl> list = new ArrayList<HelpKeywordUrl>();
List<Element> dtList = ddElement.getChild(TAG_DL).getChildren(TAG_DT);
for (int i=0; i<dtList.size(); i++) {
Element dtTag = dtList.get(i);
Element aTag = dtTag.getChild(TAG_A);
String name = dtTag.getValue();
String href = getHelpRoot() + aTag.getAttributeValue("href");
logger.trace("Keyword [" + keyword + "] : adding url [" + name + "] : [" + href + "]");
HelpKeywordUrl keywordUrl = new HelpKeywordUrl(name, href, imgReg.get(PovClipseEditorPlugin.IMAGE_LOGO_MEGAPOV));
list.add(keywordUrl);
refCounter ++;
}
keywordMap.put(keyword, list);
}
/**
* Builds the keyword map from the Povray help index file.
* @param doc The content of the Povray help index file.
*/
@SuppressWarnings("unchecked")
private void process(String doc) {
int keywordCounter = 0;
refCounter = 0;
try {
doc = doc.replaceAll("<!--[\\d\\s\\w]*-->", ""); // eliminate all comments
doc = doc.replaceAll("[\\t\\n\\x0B\\f\\r]", " "); // change all whitespaces to spaces
doc = doc.replaceAll("[ ]+", " "); // eliminate multi-spaces
doc = doc.replaceAll("<hr>", ""); // eliminate hr tags
StringBuffer sb = new StringBuffer(doc);
// now we have always the following syntax:
// <h3>TITLE</h3><dl><dt>KEY</dt><dd><dl><dt>REFERENFCE</dt></dl></dd>[NEXT_DT]
//
Document document = new org.jdom.input.SAXBuilder().build(new StringReader(sb.toString()));
Element root = document.getRootElement();
List<Element> divList = XPath.selectNodes(root, "//div[@class='indexdiv']");
String key = null;
for (int i=0; i<divList.size(); i++) {
Element divTag = divList.get(i);
List<Element> tagList = divTag.getChildren();
for (int d=0; d<tagList.size(); d++) {
Element tag = tagList.get(d);
// only the <dl> tag is of interest
if (TAG_DL.equals(tag.getName())) {
List<Element> keyList = tag.getChildren();
for (int j=0; j<keyList.size(); j++) {
Element keyElement = keyList.get(j);
if (TAG_DT.equals(keyElement.getName())) {
key = keyElement.getValue();
} else if (TAG_DD.equals(keyElement.getName())) {
// process the list
keywordCounter ++;
processKeyword(key, keyElement);
}
}
}
}
}
} catch ( IOException ioEx) {
logger.fatal(ioEx);
} catch (JDOMException jdomEx) {
logger.fatal(jdomEx);
} catch (Exception e) {
logger.fatal(e);
} finally {
setRunning(false);
logger.info(getPovrayVersion() + " help index: added " + keywordCounter + " keywords with a total of " + refCounter +" references.");
}
}
}
}
See more files for this project here
PovClipse is an eclipse editor plugin for Povray (Persistence of Vision Raytracer) sceene- and include files.\r\nIt features syntax highlighting, code folding, code assist, outline view as well as running Povray using render configurations.
Project homepage:
http://sourceforge.net/projects/povclipse
Programming language(s): Java
License: other
MegaPov1dot2dot1DocExtractor.java