initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / editor / model / WikipediaOutlineParser.java
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
5  * 
6  * Contributors: IBM Corporation - initial API and implementation
7  **********************************************************************************************************************************/
8 package net.sourceforge.phpeclipse.wiki.editor.model;
9
10 import java.util.Stack;
11
12 import org.eclipse.jface.text.BadLocationException;
13 import org.eclipse.jface.text.IDocument;
14 import org.eclipse.jface.text.IRegion;
15
16 public class WikipediaOutlineParser {
17
18   private IDocument fDocument;
19
20   /**
21    * Next line to read.
22    */
23   private int fLine;
24
25   private int fLineCount;
26
27   public WikipediaSection parse(IDocument document) {
28     try {
29       fDocument = document;
30       fLine = 0;
31       fLineCount = fDocument.getNumberOfLines();
32       return parseWikipediaText();
33     } catch (BadLocationException e) {
34       e.printStackTrace();
35       return null;
36     }
37   }
38
39   private WikipediaSection parseWikipediaText() throws BadLocationException {
40     Stack stack = new Stack();
41     char ch;
42     WikipediaText wikiText = new WikipediaText();
43     WikipediaSection section;
44     //    int startOffset = -1;
45     String textString;
46     String headerString = null;
47     char[] text;
48     IRegion region = null;
49     int headerStartOffset;
50     int headerEndOffset;
51     int headerLevelCounter;
52
53     while (fLine < fLineCount) {
54       region = fDocument.getLineInformation(fLine);
55       textString = fDocument.get(region.getOffset(), region.getLength());
56       text = textString.toCharArray();
57       fLine++;
58
59       headerStartOffset = 0;
60       if (text.length >= 2 && text[headerStartOffset++] == '=') {
61         headerEndOffset = text.length;
62         while (headerEndOffset > 0 && Character.isWhitespace(text[--headerEndOffset])) {
63           //
64         }
65         if (text[headerEndOffset] == '=') {
66           // header section
67           while (headerStartOffset < text.length && headerStartOffset < 6 && text[headerStartOffset++] == '=') {
68             //
69           }
70           headerLevelCounter = 1;
71           while (headerEndOffset > 0 && headerLevelCounter < headerStartOffset && text[--headerEndOffset] == '=') {
72             headerLevelCounter++;
73           }
74
75           //          if (!stack.isEmpty()) {
76           //            section = (WikipediaSection) stack.pop();
77           //            section.setLength(region.getOffset() - section.getOffset() - 1);
78           //            wikiText.add(section);
79           //          }
80
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,
86                 region);
87
88           }
89
90         }
91       }
92     }
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);
98     }
99     return wikiText;
100   }
101
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);
110         return;
111       } else { // if (section.getHeaderLevel() <= level) {
112         stack.pop();
113         section.setLength(region.getOffset() - section.getOffset() - 1);
114       }
115     }
116     currentSection.setParent(wikiText);
117     wikiText.add(currentSection);
118     stack.push(currentSection);
119   }
120
121   private void reduceSection(WikipediaSection wikiText, Stack stack, IRegion region) {
122     while (!stack.isEmpty()) {
123       WikipediaSection section = (WikipediaSection) stack.peek();
124       stack.pop();
125       section.setLength(region.getOffset() - section.getOffset() - 1);
126     }
127   }
128 }