1 /***********************************************************************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made
3 * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4 * http://www.eclipse.org/legal/cpl-v10.html
6 * Contributors: IBM Corporation - initial API and implementation
7 **********************************************************************************************************************************/
8 package net.sourceforge.phpeclipse.wiki.editor.model;
10 import java.util.Stack;
12 import org.eclipse.jface.text.BadLocationException;
13 import org.eclipse.jface.text.IDocument;
14 import org.eclipse.jface.text.IRegion;
16 public class WikipediaOutlineParser {
18 private IDocument fDocument;
25 private int fLineCount;
27 public WikipediaSection parse(IDocument document) {
31 fLineCount = fDocument.getNumberOfLines();
32 return parseWikipediaText();
33 } catch (BadLocationException e) {
39 private WikipediaSection parseWikipediaText() throws BadLocationException {
40 Stack stack = new Stack();
42 WikipediaText wikiText = new WikipediaText();
43 WikipediaSection section;
44 // int startOffset = -1;
46 String headerString = null;
48 IRegion region = null;
49 int headerStartOffset;
51 int headerLevelCounter;
53 while (fLine < fLineCount) {
54 region = fDocument.getLineInformation(fLine);
55 textString = fDocument.get(region.getOffset(), region.getLength());
56 text = textString.toCharArray();
59 headerStartOffset = 0;
60 if (text.length >= 2 && text[headerStartOffset++] == '=') {
61 headerEndOffset = text.length;
62 while (headerEndOffset > 0 && Character.isWhitespace(text[--headerEndOffset])) {
65 if (text[headerEndOffset] == '=') {
67 while (headerStartOffset < text.length && headerStartOffset < 6 && text[headerStartOffset++] == '=') {
70 headerLevelCounter = 1;
71 while (headerEndOffset > 0 && headerLevelCounter < headerStartOffset && text[--headerEndOffset] == '=') {
75 // if (!stack.isEmpty()) {
76 // section = (WikipediaSection) stack.pop();
77 // section.setLength(region.getOffset() - section.getOffset() - 1);
78 // wikiText.add(section);
81 // headerString = new String(text, headerStartOffset - 1, headerEndOffset - headerStartOffset + 2);
82 // stack.push(new WikipediaSection(wikiText, headerString, headerLevelCounter, region.getOffset(), 1));
83 if (headerStartOffset > 0 && (headerEndOffset - headerStartOffset + 2 > 0)) {
84 headerString = new String(text, headerStartOffset - 1, headerEndOffset - headerStartOffset + 2);
85 addSection(wikiText, new WikipediaSection(wikiText, headerString, headerLevelCounter, region.getOffset(), 1), stack,
93 if (!stack.isEmpty()) {
94 reduceSection(wikiText, stack, region);
95 // section = (WikipediaSection) stack.pop();
96 // section.setLength(region.getOffset() - section.getOffset() - 1);
97 // wikiText.add(section);
102 private void addSection(WikipediaSection wikiText, WikipediaSection currentSection, Stack stack, IRegion region) {
103 int level = currentSection.getHeaderLevel();
104 while (!stack.isEmpty()) {
105 WikipediaSection section = (WikipediaSection) stack.peek();
106 if (section.getHeaderLevel() < level) {
107 currentSection.setParent(section);
108 section.add(currentSection);
109 stack.push(currentSection);
111 } else { // if (section.getHeaderLevel() <= level) {
113 section.setLength(region.getOffset() - section.getOffset() - 1);
116 currentSection.setParent(wikiText);
117 wikiText.add(currentSection);
118 stack.push(currentSection);
121 private void reduceSection(WikipediaSection wikiText, Stack stack, IRegion region) {
122 while (!stack.isEmpty()) {
123 WikipediaSection section = (WikipediaSection) stack.peek();
125 section.setLength(region.getOffset() - section.getOffset() - 1);