
org.xwiki.rendering.internal.parser.xwiki10.XWikiParser Maven / Gradle / Ivy
/*
* 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.xwiki10;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
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.component.phase.Initializable;
import org.xwiki.component.phase.InitializationException;
import org.xwiki.rendering.block.XDOM;
import org.xwiki.rendering.internal.parser.XDOMGeneratorListener;
import org.xwiki.rendering.listener.Listener;
import org.xwiki.rendering.parser.ParseException;
import org.xwiki.rendering.parser.Parser;
import org.xwiki.rendering.parser.StreamParser;
import org.xwiki.rendering.parser.xwiki10.Filter;
import org.xwiki.rendering.parser.xwiki10.FilterContext;
import org.xwiki.rendering.parser.xwiki10.util.CleanUtil;
import org.xwiki.rendering.syntax.Syntax;
import org.xwiki.rendering.syntax.SyntaxType;
/**
* Convert XWiki 1.0 content into 2.0 content and call XWiki 2.0 parser to generate the XDOM.
*
* @version $Id: a01f2a8279317e5f6c44b072e3bd1b803a3c945b $
* @since 1.8M1
* @since 5.2M1 for the {@link StreamParser} support
*/
@Component
@Named("xwiki/1.0")
@Singleton
public class XWikiParser implements StreamParser, Parser, Initializable
{
/**
* The syntax identifier of the parser.
*/
private static final Syntax SYNTAX = new Syntax(SyntaxType.XWIKI, "1.0");
/**
* Use to create the XDOM from converted content.
*/
@Inject
@Named("xwiki/2.0")
private StreamParser xwiki20Parser;
/**
* The filters use to convert 1.0 content to 2.0.
*/
@Inject
private List filters;
@Override
public void initialize() throws InitializationException
{
// order filters
Collections.sort(this.filters, new Comparator()
{
@Override
public int compare(Filter filter1, Filter filter2)
{
return filter1.getPriority() - filter2.getPriority();
}
});
}
@Override
public Syntax getSyntax()
{
return SYNTAX;
}
@Override
public XDOM parse(Reader source) throws ParseException
{
XDOMGeneratorListener listener = new XDOMGeneratorListener();
parse(source, listener);
return listener.getXDOM();
}
/**
* Convert XWiki 1.0 content to 2.0.
*
* @param source the 1.0 source.
* @return the 2.0 converted content.
* @throws ParseException error when converting content.
*/
public String xwiki10To20(Reader source) throws ParseException
{
String content;
try {
content = IOUtils.toString(source);
} catch (IOException e) {
throw new ParseException("Failed to read source", e);
}
FilterContext filterContext = new FilterContext();
for (Filter filter : this.filters) {
content = filter.filter(content, filterContext);
}
content = filterContext.unProtect(content);
content = CleanUtil.removeLeadingNewLines(content);
content = CleanUtil.removeTrailingNewLines(content);
return content;
}
@Override
public void parse(Reader source, Listener listener) throws ParseException
{
// Convert from 1.0 syntax to 2.0 syntax
String content20 = xwiki10To20(source);
// Generate the XDOM using 2.0 syntax parser
this.xwiki20Parser.parse(new StringReader(content20), listener);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy