1*13efc523SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*13efc523SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*13efc523SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*13efc523SAndrew Rist  * distributed with this work for additional information
6*13efc523SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*13efc523SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*13efc523SAndrew Rist  * "License"); you may not use this file except in compliance
9*13efc523SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*13efc523SAndrew Rist  *
11*13efc523SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*13efc523SAndrew Rist  *
13*13efc523SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*13efc523SAndrew Rist  * software distributed under the License is distributed on an
15*13efc523SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*13efc523SAndrew Rist  * KIND, either express or implied.  See the License for the
17*13efc523SAndrew Rist  * specific language governing permissions and limitations
18*13efc523SAndrew Rist  * under the License.
19*13efc523SAndrew Rist  *
20*13efc523SAndrew Rist  *************************************************************/
21*13efc523SAndrew Rist 
22*13efc523SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package com.sun.star.wiki;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import javax.swing.text.html.*;
27cdf0e10cSrcweir import javax.swing.text.MutableAttributeSet;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir public class EditPageParser extends HTMLEditorKit.ParserCallback
30cdf0e10cSrcweir {
31cdf0e10cSrcweir 
32cdf0e10cSrcweir     protected String m_sEditTime = "";
33cdf0e10cSrcweir     protected String m_sEditToken = "";
34cdf0e10cSrcweir     protected String m_sLoginToken = "";
35cdf0e10cSrcweir     protected String m_sMainURL = "";
36cdf0e10cSrcweir 
37cdf0e10cSrcweir     private int m_nWikiArticleHash = 0;
38cdf0e10cSrcweir     private boolean m_bHTMLStartFound = false;
39cdf0e10cSrcweir     private boolean m_bInHead = false;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir     protected int m_nWikiArticleStart = -1;
42cdf0e10cSrcweir     protected int m_nWikiArticleEnd = -1;
43cdf0e10cSrcweir     protected int m_nHTMLArticleStart = -1;
44cdf0e10cSrcweir     protected int m_nHTMLArticleEnd = -1;
45cdf0e10cSrcweir     protected int m_nNoArticleInd = -1;
46cdf0e10cSrcweir     protected int m_nErrorInd = -1;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir     /** Creates a new instance of WikiHTMLParser */
EditPageParser()49cdf0e10cSrcweir     public EditPageParser()
50cdf0e10cSrcweir     {
51cdf0e10cSrcweir     }
52cdf0e10cSrcweir 
handleComment( char[] data,int pos )53cdf0e10cSrcweir     public void handleComment( char[] data,int pos )
54cdf0e10cSrcweir     {
55cdf0e10cSrcweir         // insert code to handle comments
56cdf0e10cSrcweir     }
57cdf0e10cSrcweir 
handleEndTag( HTML.Tag t,int pos )58cdf0e10cSrcweir     public void handleEndTag( HTML.Tag t,int pos )
59cdf0e10cSrcweir     {
60cdf0e10cSrcweir         if ( t == HTML.Tag.TEXTAREA )
61cdf0e10cSrcweir         {
62cdf0e10cSrcweir             m_nWikiArticleEnd = pos;
63cdf0e10cSrcweir         }
64cdf0e10cSrcweir         else if ( t == HTML.Tag.DIV )
65cdf0e10cSrcweir         {
66cdf0e10cSrcweir             if ( m_bHTMLStartFound )
67cdf0e10cSrcweir             {
68cdf0e10cSrcweir                 m_nHTMLArticleStart = pos+6;
69cdf0e10cSrcweir                 m_bHTMLStartFound = false;
70cdf0e10cSrcweir             }
71cdf0e10cSrcweir         }
72cdf0e10cSrcweir         else if ( t == HTML.Tag.HEAD )
73cdf0e10cSrcweir         {
74cdf0e10cSrcweir             m_bInHead = false;
75cdf0e10cSrcweir         }
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
handleError( String errorMsg,int pos )78cdf0e10cSrcweir     public void handleError( String errorMsg,int pos )
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         //System.out.println( errorMsg );
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir 
handleSimpleTag( HTML.Tag t, MutableAttributeSet a,int pos )83cdf0e10cSrcweir     public void handleSimpleTag( HTML.Tag t, MutableAttributeSet a,int pos )
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir         // insert code to handle simple tags
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         if ( t == HTML.Tag.INPUT )
88cdf0e10cSrcweir         {
89cdf0e10cSrcweir             String sName = ( String ) a.getAttribute( HTML.Attribute.NAME );
90cdf0e10cSrcweir             if ( sName != null )
91cdf0e10cSrcweir             {
92cdf0e10cSrcweir                 if ( sName.equalsIgnoreCase( "wpEdittime" ) )
93cdf0e10cSrcweir                 {
94cdf0e10cSrcweir                     this.m_sEditTime = ( String ) a.getAttribute( HTML.Attribute.VALUE );
95cdf0e10cSrcweir                 }
96cdf0e10cSrcweir                 else if ( sName.equalsIgnoreCase( "wpEditToken" ) )
97cdf0e10cSrcweir                 {
98cdf0e10cSrcweir                     this.m_sEditToken = ( String ) a.getAttribute( HTML.Attribute.VALUE );
99cdf0e10cSrcweir                 }
100cdf0e10cSrcweir                 else if ( sName.equalsIgnoreCase( "wpLoginToken" ) )
101cdf0e10cSrcweir                 {
102cdf0e10cSrcweir                     this.m_sLoginToken = ( String ) a.getAttribute( HTML.Attribute.VALUE );
103cdf0e10cSrcweir                 }
104cdf0e10cSrcweir             }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         }
107cdf0e10cSrcweir         else if ( t == HTML.Tag.LINK )
108cdf0e10cSrcweir         {
109cdf0e10cSrcweir             if ( m_bInHead )
110cdf0e10cSrcweir             {
111cdf0e10cSrcweir                 String sName = ( String ) a.getAttribute( HTML.Attribute.HREF );
112cdf0e10cSrcweir                 if ( sName != null )
113cdf0e10cSrcweir                 {
114cdf0e10cSrcweir                     int nIndexStart = sName.indexOf( "index.php" );
115cdf0e10cSrcweir                     // get the main URL from the first header-link with index.php
116cdf0e10cSrcweir                     // the link with "action=edit" inside is preferable
117cdf0e10cSrcweir                     if ( nIndexStart>= 0
118cdf0e10cSrcweir                       && ( m_sMainURL.length() == 0 || sName.indexOf( "action=edit" ) >= 0 ) )
119cdf0e10cSrcweir                     {
120cdf0e10cSrcweir                         m_sMainURL = sName.substring( 0, nIndexStart );
121cdf0e10cSrcweir                     }
122cdf0e10cSrcweir                 }
123cdf0e10cSrcweir             }
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     }
127cdf0e10cSrcweir 
handleStartTag( HTML.Tag t, MutableAttributeSet a,int pos )128cdf0e10cSrcweir     public void handleStartTag( HTML.Tag t, MutableAttributeSet a,int pos )
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir         // insert code to handle starting tags
131cdf0e10cSrcweir         String sName = "";
132cdf0e10cSrcweir         String sId = "";
133cdf0e10cSrcweir         String sClass = "";
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         if ( t == HTML.Tag.HEAD )
136cdf0e10cSrcweir         {
137cdf0e10cSrcweir             m_bInHead = true;
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir         if ( t == HTML.Tag.TEXTAREA )
140cdf0e10cSrcweir         {
141cdf0e10cSrcweir             sName = ( String ) a.getAttribute( HTML.Attribute.NAME );
142cdf0e10cSrcweir             if ( sName != null )
143cdf0e10cSrcweir             {
144cdf0e10cSrcweir                 if ( sName.equalsIgnoreCase( "wpTextbox1" ) )
145cdf0e10cSrcweir                 {
146cdf0e10cSrcweir                     m_nWikiArticleHash = t.hashCode();
147cdf0e10cSrcweir                     m_nWikiArticleStart = pos;
148cdf0e10cSrcweir                 }
149cdf0e10cSrcweir             }
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir         else if ( t == HTML.Tag.DIV )
152cdf0e10cSrcweir         {
153cdf0e10cSrcweir             sId = ( String ) a.getAttribute( HTML.Attribute.ID );
154cdf0e10cSrcweir             sClass = ( String ) a.getAttribute( HTML.Attribute.CLASS );
155cdf0e10cSrcweir             if ( sId != null )
156cdf0e10cSrcweir             {
157cdf0e10cSrcweir                 if ( sId.equalsIgnoreCase( "contentSub" ) )
158cdf0e10cSrcweir                 {
159cdf0e10cSrcweir                     m_bHTMLStartFound = true;
160cdf0e10cSrcweir                 }
161cdf0e10cSrcweir             }
162cdf0e10cSrcweir             if ( sClass != null )
163cdf0e10cSrcweir             {
164cdf0e10cSrcweir                 if ( sClass.equalsIgnoreCase( "printfooter" ) )
165cdf0e10cSrcweir                 {
166cdf0e10cSrcweir                     m_nHTMLArticleEnd = pos;
167cdf0e10cSrcweir                 }
168cdf0e10cSrcweir                 else if ( sClass.equalsIgnoreCase( "noarticletext" ) )
169cdf0e10cSrcweir                 {
170cdf0e10cSrcweir                     m_nNoArticleInd = pos;
171cdf0e10cSrcweir                 }
172cdf0e10cSrcweir                 else if ( sClass.equalsIgnoreCase( "errorbox" ) )
173cdf0e10cSrcweir                 {
174cdf0e10cSrcweir                     m_nErrorInd = pos;
175cdf0e10cSrcweir                 }
176cdf0e10cSrcweir             }
177cdf0e10cSrcweir         }
178cdf0e10cSrcweir         else if ( t == HTML.Tag.P )
179cdf0e10cSrcweir         {
180cdf0e10cSrcweir             sClass = ( String ) a.getAttribute( HTML.Attribute.CLASS );
181cdf0e10cSrcweir             if ( sClass != null && sClass.equalsIgnoreCase( "error" ) )
182cdf0e10cSrcweir             {
183cdf0e10cSrcweir                 m_nErrorInd = pos;
184cdf0e10cSrcweir             }
185cdf0e10cSrcweir         }
186cdf0e10cSrcweir     }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 
189cdf0e10cSrcweir }
190