/*********************************************************************************************************************************** * Copyright (c) 2000, 2004 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: IBM Corporation - initial API and implementation **********************************************************************************************************************************/ package net.sourceforge.phpeclipse.wiki.editor.model; import java.util.Stack; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; public class WikipediaOutlineParser { private IDocument fDocument; /** * Next line to read. */ private int fLine; private int fLineCount; public WikipediaSection parse(IDocument document) { try { fDocument = document; fLine = 0; fLineCount = fDocument.getNumberOfLines(); return parseWikipediaText(); } catch (BadLocationException e) { e.printStackTrace(); return null; } } private WikipediaSection parseWikipediaText() throws BadLocationException { Stack stack = new Stack(); char ch; WikipediaText wikiText = new WikipediaText(); WikipediaSection section; // int startOffset = -1; String textString; String headerString = null; char[] text; IRegion region = null; int headerStartOffset; int headerEndOffset; int headerLevelCounter; while (fLine < fLineCount) { region = fDocument.getLineInformation(fLine); textString = fDocument.get(region.getOffset(), region.getLength()); text = textString.toCharArray(); fLine++; headerStartOffset = 0; if (text.length >= 2 && text[headerStartOffset++] == '=') { headerEndOffset = text.length; while (headerEndOffset > 0 && Character.isWhitespace(text[--headerEndOffset])) { // } if (text[headerEndOffset] == '=') { // header section while (headerStartOffset < text.length && headerStartOffset < 6 && text[headerStartOffset++] == '=') { // } headerLevelCounter = 1; while (headerEndOffset > 0 && headerLevelCounter < headerStartOffset && text[--headerEndOffset] == '=') { headerLevelCounter++; } // if (!stack.isEmpty()) { // section = (WikipediaSection) stack.pop(); // section.setLength(region.getOffset() - section.getOffset() - 1); // wikiText.add(section); // } // headerString = new String(text, headerStartOffset - 1, headerEndOffset - headerStartOffset + 2); // stack.push(new WikipediaSection(wikiText, headerString, headerLevelCounter, region.getOffset(), 1)); if (headerStartOffset > 0 && (headerEndOffset - headerStartOffset + 2 > 0)) { headerString = new String(text, headerStartOffset - 1, headerEndOffset - headerStartOffset + 2); addSection(wikiText, new WikipediaSection(wikiText, headerString, headerLevelCounter, region.getOffset(), 1), stack, region); } } } } if (!stack.isEmpty()) { reduceSection(wikiText, stack, region); // section = (WikipediaSection) stack.pop(); // section.setLength(region.getOffset() - section.getOffset() - 1); // wikiText.add(section); } return wikiText; } private void addSection(WikipediaSection wikiText, WikipediaSection currentSection, Stack stack, IRegion region) { int level = currentSection.getHeaderLevel(); while (!stack.isEmpty()) { WikipediaSection section = (WikipediaSection) stack.peek(); if (section.getHeaderLevel() < level) { currentSection.setParent(section); section.add(currentSection); stack.push(currentSection); return; } else { // if (section.getHeaderLevel() <= level) { stack.pop(); section.setLength(region.getOffset() - section.getOffset() - 1); } } currentSection.setParent(wikiText); wikiText.add(currentSection); stack.push(currentSection); } private void reduceSection(WikipediaSection wikiText, Stack stack, IRegion region) { while (!stack.isEmpty()) { WikipediaSection section = (WikipediaSection) stack.peek(); stack.pop(); section.setLength(region.getOffset() - section.getOffset() - 1); } } }