PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/package/qa/storages/TestHelper.java

https://bitbucket.org/markjenkins/libreoffice_ubuntu-debian-fixes
Java | 1679 lines | 1376 code | 200 blank | 103 comment | 183 complexity | fb5ea408be7db9246fe7a32dd8a262eb MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause-No-Nuclear-License-2014

Large files files are truncated, but you can click here to view the full file

  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. package complex.storages;
  19. import com.sun.star.uno.UnoRuntime;
  20. import com.sun.star.uno.XInterface;
  21. import com.sun.star.uno.AnyConverter;
  22. import com.sun.star.lang.*;
  23. import com.sun.star.embed.*;
  24. import com.sun.star.packages.*;
  25. import com.sun.star.io.*;
  26. import com.sun.star.beans.*;
  27. import share.LogWriter;
  28. public class TestHelper {
  29. LogWriter m_aLogWriter;
  30. String m_sTestPrefix;
  31. public TestHelper( LogWriter aLogWriter, String sTestPrefix )
  32. {
  33. m_aLogWriter = aLogWriter;
  34. m_sTestPrefix = sTestPrefix;
  35. }
  36. public boolean WriteBytesToStream( XStream xStream,
  37. String sStreamName,
  38. String sMediaType,
  39. boolean bCompressed,
  40. byte[] pBytes )
  41. {
  42. // get output stream of substream
  43. XOutputStream xOutput = xStream.getOutputStream();
  44. if ( xOutput == null )
  45. {
  46. Error( "Can't get XOutputStream implementation from substream '" + sStreamName + "'!" );
  47. return false;
  48. }
  49. // get XTrucate implementation from output stream
  50. XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface( XTruncate.class, xOutput );
  51. if ( xTruncate == null )
  52. {
  53. Error( "Can't get XTruncate implementation from substream '" + sStreamName + "'!" );
  54. return false;
  55. }
  56. // write requested byte sequence
  57. try
  58. {
  59. xTruncate.truncate();
  60. xOutput.writeBytes( pBytes );
  61. }
  62. catch( Exception e )
  63. {
  64. Error( "Can't write to stream '" + sStreamName + "', exception: " + e );
  65. return false;
  66. }
  67. // get access to the XPropertySet interface
  68. XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStream );
  69. if ( xPropSet == null )
  70. {
  71. Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" );
  72. return false;
  73. }
  74. // set properties to the stream
  75. try
  76. {
  77. xPropSet.setPropertyValue( "MediaType", sMediaType );
  78. xPropSet.setPropertyValue( "Compressed", new Boolean( bCompressed ) );
  79. }
  80. catch( Exception e )
  81. {
  82. Error( "Can't set properties to substream '" + sStreamName + "', exception: " + e );
  83. return false;
  84. }
  85. // check size property of the stream
  86. try
  87. {
  88. long nSize = AnyConverter.toLong( xPropSet.getPropertyValue( "Size" ) );
  89. if ( nSize != pBytes.length )
  90. {
  91. Error( "The 'Size' property of substream '" + sStreamName + "' contains wrong value!" );
  92. return false;
  93. }
  94. }
  95. catch( Exception e )
  96. {
  97. Error( "Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e );
  98. return false;
  99. }
  100. return true;
  101. }
  102. public boolean WriteBytesToSubstreamDefaultCompressed( XStorage xStorage,
  103. String sStreamName,
  104. String sMediaType,
  105. byte[] pBytes )
  106. {
  107. // open substream element
  108. XStream xSubStream = null;
  109. try
  110. {
  111. Object oSubStream = xStorage.openStreamElement( sStreamName, ElementModes.WRITE );
  112. xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
  113. if ( xSubStream == null )
  114. {
  115. Error( "Can't create substream '" + sStreamName + "'!" );
  116. return false;
  117. }
  118. }
  119. catch( Exception e )
  120. {
  121. Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
  122. return false;
  123. }
  124. // get output stream of substream
  125. XOutputStream xOutput = xSubStream.getOutputStream();
  126. if ( xOutput == null )
  127. {
  128. Error( "Can't get XOutputStream implementation from substream '" + sStreamName + "'!" );
  129. return false;
  130. }
  131. // get XTrucate implementation from output stream
  132. XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface( XTruncate.class, xOutput );
  133. if ( xTruncate == null )
  134. {
  135. Error( "Can't get XTruncate implementation from substream '" + sStreamName + "'!" );
  136. return false;
  137. }
  138. // write requested byte sequence
  139. try
  140. {
  141. xTruncate.truncate();
  142. xOutput.writeBytes( pBytes );
  143. }
  144. catch( Exception e )
  145. {
  146. Error( "Can't write to stream '" + sStreamName + "', exception: " + e );
  147. return false;
  148. }
  149. // get access to the XPropertySet interface
  150. XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xSubStream );
  151. if ( xPropSet == null )
  152. {
  153. Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" );
  154. return false;
  155. }
  156. // set properties to the stream
  157. // do not set the compressed property
  158. try
  159. {
  160. xPropSet.setPropertyValue( "MediaType", sMediaType );
  161. }
  162. catch( Exception e )
  163. {
  164. Error( "Can't set properties to substream '" + sStreamName + "', exception: " + e );
  165. return false;
  166. }
  167. // check size property of the stream
  168. try
  169. {
  170. long nSize = AnyConverter.toLong( xPropSet.getPropertyValue( "Size" ) );
  171. if ( nSize != pBytes.length )
  172. {
  173. Error( "The 'Size' property of substream '" + sStreamName + "' contains wrong value!" );
  174. return false;
  175. }
  176. }
  177. catch( Exception e )
  178. {
  179. Error( "Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e );
  180. return false;
  181. }
  182. // free the stream resources, garbage collector may remove the object too late
  183. if ( !disposeStream( xSubStream, sStreamName ) )
  184. return false;
  185. return true;
  186. }
  187. public boolean WriteBytesToSubstream( XStorage xStorage,
  188. String sStreamName,
  189. String sMediaType,
  190. boolean bCompressed,
  191. byte[] pBytes )
  192. {
  193. // open substream element
  194. XStream xSubStream = null;
  195. try
  196. {
  197. Object oSubStream = xStorage.openStreamElement( sStreamName, ElementModes.WRITE );
  198. xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
  199. if ( xSubStream == null )
  200. {
  201. Error( "Can't create substream '" + sStreamName + "'!" );
  202. return false;
  203. }
  204. }
  205. catch( Exception e )
  206. {
  207. Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
  208. return false;
  209. }
  210. if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
  211. return false;
  212. // free the stream resources, garbage collector may remove the object too late
  213. if ( !disposeStream( xSubStream, sStreamName ) )
  214. return false;
  215. return true;
  216. }
  217. public boolean WriteBytesToEncrSubstream( XStorage xStorage,
  218. String sStreamName,
  219. String sMediaType,
  220. boolean bCompressed,
  221. byte[] pBytes,
  222. String sPass )
  223. {
  224. // open substream element
  225. XStream xSubStream = null;
  226. try
  227. {
  228. Object oSubStream = xStorage.openEncryptedStreamElement( sStreamName, ElementModes.WRITE, sPass );
  229. xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
  230. if ( xSubStream == null )
  231. {
  232. Error( "Can't create substream '" + sStreamName + "'!" );
  233. return false;
  234. }
  235. }
  236. catch( Exception e )
  237. {
  238. Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
  239. return false;
  240. }
  241. if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
  242. return false;
  243. // free the stream resources, garbage collector may remove the object too late
  244. if ( !disposeStream( xSubStream, sStreamName ) )
  245. return false;
  246. return true;
  247. }
  248. public boolean WBToSubstrOfEncr( XStorage xStorage,
  249. String sStreamName,
  250. String sMediaType,
  251. boolean bCompressed,
  252. byte[] pBytes,
  253. boolean bEncrypted )
  254. {
  255. // open substream element
  256. XStream xSubStream = null;
  257. try
  258. {
  259. Object oSubStream = xStorage.openStreamElement( sStreamName, ElementModes.WRITE );
  260. xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
  261. if ( xSubStream == null )
  262. {
  263. Error( "Can't create substream '" + sStreamName + "'!" );
  264. return false;
  265. }
  266. }
  267. catch( Exception e )
  268. {
  269. Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
  270. return false;
  271. }
  272. // get access to the XPropertySet interface
  273. XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xSubStream );
  274. if ( xPropSet == null )
  275. {
  276. Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" );
  277. return false;
  278. }
  279. // set properties to the stream
  280. try
  281. {
  282. xPropSet.setPropertyValue( "UseCommonStoragePasswordEncryption", new Boolean( bEncrypted ) );
  283. }
  284. catch( Exception e )
  285. {
  286. Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamName + "', exception: " + e );
  287. return false;
  288. }
  289. if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
  290. return false;
  291. // free the stream resources, garbage collector may remove the object too late
  292. if ( !disposeStream( xSubStream, sStreamName ) )
  293. return false;
  294. return true;
  295. }
  296. public boolean WriteBytesToStreamH( XStorage xStorage,
  297. String sStreamPath,
  298. String sMediaType,
  299. boolean bCompressed,
  300. byte[] pBytes,
  301. boolean bCommit )
  302. {
  303. // open substream element
  304. XStream xSubStream = null;
  305. try
  306. {
  307. XHierarchicalStorageAccess xHStorage =
  308. (XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
  309. if ( xHStorage == null )
  310. {
  311. Error( "The storage does not support hierarchical access!" );
  312. return false;
  313. }
  314. Object oSubStream = xHStorage.openStreamElementByHierarchicalName( sStreamPath, ElementModes.WRITE );
  315. xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
  316. if ( xSubStream == null )
  317. {
  318. Error( "Can't create substream '" + sStreamPath + "'!" );
  319. return false;
  320. }
  321. }
  322. catch( Exception e )
  323. {
  324. Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
  325. return false;
  326. }
  327. if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
  328. return false;
  329. XTransactedObject xTransact =
  330. (XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xSubStream );
  331. if ( xTransact == null )
  332. {
  333. Error( "Substream '" + sStreamPath + "', stream opened for writing must be transacted!" );
  334. return false;
  335. }
  336. if ( bCommit )
  337. {
  338. try {
  339. xTransact.commit();
  340. } catch( Exception e )
  341. {
  342. Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
  343. return false;
  344. }
  345. }
  346. // free the stream resources, garbage collector may remove the object too late
  347. if ( !disposeStream( xSubStream, sStreamPath ) )
  348. return false;
  349. return true;
  350. }
  351. public boolean WriteBytesToEncrStreamH( XStorage xStorage,
  352. String sStreamPath,
  353. String sMediaType,
  354. boolean bCompressed,
  355. byte[] pBytes,
  356. String sPass,
  357. boolean bCommit )
  358. {
  359. // open substream element
  360. XStream xSubStream = null;
  361. try
  362. {
  363. XHierarchicalStorageAccess xHStorage =
  364. (XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
  365. if ( xHStorage == null )
  366. {
  367. Error( "The storage does not support hierarchical access!" );
  368. return false;
  369. }
  370. Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sStreamPath,
  371. ElementModes.WRITE,
  372. sPass );
  373. xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
  374. if ( xSubStream == null )
  375. {
  376. Error( "Can't create substream '" + sStreamPath + "'!" );
  377. return false;
  378. }
  379. }
  380. catch( Exception e )
  381. {
  382. Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
  383. return false;
  384. }
  385. if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
  386. return false;
  387. XTransactedObject xTransact =
  388. (XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xSubStream );
  389. if ( xTransact == null )
  390. {
  391. Error( "Substream '" + sStreamPath + "', stream opened for writing must be transacted!" );
  392. return false;
  393. }
  394. if ( bCommit )
  395. {
  396. try {
  397. xTransact.commit();
  398. } catch( Exception e )
  399. {
  400. Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
  401. return false;
  402. }
  403. }
  404. // free the stream resources, garbage collector may remove the object too late
  405. if ( !disposeStream( xSubStream, sStreamPath ) )
  406. return false;
  407. return true;
  408. }
  409. public boolean WBToSubstrOfEncrH( XStorage xStorage,
  410. String sStreamPath,
  411. String sMediaType,
  412. boolean bCompressed,
  413. byte[] pBytes,
  414. boolean bEncrypted,
  415. boolean bCommit )
  416. {
  417. // open substream element
  418. XStream xSubStream = null;
  419. try
  420. {
  421. XHierarchicalStorageAccess xHStorage =
  422. (XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
  423. if ( xHStorage == null )
  424. {
  425. Error( "The storage does not support hierarchical access!" );
  426. return false;
  427. }
  428. Object oSubStream = xHStorage.openStreamElementByHierarchicalName( sStreamPath, ElementModes.WRITE );
  429. xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
  430. if ( xSubStream == null )
  431. {
  432. Error( "Can't create substream '" + sStreamPath + "'!" );
  433. return false;
  434. }
  435. }
  436. catch( Exception e )
  437. {
  438. Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
  439. return false;
  440. }
  441. // get access to the XPropertySet interface
  442. XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xSubStream );
  443. if ( xPropSet == null )
  444. {
  445. Error( "Can't get XPropertySet implementation from substream '" + sStreamPath + "'!" );
  446. return false;
  447. }
  448. // set properties to the stream
  449. try
  450. {
  451. xPropSet.setPropertyValue( "UseCommonStoragePasswordEncryption", new Boolean( bEncrypted ) );
  452. }
  453. catch( Exception e )
  454. {
  455. Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamPath + "', exception: " + e );
  456. return false;
  457. }
  458. if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
  459. return false;
  460. XTransactedObject xTransact =
  461. (XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xSubStream );
  462. if ( xTransact == null )
  463. {
  464. Error( "Substream '" + sStreamPath + "', stream opened for writing must be transacted!" );
  465. return false;
  466. }
  467. if ( bCommit )
  468. {
  469. try {
  470. xTransact.commit();
  471. } catch( Exception e )
  472. {
  473. Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
  474. return false;
  475. }
  476. }
  477. // free the stream resources, garbage collector may remove the object too late
  478. if ( !disposeStream( xSubStream, sStreamPath ) )
  479. return false;
  480. return true;
  481. }
  482. public int ChangeStreamPass( XStorage xStorage,
  483. String sStreamName,
  484. String sOldPass,
  485. String sNewPass )
  486. {
  487. // open substream element
  488. XStream xSubStream = null;
  489. try
  490. {
  491. Object oSubStream = xStorage.openEncryptedStreamElement( sStreamName, ElementModes.WRITE, sOldPass );
  492. xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
  493. if ( xSubStream == null )
  494. {
  495. Error( "Can't open substream '" + sStreamName + "'!" );
  496. return 0;
  497. }
  498. }
  499. catch( Exception e )
  500. {
  501. Error( "Can't open substream '" + sStreamName + "', exception : " + e + "!" );
  502. return 0;
  503. }
  504. // change the password for the stream
  505. XEncryptionProtectedSource xStreamEncryption =
  506. (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xSubStream );
  507. if ( xStreamEncryption == null )
  508. {
  509. Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
  510. return -1;
  511. }
  512. try {
  513. xStreamEncryption.setEncryptionPassword( sNewPass );
  514. }
  515. catch( Exception e )
  516. {
  517. Error( "Can't change encryption key of the substream '" + sStreamName + "', exception:" + e );
  518. return 0;
  519. }
  520. // free the stream resources, garbage collector may remove the object too late
  521. if ( !disposeStream( xSubStream, sStreamName ) )
  522. return 0;
  523. return 1;
  524. }
  525. public int ChangeStreamPassH( XStorage xStorage,
  526. String sPath,
  527. String sOldPass,
  528. String sNewPass,
  529. boolean bCommit )
  530. {
  531. // open substream element
  532. XHierarchicalStorageAccess xHStorage =
  533. (XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
  534. if ( xHStorage == null )
  535. {
  536. Error( "The storage does not support hierarchical access!" );
  537. return 0;
  538. }
  539. XStream xSubStream = null;
  540. try
  541. {
  542. Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sPath, ElementModes.WRITE, sOldPass );
  543. xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
  544. if ( xSubStream == null )
  545. {
  546. Error( "Can't open encrypted substream '" + sPath + "'!" );
  547. return 0;
  548. }
  549. }
  550. catch( Exception e )
  551. {
  552. Error( "Can't open encrypted substream '" + sPath + "', exception : " + e + "!" );
  553. return 0;
  554. }
  555. // change the password for the stream
  556. XEncryptionProtectedSource xStreamEncryption =
  557. (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xSubStream );
  558. if ( xStreamEncryption == null )
  559. {
  560. Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
  561. return -1;
  562. }
  563. try {
  564. xStreamEncryption.setEncryptionPassword( sNewPass );
  565. }
  566. catch( Exception e )
  567. {
  568. Error( "Can't change encryption key of the substream '" + sPath + "', exception:" + e );
  569. return 0;
  570. }
  571. XTransactedObject xTransact =
  572. (XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xSubStream );
  573. if ( xTransact == null )
  574. {
  575. Error( "Substream '" + sPath + "', stream opened for writing must be transacted!" );
  576. return 0;
  577. }
  578. if ( bCommit )
  579. {
  580. try {
  581. xTransact.commit();
  582. } catch( Exception e )
  583. {
  584. Error( "Can't commit storage after substream '" + sPath + "' change, exception : " + e + "!" );
  585. return 0;
  586. }
  587. }
  588. // free the stream resources, garbage collector may remove the object too late
  589. if ( !disposeStream( xSubStream, sPath ) )
  590. return 0;
  591. return 1;
  592. }
  593. public boolean setStorageTypeAndCheckProps( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
  594. {
  595. boolean bOk = false;
  596. // get access to the XPropertySet interface
  597. XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStorage );
  598. if ( xPropSet != null )
  599. {
  600. try
  601. {
  602. // set "MediaType" property to the stream
  603. xPropSet.setPropertyValue( "MediaType", sMediaType );
  604. // get "IsRoot" and "OpenMode" properties and control there values
  605. boolean bPropIsRoot = AnyConverter.toBoolean( xPropSet.getPropertyValue( "IsRoot" ) );
  606. int nPropMode = AnyConverter.toInt( xPropSet.getPropertyValue( "OpenMode" ) );
  607. bOk = true;
  608. if ( bPropIsRoot != bIsRoot )
  609. {
  610. Error( "'IsRoot' property contains wrong value!" );
  611. bOk = false;
  612. }
  613. if ( ( bIsRoot
  614. && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
  615. || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
  616. {
  617. Error( "'OpenMode' property contains wrong value, expected " + nMode + ", in reality " + nPropMode + "!" );
  618. bOk = false;
  619. }
  620. }
  621. catch( Exception e )
  622. {
  623. Error( "Can't control properties of substorage, exception: " + e );
  624. }
  625. }
  626. else
  627. {
  628. Error( "Can't get XPropertySet implementation from storage!" );
  629. }
  630. return bOk;
  631. }
  632. public boolean checkStorageProperties( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
  633. {
  634. boolean bOk = false;
  635. // get access to the XPropertySet interface
  636. XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStorage );
  637. if ( xPropSet != null )
  638. {
  639. try
  640. {
  641. // get "MediaType", "IsRoot" and "OpenMode" properties and control there values
  642. String sPropMediaType = AnyConverter.toString( xPropSet.getPropertyValue( "MediaType" ) );
  643. boolean bPropIsRoot = AnyConverter.toBoolean( xPropSet.getPropertyValue( "IsRoot" ) );
  644. int nPropMode = AnyConverter.toInt( xPropSet.getPropertyValue( "OpenMode" ) );
  645. bOk = true;
  646. if ( !sPropMediaType.equals( sMediaType ) )
  647. {
  648. Error( "'MediaType' property contains wrong value, expected '"
  649. + sMediaType + "', set '" + sPropMediaType + "' !" );
  650. bOk = false;
  651. }
  652. if ( bPropIsRoot != bIsRoot )
  653. {
  654. Error( "'IsRoot' property contains wrong value!" );
  655. bOk = false;
  656. }
  657. if ( ( bIsRoot
  658. && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
  659. || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
  660. {
  661. Error( "'OpenMode' property contains wrong value, expected " + nMode + ", in reality " + nPropMode + "!" );
  662. bOk = false;
  663. }
  664. }
  665. catch( Exception e )
  666. {
  667. Error( "Can't get properties of substorage, exception: " + e );
  668. }
  669. }
  670. else
  671. {
  672. Error( "Can't get XPropertySet implementation from storage!" );
  673. }
  674. return bOk;
  675. }
  676. public boolean InternalCheckStream( XStream xStream,
  677. String sName,
  678. String sMediaType,
  679. boolean bCompressed,
  680. byte[] pBytes,
  681. boolean bCheckCompressed )
  682. {
  683. // get input stream of substream
  684. XInputStream xInput = xStream.getInputStream();
  685. if ( xInput == null )
  686. {
  687. Error( "Can't get XInputStream implementation from substream '" + sName + "'!" );
  688. return false;
  689. }
  690. byte pContents[][] = new byte[1][]; // ???
  691. // read contents
  692. try
  693. {
  694. xInput.readBytes( pContents, pBytes.length + 1 );
  695. }
  696. catch( Exception e )
  697. {
  698. Error( "Can't read from stream '" + sName + "', exception: " + e );
  699. return false;
  700. }
  701. // check size of stream data
  702. if ( pContents.length == 0 )
  703. {
  704. Error( "SubStream '" + sName + "' reading produced disaster!" );
  705. return false;
  706. }
  707. if ( pBytes.length != pContents[0].length )
  708. {
  709. Error( "SubStream '" + sName + "' contains wrong amount of data! (" + pContents[0].length + "/" + pBytes.length + ")" );
  710. return false;
  711. }
  712. // check stream data
  713. for ( int ind = 0; ind < pBytes.length; ind++ )
  714. {
  715. if ( pBytes[ind] != pContents[0][ind] )
  716. {
  717. Error( "SubStream '" + sName + "' contains wrong data! ( byte num. "
  718. + ind + " should be " + pBytes[ind] + " but it is " + pContents[0][ind] + ")" );
  719. return false;
  720. }
  721. }
  722. // check properties
  723. boolean bOk = false;
  724. // get access to the XPropertySet interface
  725. XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStream );
  726. if ( xPropSet != null )
  727. {
  728. try
  729. {
  730. // get "MediaType" and "Size" properties and control there values
  731. String sPropMediaType = AnyConverter.toString( xPropSet.getPropertyValue( "MediaType" ) );
  732. long nPropSize = AnyConverter.toLong( xPropSet.getPropertyValue( "Size" ) );
  733. boolean bPropCompress = AnyConverter.toBoolean( xPropSet.getPropertyValue( "Compressed" ) );
  734. bOk = true;
  735. if ( !sPropMediaType.equals( sMediaType ) )
  736. {
  737. Error( "'MediaType' property contains wrong value for stream '" + sName + "',\nexpected: '"
  738. + sMediaType + "', set: '" + sPropMediaType + "'!" );
  739. bOk = false;
  740. }
  741. if ( nPropSize != pBytes.length )
  742. {
  743. Error( "'Size' property contains wrong value for stream'" + sName + "'!" );
  744. bOk = false;
  745. }
  746. if ( bCheckCompressed && bPropCompress != bCompressed )
  747. {
  748. Error( "'Compressed' property contains wrong value for stream'" + sName + "'!" );
  749. bOk = false;
  750. }
  751. }
  752. catch( Exception e )
  753. {
  754. Error( "Can't get properties of substream '" + sName + "', exception: " + e );
  755. }
  756. }
  757. else
  758. {
  759. Error( "Can't get XPropertySet implementation from stream '" + sName + "'!" );
  760. }
  761. return bOk;
  762. }
  763. public boolean checkStream( XStorage xParentStorage,
  764. String sName,
  765. String sMediaType,
  766. boolean bCompressed,
  767. byte[] pBytes )
  768. {
  769. // open substream element first
  770. XStream xSubStream = null;
  771. try
  772. {
  773. Object oSubStream = xParentStorage.openStreamElement( sName, ElementModes.READ );
  774. xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
  775. if ( xSubStream == null )
  776. {
  777. Error( "Can't open substream '" + sName + "'!" );
  778. return false;
  779. }
  780. }
  781. catch( Exception e )
  782. {
  783. Error( "Can't open substream '" + sName + "', exception : " + e + "!" );
  784. return false;
  785. }
  786. boolean bResult = InternalCheckStream( xSubStream, sName, sMediaType, bCompressed, pBytes, true );
  787. // free the stream resources, garbage collector may remove the object too late
  788. if ( !disposeStream( xSubStream, sName ) )
  789. return false;
  790. return bResult;
  791. }
  792. public boolean checkEncrStream( XStorage xParentStorage,
  793. String sName,
  794. String sMediaType,
  795. byte[] pBytes,
  796. String sPass )
  797. {
  798. // Important: a common password for any of parent storage should not be set or
  799. // should be different from sPass
  800. try
  801. {
  802. Object oSubStream = xParentStorage.openStreamElement( sName, ElementModes.READ );
  803. Error( "Encrypted stream '" + sName + "' was opened without password!" );
  804. return false;
  805. }
  806. catch( WrongPasswordException wpe )
  807. {}
  808. catch( Exception e )
  809. {
  810. Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' without password: " + e + "!" );
  811. return false;
  812. }
  813. String sWrongPass = "11";
  814. sWrongPass += sPass;
  815. try
  816. {
  817. Object oSubStream = xParentStorage.openEncryptedStreamElement( sName, ElementModes.READ, sWrongPass );
  818. Error( "Encrypted stream '" + sName + "' was opened with wrong password!" );
  819. return false;
  820. }
  821. catch( WrongPasswordException wpe )
  822. {}
  823. catch( Exception e )
  824. {
  825. Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' with wrong password: " + e + "!" );
  826. return false;
  827. }
  828. XStream xSubStream = null;
  829. try
  830. {
  831. Object oSubStream = xParentStorage.openEncryptedStreamElement( sName, ElementModes.READ, sPass );
  832. xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
  833. if ( xSubStream == null )
  834. {
  835. Error( "Can't open encrypted substream '" + sName + "'!" );
  836. return false;
  837. }
  838. }
  839. catch( Exception e )
  840. {
  841. Error( "Can't open encrypted substream '" + sName + "', exception : " + e + "!" );
  842. return false;
  843. }
  844. // encrypted streams will be compressed always, so after the storing this property is always true,
  845. // although before the storing it can be set to false ( it is not always clear whether a stream is encrypted
  846. // before the storing )
  847. boolean bResult = InternalCheckStream( xSubStream, sName, sMediaType, true, pBytes, false );
  848. // free the stream resources, garbage collector may remove the object too late
  849. if ( !disposeStream( xSubStream, sName ) )
  850. return false;
  851. return bResult;
  852. }
  853. public boolean checkStreamH( XStorage xParentStorage,
  854. String sPath,
  855. String sMediaType,
  856. boolean bCompressed,
  857. byte[] pBytes )
  858. {
  859. // open substream element first
  860. XStream xSubStream = null;
  861. try
  862. {
  863. XHierarchicalStorageAccess xHStorage =
  864. (XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xParentStorage );
  865. if ( xHStorage == null )
  866. {
  867. Error( "The storage does not support hierarchical access!" );
  868. return false;
  869. }
  870. Object oSubStream = xHStorage.openStreamElementByHierarchicalName( sPath, ElementModes.READ );
  871. xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
  872. if ( xSubStream == null )
  873. {
  874. Error( "Can't open substream '" + sPath + "'!" );
  875. return false;
  876. }
  877. }
  878. catch( Exception e )
  879. {
  880. Error( "Can't open substream '" + sPath + "', exception : " + e + "!" );
  881. return false;
  882. }
  883. boolean bResult = InternalCheckStream( xSubStream, sPath, sMediaType, bCompressed, pBytes, true );
  884. // free the stream resources, garbage collector may remove the object too late
  885. if ( !disposeStream( xSubStream, sPath ) )
  886. return false;
  887. return bResult;
  888. }
  889. public boolean checkEncrStreamH( XStorage xParentStorage,
  890. String sPath,
  891. String sMediaType,
  892. byte[] pBytes,
  893. String sPass )
  894. {
  895. // Important: a common password for any of parent storage should not be set or
  896. // should be different from sPass
  897. XHierarchicalStorageAccess xHStorage =
  898. (XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xParentStorage );
  899. if ( xHStorage == null )
  900. {
  901. Error( "The storage does not support hierarchical access!" );
  902. return false;
  903. }
  904. try
  905. {
  906. Object oSubStream = xHStorage.openStreamElementByHierarchicalName( sPath, ElementModes.READ );
  907. XStream xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
  908. Error( "Encrypted substream '" + sPath + "' was opened without password!" );
  909. return false;
  910. }
  911. catch( WrongPasswordException wpe )
  912. {}
  913. catch( Exception e )
  914. {
  915. Error( "Unexpected exception in case of opening of encrypted stream '" + sPath + "' without password: " + e + "!" );
  916. return false;
  917. }
  918. String sWrongPass = "11";
  919. sWrongPass += sPass;
  920. try
  921. {
  922. Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sPath, ElementModes.READ, sWrongPass );
  923. XStream xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
  924. Error( "Encrypted substream '" + sPath + "' was opened with wrong password!" );
  925. return false;
  926. }
  927. catch( WrongPasswordException wpe )
  928. {}
  929. catch( Exception e )
  930. {
  931. Error( "Unexpected exception in case of opening of encrypted stream '" + sPath + "' with wrong password: " + e + "!" );
  932. return false;
  933. }
  934. XStream xSubStream = null;
  935. try
  936. {
  937. Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sPath, ElementModes.READ, sPass );
  938. xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
  939. if ( xSubStream == null )
  940. {
  941. Error( "Can't open encrypted substream '" + sPath + "'!" );
  942. return false;
  943. }
  944. }
  945. catch( Exception e )
  946. {
  947. Error( "Can't open encrypted substream '" + sPath + "', exception : " + e + "!" );
  948. return false;
  949. }
  950. // encrypted streams will be compressed always, so after the storing this property is always true,
  951. // although before the storing it can be set to false ( it is not always clear whether a stream is encrypted
  952. // before the storing )
  953. boolean bResult = InternalCheckStream( xSubStream, sPath, sMediaType, true, pBytes, false );
  954. // free the stream resources, garbage collector may remove the object too late
  955. if ( !disposeStream( xSubStream, sPath ) )
  956. return false;
  957. return bResult;
  958. }
  959. public boolean copyStorage( XStorage xSourceStorage, XStorage xDestStorage )
  960. {
  961. // copy xSourceStorage to xDestStorage
  962. try
  963. {
  964. xSourceStorage.copyToStorage( xDestStorage );
  965. }
  966. catch( Exception e )
  967. {
  968. Error( "Storage copying failed, exception: " + e );
  969. return false;
  970. }
  971. return true;
  972. }
  973. public boolean commitStorage( XStorage xStorage )
  974. {
  975. // XTransactedObject must be supported by storages
  976. XTransactedObject xTransact = (XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xStorage );
  977. if ( xTransact == null )
  978. {
  979. Error( "Storage doesn't implement transacted access!" );
  980. return false;
  981. }
  982. try
  983. {
  984. xTransact.commit();
  985. }
  986. catch( Exception e )
  987. {
  988. Error( "Storage commit failed, exception:" + e );
  989. return false;
  990. }
  991. return true;
  992. }
  993. public boolean disposeStream( XStream xStream, String sStreamName )
  994. {
  995. XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class, xStream );
  996. if ( xComponent == null )
  997. {
  998. Error( "Can't get XComponent implementation from substream '" + sStreamName + "'!" );
  999. return false;
  1000. }
  1001. try
  1002. {
  1003. xComponent.dispose();
  1004. }
  1005. catch( Exception e )
  1006. {
  1007. Error( "Substream '" + sStreamName + "' disposing throws exception: " + e );
  1008. return false;
  1009. }
  1010. return true;
  1011. }
  1012. public boolean disposeStorage( XStorage xStorage )
  1013. {
  1014. // dispose the storage
  1015. XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class, xStorage );
  1016. if ( xComponent == null )
  1017. {
  1018. Error( "Can't retrieve XComponent implementation from storage!" );
  1019. return false;
  1020. }
  1021. try
  1022. {
  1023. xComponent.dispose();
  1024. }
  1025. catch( Exception e )
  1026. {
  1027. Error( "Storage disposing failed!" );
  1028. return false;
  1029. }
  1030. return true;
  1031. }
  1032. public XInputStream getInputStream( XStream xStream )
  1033. {
  1034. XInputStream xInTemp = null;
  1035. try
  1036. {
  1037. xInTemp = xStream.getInputStream();
  1038. if ( xInTemp == null )
  1039. Error( "Can't get the input part of a stream!" );
  1040. }
  1041. catch ( Exception e )
  1042. {
  1043. Error( "Can't get the input part of a stream, exception :" + e );
  1044. }
  1045. return xInTemp;
  1046. }
  1047. public boolean closeOutput( XStream xStream )
  1048. {
  1049. XOutputStream xOutTemp = null;
  1050. try
  1051. {
  1052. xOutTemp = xStream.getOutputStream();
  1053. if ( xOutTemp == null )
  1054. {
  1055. Error( "Can't get the output part of a stream!" );
  1056. return false;
  1057. }
  1058. }
  1059. catch ( Exception e )
  1060. {
  1061. Error( "Can't get the output part of a stream, exception :" + e );
  1062. return false;
  1063. }
  1064. try
  1065. {
  1066. xOutTemp.closeOutput();
  1067. }
  1068. catch ( Exception e )
  1069. {
  1070. Error( "Can't close output part of a stream, exception :" + e );
  1071. return false;
  1072. }
  1073. return true;
  1074. }
  1075. public XStorage openSubStorage( XStorage xStorage, String sName, int nMode )
  1076. {
  1077. // open existing substorage
  1078. try
  1079. {
  1080. Object oSubStorage = xStorage.openStorageElement( sName, nMode );
  1081. XStorage xSubStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oSubStorage );
  1082. return xSubStorage;
  1083. }
  1084. catch( Exception e )
  1085. {
  1086. Error( "Can't open substorage '" + sName + "', exception: " + e );
  1087. }
  1088. return null;
  1089. }
  1090. public XStream CreateTempFileStream( XMultiServiceFactory xMSF )
  1091. {
  1092. // try to get temporary file representation
  1093. XStream xTempFileStream = null;
  1094. try
  1095. {
  1096. Object oTempFile = xMSF.createInstance( "com.sun.star.io.TempFile" );
  1097. xTempFileStream = (XStream)UnoRuntime.queryInterface( XStream.class, oTempFile );
  1098. }
  1099. catch( Exception e )
  1100. {}
  1101. if ( xTempFileStream == null )
  1102. Error( "Can't create temporary file!" );
  1103. return xTempFileStream;
  1104. }
  1105. public String CreateTempFile( XMultiServiceFactory xMSF )
  1106. {
  1107. String sResult = null;
  1108. // try to get temporary file representation
  1109. XPropertySet xTempFileProps = null;
  1110. try
  1111. {
  1112. Object oTempFile = xMSF.createInstance( "com.sun.star.io.TempFile" );
  1113. xTempFileProps = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, oTempFile );
  1114. }
  1115. catch( Exception e )
  1116. {}
  1117. if ( xTempFileProps != null )
  1118. {
  1119. try
  1120. {
  1121. xTempFileProps.setPropertyValue( "RemoveFile", new Boolean( false ) );
  1122. sResult = AnyConverter.toString( xTempFileProps.getPropertyValue( "Uri" ) );
  1123. }
  1124. catch( Exception e )
  1125. {
  1126. Error( "Can't control TempFile properties, exception: " + e );
  1127. }
  1128. }
  1129. else
  1130. {
  1131. Error( "Can't create temporary file representation!" );
  1132. }
  1133. // close temporary file explicitly
  1134. try
  1135. {
  1136. XStream xStream = (XStream)UnoRuntime.queryInterface( XStream.class, xTempFileProps );
  1137. if ( xStream != null )
  1138. {
  1139. XOutputStream xOut = xStream.getOutputStream();
  1140. if ( xOut != null )
  1141. xOut.closeOutput();
  1142. XInputStream xIn = xStream.getInputStream();
  1143. if ( xIn != null )
  1144. xIn.closeInput();
  1145. }
  1146. else
  1147. Error( "Can't close TempFile!" );
  1148. }
  1149. catch( Exception e )
  1150. {
  1151. Error( "Can't close TempFile, exception: " + e );
  1152. }
  1153. return sResult;
  1154. }
  1155. public boolean copyElementTo( XStorage xSource, String sName, XStorage xDest )
  1156. {
  1157. // copy element with name sName from xSource to xDest
  1158. try
  1159. {
  1160. xSource.copyElementTo( sName, xDest, sName );
  1161. }
  1162. catch( Exception e )
  1163. {
  1164. Error( "Element copying failed, exception: " + e );
  1165. return false;
  1166. }
  1167. return true;
  1168. }
  1169. public boolean copyElementTo( XStorage xSource, String sName, XStorage xDest, String sTargetName )
  1170. {
  1171. // copy element with name sName from xSource to xDest
  1172. try
  1173. {
  1174. xSource.copyElementTo( sName, xDest, sTargetName );
  1175. }
  1176. catch( Exception e )
  1177. {
  1178. Error( "Element copying failed, exception: " + e );
  1179. return false;
  1180. }
  1181. return true;
  1182. }
  1183. public boolean moveElementTo( XStorage xSource, String sName, XStorage xDest )
  1184. {
  1185. // move element with name sName from xSource to xDest
  1186. try
  1187. {
  1188. xSource.moveElementTo( sName, xDest, sName );
  1189. }
  1190. catch( Exception e )
  1191. {
  1192. Error( "Element moving failed, exception: " + e );
  1193. return false;
  1194. }
  1195. return true;
  1196. }
  1197. public boolean renameElement( XStorage xStorage, String sOldName, String sNewName )
  1198. {
  1199. // rename element with name sOldName to sNewName
  1200. try
  1201. {
  1202. xStorage.renameElement( sOldName, sNewName );
  1203. }
  1204. catch( Exception e )
  1205. {
  1206. Error( "Element renaming failed, exception: " + e );
  1207. return false;
  1208. }
  1209. return true;
  1210. }
  1211. public boolean removeElement( XStorage xStorage, String sName )
  1212. {
  1213. // remove element with name sName
  1214. try
  1215. {
  1216. xStorage.removeElement( sName );
  1217. }
  1218. catch( Exception e )
  1219. {
  1220. Error( "Element removing failed, exception: " + e );
  1221. return false;
  1222. }
  1223. return true;
  1224. }
  1225. public XStream OpenStream( XStorage xStorage,
  1226. String sStreamName,
  1227. int nMode )
  1228. {
  1229. // open substream element
  1230. XStream xSubStream = null;
  1231. try
  1232. {
  1233. Object oSubStream = xStorage.openStreamElement( sStreamName, nMode );
  1234. xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
  1235. if ( xSubStream == null )
  1236. Error( "Can't create substream '" + sStreamName + "'!" );
  1237. }
  1238. catch( Exception e )
  1239. {
  1240. Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
  1241. }
  1242. return xSubStream;
  1243. }
  1244. public boolean compareRawMethodsOnEncrStream( XStorage xStorage, String sStreamName )
  1245. {
  1246. XStorageRawAccess xRawStorage;
  1247. try
  1248. {
  1249. xRawStorage = (XStorageRawAccess) UnoRuntime.queryInterface( XStorageRawAccess.class, xStorage );
  1250. }
  1251. catch( Exception e )
  1252. {
  1253. Error( "Can't get raw access to the storage, exception : " + e + "!" );
  1254. return false;
  1255. }
  1256. if ( xRawStorage == null )
  1257. {
  1258. Error( "Can't get raw access to the storage!" );
  1259. return false;
  1260. }
  1261. XInputStream xHeadRawStream = null;
  1262. try
  1263. {
  1264. xHeadRawStream = xRawStorage.getRawEncrStreamElement( sStreamName );
  1265. }
  1266. catch( Exception e )
  1267. {
  1268. Error( "Can't open encrypted stream '" + sStreamName + "' in raw mode with header, exception : " + e + "!" );
  1269. }
  1270. XInputStream xPlainRawStream = nul

Large files files are truncated, but you can click here to view the full file