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