1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.wiki;
29 
30 import javax.swing.text.html.*;
31 import javax.swing.text.MutableAttributeSet;
32 
33 public class EditPageParser extends HTMLEditorKit.ParserCallback
34 {
35 
36     protected String m_sEditTime = "";
37     protected String m_sEditToken = "";
38     protected String m_sLoginToken = "";
39     protected String m_sMainURL = "";
40 
41     private int m_nWikiArticleHash = 0;
42     private boolean m_bHTMLStartFound = false;
43     private boolean m_bInHead = false;
44 
45     protected int m_nWikiArticleStart = -1;
46     protected int m_nWikiArticleEnd = -1;
47     protected int m_nHTMLArticleStart = -1;
48     protected int m_nHTMLArticleEnd = -1;
49     protected int m_nNoArticleInd = -1;
50     protected int m_nErrorInd = -1;
51 
52     /** Creates a new instance of WikiHTMLParser */
53     public EditPageParser()
54     {
55     }
56 
57     public void handleComment( char[] data,int pos )
58     {
59         // insert code to handle comments
60     }
61 
62     public void handleEndTag( HTML.Tag t,int pos )
63     {
64         if ( t == HTML.Tag.TEXTAREA )
65         {
66             m_nWikiArticleEnd = pos;
67         }
68         else if ( t == HTML.Tag.DIV )
69         {
70             if ( m_bHTMLStartFound )
71             {
72                 m_nHTMLArticleStart = pos+6;
73                 m_bHTMLStartFound = false;
74             }
75         }
76         else if ( t == HTML.Tag.HEAD )
77         {
78             m_bInHead = false;
79         }
80     }
81 
82     public void handleError( String errorMsg,int pos )
83     {
84         //System.out.println( errorMsg );
85     }
86 
87     public void handleSimpleTag( HTML.Tag t, MutableAttributeSet a,int pos )
88     {
89         // insert code to handle simple tags
90 
91         if ( t == HTML.Tag.INPUT )
92         {
93             String sName = ( String ) a.getAttribute( HTML.Attribute.NAME );
94             if ( sName != null )
95             {
96                 if ( sName.equalsIgnoreCase( "wpEdittime" ) )
97                 {
98                     this.m_sEditTime = ( String ) a.getAttribute( HTML.Attribute.VALUE );
99                 }
100                 else if ( sName.equalsIgnoreCase( "wpEditToken" ) )
101                 {
102                     this.m_sEditToken = ( String ) a.getAttribute( HTML.Attribute.VALUE );
103                 }
104                 else if ( sName.equalsIgnoreCase( "wpLoginToken" ) )
105                 {
106                     this.m_sLoginToken = ( String ) a.getAttribute( HTML.Attribute.VALUE );
107                 }
108             }
109 
110         }
111         else if ( t == HTML.Tag.LINK )
112         {
113             if ( m_bInHead )
114             {
115                 String sName = ( String ) a.getAttribute( HTML.Attribute.HREF );
116                 if ( sName != null )
117                 {
118                     int nIndexStart = sName.indexOf( "index.php" );
119                     // get the main URL from the first header-link with index.php
120                     // the link with "action=edit" inside is preferable
121                     if ( nIndexStart>= 0
122                       && ( m_sMainURL.length() == 0 || sName.indexOf( "action=edit" ) >= 0 ) )
123                     {
124                         m_sMainURL = sName.substring( 0, nIndexStart );
125                     }
126                 }
127             }
128         }
129 
130     }
131 
132     public void handleStartTag( HTML.Tag t, MutableAttributeSet a,int pos )
133     {
134         // insert code to handle starting tags
135         String sName = "";
136         String sId = "";
137         String sClass = "";
138 
139         if ( t == HTML.Tag.HEAD )
140         {
141             m_bInHead = true;
142         }
143         if ( t == HTML.Tag.TEXTAREA )
144         {
145             sName = ( String ) a.getAttribute( HTML.Attribute.NAME );
146             if ( sName != null )
147             {
148                 if ( sName.equalsIgnoreCase( "wpTextbox1" ) )
149                 {
150                     m_nWikiArticleHash = t.hashCode();
151                     m_nWikiArticleStart = pos;
152                 }
153             }
154         }
155         else if ( t == HTML.Tag.DIV )
156         {
157             sId = ( String ) a.getAttribute( HTML.Attribute.ID );
158             sClass = ( String ) a.getAttribute( HTML.Attribute.CLASS );
159             if ( sId != null )
160             {
161                 if ( sId.equalsIgnoreCase( "contentSub" ) )
162                 {
163                     m_bHTMLStartFound = true;
164                 }
165             }
166             if ( sClass != null )
167             {
168                 if ( sClass.equalsIgnoreCase( "printfooter" ) )
169                 {
170                     m_nHTMLArticleEnd = pos;
171                 }
172                 else if ( sClass.equalsIgnoreCase( "noarticletext" ) )
173                 {
174                     m_nNoArticleInd = pos;
175                 }
176                 else if ( sClass.equalsIgnoreCase( "errorbox" ) )
177                 {
178                     m_nErrorInd = pos;
179                 }
180             }
181         }
182         else if ( t == HTML.Tag.P )
183         {
184             sClass = ( String ) a.getAttribute( HTML.Attribute.CLASS );
185             if ( sClass != null && sClass.equalsIgnoreCase( "error" ) )
186             {
187                 m_nErrorInd = pos;
188             }
189         }
190     }
191 
192 
193 }
194