package org.apache.maven.doxia.module.confluence.parser.list; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import org.apache.maven.doxia.util.ByLineSource; import org.apache.maven.doxia.module.confluence.ConfluenceMarkup; import org.apache.maven.doxia.module.confluence.parser.Block; import org.apache.maven.doxia.module.confluence.parser.BlockParser; import org.apache.maven.doxia.parser.ParseException; /** * @author Jason van Zyl * @version $Id: ListBlockParser.java 709605 2008-10-31 23:57:03Z hboutemy $ */ public class ListBlockParser implements BlockParser { public static final int BULLETED_LIST = 0; public static final int NUMBERED_LIST = 1; /** {@inheritDoc} */ public boolean accept(String line, ByLineSource source) { if (isList(line)) { return true; } return false; } /** {@inheritDoc} */ public Block visit(String line, ByLineSource source) throws ParseException { TreeListBuilder treeListBuilder = new TreeListBuilder(); StringBuffer text = new StringBuffer(); do { if (line.trim().length() == 0) { break; } if (text.length() > 0 && isList(line)) { String item = text.toString(); treeListBuilder.feedEntry(getPrefix(item), item.substring( getPrefix(item).length()).trim()); text.setLength(0); } if (text.length() == 0) { text.append(line.trim()); } else { text.append(ConfluenceMarkup.SPACE + line.trim()); } } while ((line = source.getNextLine()) != null); if (text.length() > 0) { String item = text.toString(); treeListBuilder.feedEntry(getPrefix(item), item.substring( getPrefix(item).length()).trim()); } return treeListBuilder.getBlock(); } private boolean isList(String line) { line = line.trim(); char bold = ConfluenceMarkup.BOLD_START_MARKUP.charAt(0); char dash = ConfluenceMarkup.DASH.charAt(0); char hash = ConfluenceMarkup.HASH_MARKUP.charAt(0); if (line.startsWith(ConfluenceMarkup.BOLD_START_MARKUP) || line.startsWith(ConfluenceMarkup.DASH) || line.startsWith(ConfluenceMarkup.HASH_MARKUP)) { String temp = line.substring(1); while (temp.charAt(0) == bold || temp.charAt(0) == dash || temp.charAt(0) == hash) temp = temp.substring(1); if (temp.charAt(0) == ConfluenceMarkup.SPACE.charAt(0)) return true; } return false; } private String getPrefix(String line) { int i = 0; while (i < line.length() && (line.charAt(i) == ConfluenceMarkup.DASH.toCharArray()[0] || line.charAt(i) == ConfluenceMarkup.BOLD_START_MARKUP .toCharArray()[0] || line.charAt(i) == ConfluenceMarkup.HASH_MARKUP .toCharArray()[0])) i++; return line.substring(0, i); } }