2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
19 package com
.sun
.star
.wiki
;
21 import java
.io
.StringReader
;
22 import java
.io
.OutputStreamWriter
;
24 import java
.net
.URLEncoder
;
26 import java
.net
.HttpURLConnection
;
28 import javax
.swing
.text
.html
.HTMLEditorKit
;
30 import com
.sun
.star
.uno
.XComponentContext
;
33 public class WikiArticle
35 private final XComponentContext m_xContext
;
37 private String m_sEditTime
= "";
38 private String m_sEditToken
= "";
40 private String m_sHTMLCode
;
41 private boolean m_bNoArticle
= true;
43 private String m_sWikiUser
;
44 private String m_sWikiPass
;
46 private final String m_sTitle
;
48 private final URI m_aMainURI
;
49 private boolean m_isLoggedIn
= false;
51 /** Creates a new instance of WikiArticle */
52 public WikiArticle( XComponentContext xContext
, String sTitle
, Map
<String
,String
> wikiSettings
, boolean bLogin
, WikiPropDialog aPropDialog
)
53 throws java
.net
.URISyntaxException
, java
.io
.IOException
, WikiCancelException
55 m_xContext
= xContext
;
57 String sMainUrl
= wikiSettings
.get("Url");
58 m_sWikiUser
= wikiSettings
.get("Username");
59 m_sWikiPass
= wikiSettings
.get("Password");
62 m_aMainURI
= new URI(sMainUrl
);
66 WikiEditSettingDialog aDialog
= new WikiEditSettingDialog(m_xContext
, "vnd.sun.star.script:WikiEditor.EditSetting?location=application", wikiSettings
, false );
71 if ( aPropDialog
!= null )
72 aPropDialog
.SetThrobberActive( false );
74 if ( MainThreadDialogExecutor
.Show( xContext
, aDialog
) )
76 m_sWikiUser
= wikiSettings
.get("Username");
77 m_sWikiPass
= wikiSettings
.get("Password");
80 throw new WikiCancelException();
82 if ( aPropDialog
!= null )
84 aPropDialog
.SetThrobberActive( true );
91 aDialog
.DisposeDialog();
95 // in case of loading the html contents are used
96 // in case of saving the contents should be checked whether they are empty
100 public String
GetMainURL()
102 return m_aMainURI
.toString();
105 public String
GetTitle()
112 private String
getArticleWiki()
113 throws java
.net
.URISyntaxException
, java
.io
.IOException
, WikiCancelException
115 String sWikiCode
= null;
119 URI aURI
= new URI(m_aMainURI
.toString() + "index.php?title=" + m_sTitle
+ "&action=edit");
120 HttpURLConnection connGet
= Helper
.PrepareMethod("GET", aURI
, m_xContext
);
123 int nResultCode
= connGet
.getResponseCode();
124 String sWebPage
= null;
125 if (nResultCode
== 200) {
126 sWebPage
= Helper
.ReadResponseBody(connGet
);
129 if ( sWebPage
!= null )
131 StringReader r
= new StringReader(sWebPage
);
132 HTMLEditorKit
.Parser parse
= Helper
.GetHTMLParser();
133 EditPageParser callback
= new EditPageParser();
135 parse
.parse(r
,callback
,true);
136 m_sEditTime
= callback
.m_sEditTime
;
137 m_sEditToken
= callback
.m_sEditToken
;
139 int iPosStart
= callback
.m_nWikiArticleStart
;
140 int iPosEnd
= callback
.m_nWikiArticleEnd
;
142 if ( iPosStart
>= 0 && iPosEnd
> 0 )
144 String sArticle
= sWebPage
.substring(iPosStart
, iPosEnd
);
145 iPosStart
= sArticle
.indexOf('>') + 1;
146 sWikiCode
= sArticle
.substring( iPosStart
, sArticle
.length() );
154 private void InitArticleHTML()
155 throws java
.net
.URISyntaxException
, java
.io
.IOException
, WikiCancelException
159 URI uri
= new URI(m_aMainURI
.toString() + "index.php?title=" + m_sTitle
);
160 HttpURLConnection connGet
= Helper
.PrepareMethod("GET", uri
, m_xContext
);
163 int nResultCode
= connGet
.getResponseCode();
164 String sWebPage
= null;
165 if (nResultCode
== 200) {
166 sWebPage
= Helper
.ReadResponseBody(connGet
);
169 if ( sWebPage
!= null )
171 StringReader r
= new StringReader(sWebPage
);
172 HTMLEditorKit
.Parser parse
= Helper
.GetHTMLParser();
173 EditPageParser callback
= new EditPageParser();
175 parse
.parse(r
,callback
,true);
177 int iPosStart
= callback
.m_nHTMLArticleStart
;
178 int iPosEnd
= callback
.m_nHTMLArticleEnd
;
179 int nPosNoArt
= callback
.m_nNoArticleInd
;
181 if ( iPosStart
>= 0 && iPosEnd
> 0 )
183 m_sHTMLCode
= sWebPage
.substring(iPosStart
, iPosEnd
);
184 m_bNoArticle
= ( nPosNoArt
>= 0 && nPosNoArt
>= iPosStart
&& nPosNoArt
<= iPosEnd
);
190 protected boolean setArticle( String sWikiCode
, String sWikiComment
, boolean bMinorEdit
)
191 throws java
.net
.URISyntaxException
, java
.io
.IOException
, WikiCancelException
193 boolean bResult
= false;
195 if (m_isLoggedIn
&& sWikiCode
!= null && sWikiComment
!= null)
197 // get the edit time and token
200 URI uri
= new URI(m_aMainURI
.toString() + "index.php?title=" + m_sTitle
+ "&action=submit");
202 HttpURLConnection connPost
= Helper
.PrepareMethod("POST", uri
, m_xContext
);
203 connPost
.setDoInput(true);
204 connPost
.setDoOutput(true);
207 OutputStreamWriter post
= new OutputStreamWriter(connPost
.getOutputStream(), "UTF-8");
210 post
.write("wpTextbox1=");
211 post
.write(URLEncoder
.encode(sWikiCode
, "UTF-8"));
212 post
.write("&wpSummary=");
213 post
.write(URLEncoder
.encode(sWikiComment
, "UTF-8"));
214 post
.write("&wpSection=");
215 post
.write("&wpEdittime=");
216 post
.write(URLEncoder
.encode(m_sEditTime
, "UTF-8"));
217 post
.write("&wpSave=Save%20page");
218 post
.write("&wpEditToken=");
219 post
.write(URLEncoder
.encode(m_sEditToken
, "UTF-8"));
222 post
.write("&wpMinoredit=1");
232 int nResultCode
= connPost
.getResponseCode();
233 if ( nResultCode
< 400 )
236 String aResult
= Helper
.ReadResponseBody(connPost
);
238 // TODO: remove the debug printing, try to detect the error
239 System
.out
.print( "nSubmitCode = " + nResultCode
+ "\n===\n" + aResult
);
245 private boolean Login()
246 throws java
.net
.URISyntaxException
, java
.io
.IOException
, WikiCancelException
248 m_isLoggedIn
= Helper
.Login( m_aMainURI
, m_sWikiUser
, m_sWikiPass
, m_xContext
);
252 protected boolean NotExist()
254 boolean bResult
= true;
255 if ( m_sHTMLCode
!= null )
256 bResult
= m_bNoArticle
;