This is an automated email from the ASF dual-hosted git repository.

juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git

commit 5851edd12f623e4db3fd28941afbd29f5ef41197
Author: juanpablo <[email protected]>
AuthorDate: Mon Jan 13 00:16:46 2020 +0100

    extract text nodes extraction routine from Dom to XmlUtils
---
 .../apache/wiki/parser/JSPWikiMarkupParser.java    | 53 ++++++----------------
 .../org/apache/wiki/render/CleanTextRenderer.java  | 30 ++----------
 .../main/java/org/apache/wiki/util/XmlUtil.java    | 35 +++++++++++---
 .../java/org/apache/wiki/util/XmlUtilTest.java     | 17 +++++++
 4 files changed, 65 insertions(+), 70 deletions(-)

diff --git 
a/jspwiki-main/src/main/java/org/apache/wiki/parser/JSPWikiMarkupParser.java 
b/jspwiki-main/src/main/java/org/apache/wiki/parser/JSPWikiMarkupParser.java
index 7dad359..567e65c 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/parser/JSPWikiMarkupParser.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/parser/JSPWikiMarkupParser.java
@@ -38,8 +38,8 @@ import org.apache.wiki.auth.WikiSecurityException;
 import org.apache.wiki.auth.acl.Acl;
 import org.apache.wiki.i18n.InternationalizationManager;
 import org.apache.wiki.preferences.Preferences;
-import org.apache.wiki.render.CleanTextRenderer;
 import org.apache.wiki.util.TextUtil;
+import org.apache.wiki.util.XmlUtil;
 import org.jdom2.Attribute;
 import org.jdom2.Content;
 import org.jdom2.Element;
