
org.xwiki.rendering.internal.parser.confluencexhtml.ConfluenceXHTMLParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xwiki-rendering-syntax-confluencexhtml Show documentation
Show all versions of xwiki-rendering-syntax-confluencexhtml Show documentation
XWiki Rendering - Syntax - Confluence XHTML
The newest version!
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.rendering.internal.parser.confluencexhtml;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.apache.commons.io.IOUtils;
import org.xwiki.component.annotation.Component;
import org.xwiki.rendering.internal.parser.confluencexhtml.wikimodel.AttachmentTagHandler;
import org.xwiki.rendering.internal.parser.confluencexhtml.wikimodel.ConfluenceXWikiGeneratorListener;
import org.xwiki.rendering.internal.parser.confluencexhtml.wikimodel.ImageTagHandler;
import org.xwiki.rendering.internal.parser.confluencexhtml.wikimodel.LinkTagHandler;
import org.xwiki.rendering.internal.parser.confluencexhtml.wikimodel.MacroTagHandler;
import org.xwiki.rendering.internal.parser.confluencexhtml.wikimodel.PageTagHandler;
import org.xwiki.rendering.internal.parser.confluencexhtml.wikimodel.ParameterTagHandler;
import org.xwiki.rendering.internal.parser.confluencexhtml.wikimodel.PlainTextBodyTagHandler;
import org.xwiki.rendering.internal.parser.confluencexhtml.wikimodel.PlainTextLinkBodyTagHandler;
import org.xwiki.rendering.internal.parser.confluencexhtml.wikimodel.RichTextBodyTagHandler;
import org.xwiki.rendering.internal.parser.confluencexhtml.wikimodel.SpaceTagHandler;
import org.xwiki.rendering.internal.parser.confluencexhtml.wikimodel.URLTagHandler;
import org.xwiki.rendering.internal.parser.confluencexhtml.wikimodel.UserTagHandler;
import org.xwiki.rendering.internal.parser.wikimodel.AbstractWikiModelParser;
import org.xwiki.rendering.internal.parser.wikimodel.XWikiGeneratorListener;
import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiCommentHandler;
import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiHeaderTagHandler;
import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiReferenceTagHandler;
import org.xwiki.rendering.listener.Listener;
import org.xwiki.rendering.parser.ParseException;
import org.xwiki.rendering.parser.ResourceReferenceParser;
import org.xwiki.rendering.parser.StreamParser;
import org.xwiki.rendering.renderer.PrintRendererFactory;
import org.xwiki.rendering.syntax.Syntax;
import org.xwiki.rendering.util.IdGenerator;
import org.xwiki.rendering.wikimodel.IWikiParser;
import org.xwiki.rendering.wikimodel.xhtml.XhtmlParser;
import org.xwiki.rendering.wikimodel.xhtml.handler.TagHandler;
/**
* Parses Confluence XHTML and generate rendering events.
*
* @version $Id: 7f6adc4a5a40a4136d150e56cd91803ca92f5944 $
* @since 5.3M2
*/
@Component
@Named("confluence+xhtml/1.0")
@Singleton
public class ConfluenceXHTMLParser extends AbstractWikiModelParser
{
/**
* The parser used for the link label parsing. For (x)html parsing, this will be an xwiki 2.0 parser, since it's
* more convenient to pass link labels in xwiki syntax. See referred resource for more details.
*
* @see XWikiCommentHandler#handleLinkCommentStop(String,
* org.xwiki.rendering.wikimodel.xhtml.impl.TagStack)
*/
@Inject
@Named("xdom+xml/current")
private StreamParser xmlParser;
@Inject
@Named("xdom+xml/current")
private PrintRendererFactory xmlRenderer;
/**
* @see #getLinkReferenceParser()
*/
@Inject
@Named("link")
private ResourceReferenceParser linkReferenceParser;
/**
* @see #getImageReferenceParser()
*/
@Inject
@Named("image")
private ResourceReferenceParser imageReferenceParser;
@Inject
@Named("plain/1.0")
private StreamParser plainParser;
@Override
public Syntax getSyntax()
{
return Syntax.CONFLUENCEXHTML_1_0;
}
@Override
public StreamParser getLinkLabelParser()
{
return this.xmlParser;
}
@Override
public IWikiParser createWikiModelParser() throws ParseException
{
XhtmlParser parser = new XhtmlParser();
parser.setNamespacesEnabled(false);
// Override some of the WikiModel XHTML parser tag handlers to introduce our own logic.
Map handlers = new HashMap();
TagHandler handler = new XWikiHeaderTagHandler();
handlers.put("h1", handler);
handlers.put("h2", handler);
handlers.put("h3", handler);
handlers.put("h4", handler);
handlers.put("h5", handler);
handlers.put("h6", handler);
handlers.put("a", new XWikiReferenceTagHandler(this, this.xmlRenderer));
handlers.put("ac:macro", new MacroTagHandler());
handlers.put("ac:structured-macro", new MacroTagHandler());
handlers.put("ac:parameter", new ParameterTagHandler());
handlers.put("ac:plain-text-body", new PlainTextBodyTagHandler());
handlers.put("ac:rich-text-body", new RichTextBodyTagHandler());
handlers.put("ac:image", new ImageTagHandler());
handlers.put("ri:url", new URLTagHandler());
handlers.put("ac:link", new LinkTagHandler());
handlers.put("ri:page", new PageTagHandler());
handlers.put("ri:space", new SpaceTagHandler());
handlers.put("ri:user", new UserTagHandler());
handlers.put("ac:plain-text-link-body", new PlainTextLinkBodyTagHandler());
handlers.put("ri:attachment", new AttachmentTagHandler());
parser.setExtraHandlers(handlers);
return parser;
}
@Override
protected void parse(final Reader source, Listener listener, IdGenerator idGenerator) throws ParseException
{
String content;
try {
content = IOUtils.toString(source);
} catch (IOException e) {
throw new ParseException("Failed to read source", e);
}
// Add element around the content to make sure to have valid xml
content = "" + content + " ";
// Add XHTML entities
content =
"" + content;
super.parse(new StringReader(content), listener, idGenerator);
}
@Override
public ResourceReferenceParser getLinkReferenceParser()
{
return this.linkReferenceParser;
}
@Override
public ResourceReferenceParser getImageReferenceParser()
{
return this.imageReferenceParser;
}
@Override
public XWikiGeneratorListener createXWikiGeneratorListener(Listener listener, IdGenerator idGenerator)
{
return new ConfluenceXWikiGeneratorListener(getLinkLabelParser(), listener, getLinkReferenceParser(),
getImageReferenceParser(), this.plainRendererFactory, idGenerator, getSyntax(), this.plainParser);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy