📄 filtermanager.java
字号:
Document doc = new SAXBuilder().build( xmlStream ); XPath xpath = XPath.newInstance("/pagefilters/filter"); List nodes = xpath.selectNodes( doc ); for( Iterator i = nodes.iterator(); i.hasNext(); ) { Element f = (Element) i.next(); String filterClass = f.getChildText("class"); Properties props = new Properties(); List params = f.getChildren("param"); for( Iterator par = params.iterator(); par.hasNext(); ) { Element p = (Element) par.next(); props.setProperty( p.getChildText("name"), p.getChildText("value") ); } initPageFilter( filterClass, props ); } } /** * Does the filtering before a translation. * * @param context The WikiContext * @param pageData WikiMarkup data to be passed through the preTranslate chain. * @throws FilterException If any of the filters throws a FilterException * @return The modified WikiMarkup * * @see PageFilter#preTranslate(WikiContext, String) */ public String doPreTranslateFiltering( WikiContext context, String pageData ) throws FilterException { fireEvent( WikiPageEvent.PRE_TRANSLATE_BEGIN, context ); for( Iterator i = m_pageFilters.iterator(); i.hasNext(); ) { PageFilter f = (PageFilter) i.next(); pageData = f.preTranslate( context, pageData ); } fireEvent( WikiPageEvent.PRE_TRANSLATE_END, context ); return pageData; } /** * Does the filtering after HTML translation. * * @param context The WikiContext * @param htmlData HTML data to be passed through the postTranslate * @throws FilterException If any of the filters throws a FilterException * @return The modified HTML * @see PageFilter#postTranslate(WikiContext, String) */ public String doPostTranslateFiltering( WikiContext context, String htmlData ) throws FilterException { fireEvent( WikiPageEvent.POST_TRANSLATE_BEGIN, context ); for( Iterator i = m_pageFilters.iterator(); i.hasNext(); ) { PageFilter f = (PageFilter) i.next(); htmlData = f.postTranslate( context, htmlData ); } fireEvent( WikiPageEvent.POST_TRANSLATE_END, context ); return htmlData; } /** * Does the filtering before a save to the page repository. * * @param context The WikiContext * @param pageData WikiMarkup data to be passed through the preSave chain. * @throws FilterException If any of the filters throws a FilterException * @return The modified WikiMarkup * @see PageFilter#preSave(WikiContext, String) */ public String doPreSaveFiltering( WikiContext context, String pageData ) throws FilterException { fireEvent( WikiPageEvent.PRE_SAVE_BEGIN, context ); for( Iterator i = m_pageFilters.iterator(); i.hasNext(); ) { PageFilter f = (PageFilter) i.next(); pageData = f.preSave( context, pageData ); } fireEvent( WikiPageEvent.PRE_SAVE_END, context ); return pageData; } /** * Does the page filtering after the page has been saved. * * @param context The WikiContext * @param pageData WikiMarkup data to be passed through the postSave chain. * @throws FilterException If any of the filters throws a FilterException * * @see PageFilter#postSave(WikiContext, String) */ public void doPostSaveFiltering( WikiContext context, String pageData ) throws FilterException { fireEvent( WikiPageEvent.POST_SAVE_BEGIN, context ); for( Iterator i = m_pageFilters.iterator(); i.hasNext(); ) { PageFilter f = (PageFilter) i.next(); // log.info("POSTSAVE: "+f.toString() ); f.postSave( context, pageData ); } fireEvent( WikiPageEvent.POST_SAVE_END, context ); } /** * Returns the list of filters currently installed. Note that this is not * a copy, but the actual list. So be careful with it. * * @return A List of PageFilter objects */ public List getFilterList() { return m_pageFilters; } /** * * Notifies PageFilters to clean up their ressources. * */ public void destroy() { for( Iterator i = m_pageFilters.iterator(); i.hasNext(); ) { PageFilter f = (PageFilter) i.next(); f.destroy( m_engine ); } } // events processing ....................................................... /** * Fires a WikiPageEvent of the provided type and WikiContext. * Invalid WikiPageEvent types are ignored. * * @see com.ecyrd.jspwiki.event.WikiPageEvent * @param type the WikiPageEvent type to be fired. * @param context the WikiContext of the event. */ public final void fireEvent( int type, WikiContext context ) { if ( WikiEventManager.isListening(this) && WikiPageEvent.isValidType(type) ) { WikiEventManager.fireEvent(this, new WikiPageEvent(m_engine,type,context.getPage().getName()) ); } } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") public Collection modules() { ArrayList modules = new ArrayList(); modules.addAll( m_pageFilters ); return modules; } private void registerFilters() { log.info( "Registering filters" ); SAXBuilder builder = new SAXBuilder(); try { // // Register all filters which have created a resource containing its properties. // // Get all resources of all plugins. // Enumeration resources = getClass().getClassLoader().getResources( PLUGIN_RESOURCE_LOCATION ); while( resources.hasMoreElements() ) { URL resource = (URL) resources.nextElement(); try { log.debug( "Processing XML: " + resource ); Document doc = builder.build( resource ); List plugins = XPath.selectNodes( doc, "/modules/filter"); for( Iterator i = plugins.iterator(); i.hasNext(); ) { Element pluginEl = (Element) i.next(); String className = pluginEl.getAttributeValue("class"); PageFilterInfo pluginInfo = PageFilterInfo.newInstance( className, pluginEl ); if( pluginInfo != null ) { registerPlugin( pluginInfo ); } } } catch( java.io.IOException e ) { log.error( "Couldn't load " + PLUGIN_RESOURCE_LOCATION + " resources: " + resource, e ); } catch( JDOMException e ) { log.error( "Error parsing XML for filter: "+PLUGIN_RESOURCE_LOCATION ); } } } catch( java.io.IOException e ) { log.error( "Couldn't load all " + PLUGIN_RESOURCE_LOCATION + " resources", e ); } } private void registerPlugin(PageFilterInfo pluginInfo) { m_filterClassMap.put( pluginInfo.getName(), pluginInfo ); } /** * Stores information about the filters. * * @since 2.6.1 */ private static final class PageFilterInfo extends WikiModuleInfo { private PageFilterInfo( String name ) { super(name); } protected static PageFilterInfo newInstance(String className, Element pluginEl) { if( className == null || className.length() == 0 ) return null; PageFilterInfo info = new PageFilterInfo( className ); info.initializeFromXML( pluginEl ); return info; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -