001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.rendering.wiki;
023
024import java.io.IOException;
025import java.io.Reader;
026import java.io.Writer;
027import java.util.ArrayList;
028import java.util.HashMap;
029import java.util.List;
030import java.util.Map;
031
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034import org.wikimodel.wem.WikiParserException;
035import org.wikimodel.wem.common.CommonWikiParser;
036
037/**
038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
039 */
040public class WikiSerializer {
041
042    public static final Log log = LogFactory.getLog(WikiSerializer.class);
043
044    protected final CommonWikiParser parser;
045
046    protected final Map<String, WikiMacro> macros = new HashMap<String, WikiMacro>();
047
048    protected final List<WikiFilter> filters = new ArrayList<WikiFilter>();
049
050    public WikiSerializer() {
051        parser = new CommonWikiParser();
052        registerMacro(new TocMacro());
053    }
054
055    public void registerMacro(WikiMacro macro) {
056        macros.put(macro.getName(), macro);
057    }
058
059    public void addFilter(WikiFilter filter) {
060        filters.add(filter);
061    }
062
063    public void serialize(Reader reader, Writer writer) throws IOException, WikiParserException {
064        WikiSerializerHandler serializer = new WikiSerializerHandler(this);
065        parser.parse(reader, serializer);
066        serializer.getWriter().writeTo(serializer, writer);
067        writer.flush();
068    }
069
070}