| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 4 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 10 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 11 |
* contributor license agreements. See the NOTICE file distributed |
| 12 |
* with this work for additional information regarding copyright |
| 13 |
* ownership. The ASF licenses this file to you under the Apache |
| 14 |
* License, Version 2.0 (the "License"); you may not use this file |
| 15 |
* except in compliance with the License. You may obtain a copy of |
| 16 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 17 |
*/ |
| 18 |
|
| 19 |
package com.sun.star.wiki; |
| 20 |
|
| 21 |
import javax.swing.text.html.*; |
| 22 |
import javax.swing.text.MutableAttributeSet; |
| 23 |
|
| 24 |
public class EditPageParser extends HTMLEditorKit.ParserCallback |
| 25 |
{ |
| 26 |
|
| 27 |
protected String m_sEditTime = ""; |
| 28 |
protected String m_sEditToken = ""; |
| 29 |
protected String m_sLoginToken = ""; |
| 30 |
protected String m_sMainURL = ""; |
| 31 |
|
| 32 |
private boolean m_bHTMLStartFound = false; |
| 33 |
private boolean m_bInHead = false; |
| 34 |
|
| 35 |
protected int m_nWikiArticleStart = -1; |
| 36 |
protected int m_nWikiArticleEnd = -1; |
| 37 |
protected int m_nHTMLArticleStart = -1; |
| 38 |
protected int m_nHTMLArticleEnd = -1; |
| 39 |
protected int m_nNoArticleInd = -1; |
| 40 |
protected int m_nErrorInd = -1; |
| 41 |
|
| 42 |
@Override |
| 43 |
public void handleComment( char[] data,int pos ) |
| 44 |
{ |
| 45 |
// insert code to handle comments |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public void handleEndTag( HTML.Tag t,int pos ) |
| 50 |
{ |
| 51 |
if ( t == HTML.Tag.TEXTAREA ) |
| 52 |
{ |
| 53 |
m_nWikiArticleEnd = pos; |
| 54 |
} |
| 55 |
else if ( t == HTML.Tag.DIV ) |
| 56 |
{ |
| 57 |
if ( m_bHTMLStartFound ) |
| 58 |
{ |
| 59 |
m_nHTMLArticleStart = pos+6; |
| 60 |
m_bHTMLStartFound = false; |
| 61 |
} |
| 62 |
} |
| 63 |
else if ( t == HTML.Tag.HEAD ) |
| 64 |
{ |
| 65 |
m_bInHead = false; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public void handleError( String errorMsg,int pos ) |
| 71 |
{ |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void handleSimpleTag( HTML.Tag t, MutableAttributeSet a,int pos ) |
| 76 |
{ |
| 77 |
// insert code to handle simple tags |
| 78 |
|
| 79 |
if ( t == HTML.Tag.INPUT ) |
| 80 |
{ |
| 81 |
String sName = ( String ) a.getAttribute( HTML.Attribute.NAME ); |
| 82 |
if ( sName != null ) |
| 83 |
{ |
| 84 |
if ( sName.equalsIgnoreCase( "wpEdittime" ) ) |
| 85 |
{ |
| 86 |
this.m_sEditTime = ( String ) a.getAttribute( HTML.Attribute.VALUE ); |
| 87 |
} |
| 88 |
else if ( sName.equalsIgnoreCase( "wpEditToken" ) ) |
| 89 |
{ |
| 90 |
this.m_sEditToken = ( String ) a.getAttribute( HTML.Attribute.VALUE ); |
| 91 |
} |
| 92 |
else if ( sName.equalsIgnoreCase( "wpLoginToken" ) ) |
| 93 |
{ |
| 94 |
this.m_sLoginToken = ( String ) a.getAttribute( HTML.Attribute.VALUE ); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
else if ( t == HTML.Tag.LINK ) |
| 100 |
{ |
| 101 |
if ( m_bInHead ) |
| 102 |
{ |
| 103 |
String sName = ( String ) a.getAttribute( HTML.Attribute.HREF ); |
| 104 |
if ( sName != null ) |
| 105 |
{ |
| 106 |
// get the main URL from the first header-link with load.php (which is used for stylesheet) |
| 107 |
int nPhpFileStart = sName.indexOf( "load.php" ); |
| 108 |
if (nPhpFileStart < 0) |
| 109 |
// if not found, try header-link with opensearch_desc.php |
| 110 |
nPhpFileStart = sName.indexOf( "opensearch_desc.php" ); |
| 111 |
|
| 112 |
if ( nPhpFileStart >= 0 |
| 113 |
&& m_sMainURL.length() == 0 ) |
| 114 |
{ |
| 115 |
m_sMainURL = sName.substring( 0, nPhpFileStart ); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public void handleStartTag( HTML.Tag t, MutableAttributeSet a,int pos ) |
| 125 |
{ |
| 126 |
// insert code to handle starting tags |
| 127 |
String sClass; |
| 128 |
|
| 129 |
if ( t == HTML.Tag.HEAD ) |
| 130 |
{ |
| 131 |
m_bInHead = true; |
| 132 |
} |
| 133 |
else if ( t == HTML.Tag.TEXTAREA ) |
| 134 |
{ |
| 135 |
String sName = ( String ) a.getAttribute( HTML.Attribute.NAME ); |
| 136 |
if ( sName != null && sName.equalsIgnoreCase( "wpTextbox1" ) ) |
| 137 |
{ |
| 138 |
m_nWikiArticleStart = pos; |
| 139 |
} |
| 140 |
} |
| 141 |
else if ( t == HTML.Tag.DIV ) |
| 142 |
{ |
| 143 |
String sId = ( String ) a.getAttribute( HTML.Attribute.ID ); |
| 144 |
sClass = ( String ) a.getAttribute( HTML.Attribute.CLASS ); |
| 145 |
if ( sId != null && sId.equalsIgnoreCase( "contentSub" ) ) |
| 146 |
{ |
| 147 |
m_bHTMLStartFound = true; |
| 148 |
} |
| 149 |
if ( sClass != null ) |
| 150 |
{ |
| 151 |
if ( sClass.equalsIgnoreCase( "printfooter" ) ) |
| 152 |
{ |
| 153 |
m_nHTMLArticleEnd = pos; |
| 154 |
} |
| 155 |
else if ( sClass.equalsIgnoreCase( "noarticletext" ) ) |
| 156 |
{ |
| 157 |
m_nNoArticleInd = pos; |
| 158 |
} |
| 159 |
else if ( sClass.equalsIgnoreCase( "errorbox" ) ) |
| 160 |
{ |
| 161 |
m_nErrorInd = pos; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
else if ( t == HTML.Tag.P ) |
| 166 |
{ |
| 167 |
sClass = ( String ) a.getAttribute( HTML.Attribute.CLASS ); |
| 168 |
if ( sClass != null && sClass.equalsIgnoreCase( "error" ) ) |
| 169 |
{ |
| 170 |
m_nErrorInd = pos; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
} |