@@ -85,7 +85,7 @@ public class JSPWikiMarkupParser extends MarkupParser {
     protected static final int              IMAGEWIKILINK = 9;
     protected static final int              ATTACHMENT    = 10;
 
-    private static Logger log = Logger.getLogger( JSPWikiMarkupParser.class );
+    private static final Logger log = Logger.getLogger( 
JSPWikiMarkupParser.class );
 
     private boolean        m_isbold       = false;
     private boolean        m_isitalic     = false;
@@ -221,17 +221,12 @@ public class JSPWikiMarkupParser extends MarkupParser {
      *  @param text Text that should be passed to the mutate() method of each 
of the mutators in the chain.
      *  @return The result of the mutation.
      */
-    protected String callMutatorChain( Collection< StringTransmutator > list, 
String text )
-    {
-        if( list == null || list.size() == 0 )
-        {
+    protected String callMutatorChain( final Collection< StringTransmutator > 
list, String text ) {
+        if( list == null || list.size() == 0 ) {
             return text;
         }
 
-        for( Iterator< StringTransmutator > i = list.iterator(); i.hasNext(); )
-        {
-            StringTransmutator m = i.next();
-
+        for( final StringTransmutator m : list ) {
             text = m.mutate( m_context, text );
         }
 
@@ -808,19 +803,13 @@ public class JSPWikiMarkupParser extends MarkupParser {
     private JSPWikiMarkupParser m_cleanTranslator;
 
     /**
-     *  Does a lazy init.  Otherwise, we would get into a situation
-     *  where HTMLRenderer would try and boot a TranslatorReader before
+     *  Does a lazy init.  Otherwise, we would get into a situation where 
HTMLRenderer would try and boot a TranslatorReader before
      *  the TranslatorReader it is contained by is up.
      */
-    private JSPWikiMarkupParser getCleanTranslator()
-    {
-        if( m_cleanTranslator == null )
-        {
-            WikiContext dummyContext = new WikiContext( m_engine,
-                                                        
m_context.getHttpRequest(),
-                                                        m_context.getPage() );
+    private JSPWikiMarkupParser getCleanTranslator() {
+        if( m_cleanTranslator == null ) {
+            final WikiContext dummyContext = new WikiContext( m_engine, 
m_context.getHttpRequest(), m_context.getPage() );
             m_cleanTranslator = new JSPWikiMarkupParser( dummyContext, null );
-
             m_cleanTranslator.m_allowHTML = true;
         }
 
@@ -834,27 +823,20 @@ public class JSPWikiMarkupParser extends MarkupParser {
      *  Counts also duplicate headings (= headings with similar name), and
      *  attaches a counter.
      */
-    private String makeHeadingAnchor( String baseName, String title, Heading 
hd )
-    {
+    private String makeHeadingAnchor( final String baseName, String title, 
final Heading hd ) {
         hd.m_titleText = title;
         title = MarkupParser.wikifyLink( title );
-
         hd.m_titleSection = m_engine.encodeName(title);
 
-        if( m_titleSectionCounter.containsKey( hd.m_titleSection ) )
-        {
-            Integer count = m_titleSectionCounter.get( hd.m_titleSection );
-            count = count + 1;
+        if( m_titleSectionCounter.containsKey( hd.m_titleSection ) ) {
+            final Integer count = m_titleSectionCounter.get( hd.m_titleSection 
) + 1;
             m_titleSectionCounter.put( hd.m_titleSection, count );
             hd.m_titleSection += "-" + count;
-        }
-        else
-        {
+        } else {
             m_titleSectionCounter.put( hd.m_titleSection, 1 );
         }
 
-        hd.m_titleAnchor = "section-"+m_engine.encodeName(baseName)+
-                           "-"+hd.m_titleSection;
+        hd.m_titleAnchor = "section-" + m_engine.encodeName( baseName ) + "-" 
+ hd.m_titleSection;
         hd.m_titleAnchor = hd.m_titleAnchor.replace( '%', '_' );
         hd.m_titleAnchor = hd.m_titleAnchor.replace( '/', '_' );
 
@@ -863,20 +845,15 @@ public class JSPWikiMarkupParser extends MarkupParser {
 
     private String makeSectionTitle( String title ) {
         title = title.trim();
-        final String outTitle;
-
         try {
             final JSPWikiMarkupParser dtr = getCleanTranslator();
             dtr.setInputReader( new StringReader( title ) );
 
-            final CleanTextRenderer ctt = new CleanTextRenderer( m_context, 
dtr.parse() );
-            outTitle = ctt.getString();
+            return XmlUtil.extractTextFromDocument( dtr.parse() );
         } catch( final IOException e ) {
             log.fatal("CleanTranslator not working", e );
             throw new InternalWikiException( "CleanTranslator not working as 
expected, when cleaning title"+ e.getMessage() , e );
         }
-
-        return outTitle;
     }
 
     /**
diff --git 
a/jspwiki-main/src/main/java/org/apache/wiki/render/CleanTextRenderer.java 
b/jspwiki-main/src/main/java/org/apache/wiki/render/CleanTextRenderer.java
index 602f6ba..dec05ec 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/render/CleanTextRenderer.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/render/CleanTextRenderer.java
@@ -18,27 +18,18 @@
  */
 package org.apache.wiki.render;
 
-import org.apache.log4j.Logger;
 import org.apache.wiki.WikiContext;
 import org.apache.wiki.parser.WikiDocument;
-import org.jdom2.Text;
-import org.jdom2.xpath.XPathFactory;
-
-import java.io.IOException;
-import java.util.List;
+import org.apache.wiki.util.XmlUtil;
 
 
 /**
- *  A simple renderer that just renders all the text() nodes from the DOM tree.
- *  This is very useful for cleaning away all of the XHTML.
+ *  A simple renderer that just renders all the text() nodes from the DOM 
tree. This is very useful for cleaning away all of the XHTML.
  *
  *  @since  2.4
  */
 public class CleanTextRenderer extends WikiRenderer {
 
-    private static final String ALL_TEXT_NODES = "//text()";
-    private static final Logger log = Logger.getLogger( 
CleanTextRenderer.class );
-
     /**
      *  Create a renderer.
      *
@@ -52,21 +43,8 @@ public class CleanTextRenderer extends WikiRenderer {
     /**
      *  {@inheritDoc}
      */
-    public String getString() throws IOException {
-       final StringBuilder sb = new StringBuilder();
-        try {
-            final List< ? > nodes = XPathFactory.instance().compile( 
ALL_TEXT_NODES ).evaluate( m_document.getDocument() );
-            for( final Object el : nodes ) {
-                if( el instanceof Text ) {
-                    sb.append( ( ( Text )el ).getValue() );
-                }
-            }
-        } catch( final IllegalStateException e ) {
-            log.error("Could not parse XPATH expression");
-            throw new IOException( e.getMessage(), e );
-        }
-
-        return sb.toString();
+    public String getString() {
+       return m_document != null ? XmlUtil.extractTextFromDocument( 
m_document.getDocument() ) : "";
     }
 
 }
diff --git a/jspwiki-util/src/main/java/org/apache/wiki/util/XmlUtil.java 
b/jspwiki-util/src/main/java/org/apache/wiki/util/XmlUtil.java
index 2412b2c..1d0ef08 100644
--- a/jspwiki-util/src/main/java/org/apache/wiki/util/XmlUtil.java
+++ b/jspwiki-util/src/main/java/org/apache/wiki/util/XmlUtil.java
@@ -24,6 +24,7 @@ import org.apache.log4j.Logger;
 import org.jdom2.Document;
 import org.jdom2.Element;
 import org.jdom2.JDOMException;
+import org.jdom2.Text;
 import org.jdom2.filter.Filters;
 import org.jdom2.input.SAXBuilder;
 import org.jdom2.xpath.XPathExpression;
@@ -49,7 +50,8 @@ import java.util.Set;
  */
 public final class XmlUtil  {
 
-       private static final Logger log = Logger.getLogger( XmlUtil.class );
+       private static final String ALL_TEXT_NODES = "//text()";
+       private static final Logger LOG = Logger.getLogger( XmlUtil.class );
        private XmlUtil() {}
        
        /**
@@ -69,7 +71,7 @@ public final class XmlUtil  {
                                final Enumeration< URL > resources = 
XmlUtil.class.getClassLoader().getResources( xml );
                                while( resources.hasMoreElements() ) {
                                        final URL resource = 
resources.nextElement();
-                       log.debug( "reading " + resource.toString() );
+                                       LOG.debug( "reading " + 
resource.toString() );
                                        final Document doc = builder.build( 
resource );
                                        final XPathFactory xpfac = 
XPathFactory.instance();
                                        final XPathExpression<Element> xp = 
xpfac.compile( requestedNodes, Filters.element() );
@@ -77,9 +79,9 @@ public final class XmlUtil  {
                    }
                                return new ArrayList<>( readed );
                        } catch( final IOException ioe ) {
-                               log.error( "Couldn't load all " + xml + " 
resources", ioe );
+                               LOG.error( "Couldn't load all " + xml + " 
resources", ioe );
                        } catch( final JDOMException jdome ) {
-                               log.error( "error parsing " + xml + " 
resources", jdome );
+                               LOG.error( "error parsing " + xml + " 
resources", jdome );
                        }
                }
                return Collections.emptyList();
@@ -102,12 +104,33 @@ public final class XmlUtil  {
                                final XPathExpression< Element > xp = 
xpfac.compile( requestedNodes,Filters.element() );
                                return xp.evaluate( doc );
                        } catch( final IOException ioe ) {
-                               log.error( "Couldn't load all " + xmlStream + " 
resources", ioe );
+                               LOG.error( "Couldn't load all " + xmlStream + " 
resources", ioe );
                        } catch( final JDOMException jdome ) {
-                               log.error( "error parsing " + xmlStream + " 
resources", jdome );
+                               LOG.error( "error parsing " + xmlStream + " 
resources", jdome );
                        }
                }               
                return Collections.emptyList();
        }
 
+       /**
+        * Renders all the text() nodes from the DOM tree. This is very useful 
for cleaning away all of the XHTML.
+        *
+        * @param doc Dom tree
+        * @return String containing only the text from the provided Dom tree.
+        */
+       public static String extractTextFromDocument( final Document doc ) {
+               if( doc == null ) {
+                       return "";
+               }
+               final StringBuilder sb = new StringBuilder();
+               final List< ? > nodes = XPathFactory.instance().compile( 
ALL_TEXT_NODES ).evaluate( doc );
+               for( final Object el : nodes ) {
+                       if( el instanceof Text ) {
+                               sb.append( ( ( Text )el ).getValue() );
+                       }
+               }
+
+               return sb.toString();
+       }
+
 }
\ No newline at end of file
diff --git a/jspwiki-util/src/test/java/org/apache/wiki/util/XmlUtilTest.java 
b/jspwiki-util/src/test/java/org/apache/wiki/util/XmlUtilTest.java
index cefc0e9..68aa485 100644
--- a/jspwiki-util/src/test/java/org/apache/wiki/util/XmlUtilTest.java
+++ b/jspwiki-util/src/test/java/org/apache/wiki/util/XmlUtilTest.java
@@ -18,7 +18,9 @@
  */
 package org.apache.wiki.util;
 
+import org.jdom2.Document;
 import org.jdom2.Element;
+import org.jdom2.input.SAXBuilder;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
@@ -80,4 +82,19 @@ public class XmlUtilTest {
         }
     }
 
+    @Test
+    public void testExtractTestFrom() throws Exception {
+        Assertions.assertEquals( "", XmlUtil.extractTextFromDocument( null ) );
+        final SAXBuilder builder = new SAXBuilder();
+        try( final InputStream is = new FileInputStream( new File 
("./src/test/resources/ini/classmappings.xml" ) ) ) {
+            final Document doc = builder.build( is );
+            final String text = XmlUtil.extractTextFromDocument( doc );
+            Assertions.assertEquals( "\n" +
+                                     "  \n" +
+                                     "    java.util.List\n" +
+                                     "    java.util.ArrayList\n" +
+                                     "  \n", text );
+        }
+    }
+
 }

Reply via email to