-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfo.java
More file actions
1265 lines (983 loc) · 37.5 KB
/
Copy pathInfo.java
File metadata and controls
1265 lines (983 loc) · 37.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Info.java
// Andrew Davison, ad@fivedots.coe.psu.ac.th, December 2016
/* * fonts
* lookup registry modifications
* configuration paths in office
* get info about the loaded document
* services, interfaces, methods info
* style info
* fonts
* document properties
* installed package info
* import/export filters
*/
import java.io.*;
import java.util.*;
import java.lang.reflect.Method;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.xpath.*;
import javax.activation.MimetypesFileTypeMap;
import com.sun.star.beans.*;
import com.sun.star.lang.*;
import com.sun.star.uno.*;
import com.sun.star.awt.*;
import com.sun.star.document.*;
import com.sun.star.container.*;
import com.sun.star.style.*;
import com.sun.star.util.*;
import com.sun.star.deployment.*;
import com.sun.star.uno.Exception;
import com.sun.star.io.IOException;
import com.sun.star.lang.IllegalArgumentException;
public class Info
{
public static final String REG_MOD_FNM = "registrymodifications.xcu";
public static final String NODE_PRODUCT = "/org.openoffice.Setup/Product";
public static final String NODE_L10N = "/org.openoffice.Setup/L10N";
public static final String NODE_OFFICE = "/org.openoffice.Setup/Office";
// used to access Office configuration nodes at runtime
private static final String[] NODE_PATHS = { NODE_PRODUCT, NODE_L10N};
private static final String MIME_FNM = "mime.types";
// mime.types "borrowed" from Python 3.5
/* In LibreOffice 4\share\registry\main.xcd:
Arguments (in Product):
ooName
ooXMLFileFormatVersion
ooXMLFileFormatName
ooSetupVersion
ooSetupVersionAboutBox
ooSetupVersionAboutBoxSuffix
ooVendor
ooSetupExtension
ooOpenSourceContext
In L10N:
ooLocale
ooSetupSystemLocale
ooSetupCurrency
DecimalSeparatorAsLocale
IgnoreLanguageChange
DateAcceptancePatterns
In Office:
ooSetupInstCompleted
InstalledLocales
ooSetupConnectionURL
MigrationCompleted
LastCompatibilityCheckID
Factories
*/
// filter flag constants
// from https://wiki.openoffice.org/wiki/Documentation/
// DevGuide/OfficeDev/Properties_of_a_Filter
public static final int IMPORT = 0x00000001;
public static final int EXPORT = 0x00000002;
public static final int TEMPLATE = 0x00000004;
public static final int INTERNAL = 0x00000008;
public static final int TEMPLATEPATH = 0x00000010;
public static final int OWN = 0x00000020;
public static final int ALIEN = 0x00000040;
public static final int DEFAULT = 0x00000100;
public static final int SUPPORTSSELECTION = 0x00000400;
public static final int NOTINFILEDIALOG = 0x00001000;
public static final int NOTINCHOOSER = 0x00002000;
public static final int READONLY = 0x00010000;
public static final int THIRDPARTYFILTER = 0x00080000;
public static final int PREFERRED = 0x10000000;
// ----------------------- fonts ----------------------------------
public static FontDescriptor[] getFonts()
{
XToolkit xToolkit = Lo.createInstanceMCF(XToolkit.class,
"com.sun.star.awt.Toolkit");
XDevice device = xToolkit.createScreenCompatibleDevice(0,0);
if (device == null) {
System.out.println("Could not access graphical output device");
return null;
}
else
return device.getFontDescriptors();
} // end of getFonts()
public static String[] getFontNames()
{
FontDescriptor[] fds = getFonts();
if (fds == null)
return null;
// use a set to exclude duplicate names
Set<String> namesSet = new HashSet<String>();
for(int i = 0; i < fds.length; i++)
namesSet.add(fds[i].Name);
String[] names = namesSet.toArray(new String[0]);
Arrays.sort(names);
return names;
} // end of getFontNames()
// ----------------- lookup registry modifications --------------------
public static String getRegModsPath()
// return path to "registrymodifications.xcu"
{
String userConfigDir = FileIO.urlToPath(Info.getPaths("UserConfig"));
// System.out.println("\nUser Config: " + userConfigDir);
try {
String parentPath = new File(userConfigDir).getParent();
return parentPath + "//" + REG_MOD_FNM;
}
catch (java.lang.Exception e)
{ System.out.println("Could not parse " + userConfigDir);
return null;
}
} // end of getRegModsPath()
public static String getRegItemProp(String item, String prop)
{ return getRegItemProp(item, null, prop); }
public static String getRegItemProp(String item, String node, String prop)
// return value from "registrymodifications.xcu"
// e.g. "Writer/MailMergeWizard" null, "MailAddress"
// e.g. "Logging/Settings", "org.openoffice.logging.sdbc.DriverManager", "LogLevel"
/*
This xpath doesn't deal with all cases in the XCU file, which sometimes
has many node levels between the item and the prop.
Returns null if no value is found, or it's only an empty string.
*/
{
String fnm = getRegModsPath();
// System.out.println(fnm);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringElementContentWhitespace(true);
factory.setNamespaceAware(true);
String value = null;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse( new File(fnm));
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
xpath.setNamespaceContext( new UniversalNamespaceResolver(doc) );
XPathExpression expr = null;
if (node == null)
expr = xpath.compile("//item[@oor:path='/org.openoffice.Office." + item +
"']/prop[@oor:name='" + prop + "']");
else
expr = xpath.compile("//item[@oor:path='/org.openoffice.Office." + item +
"']/node[@oor:name='" + node +
"']/prop[@oor:name='" + prop + "']");
value = (String) expr.evaluate(doc, XPathConstants.STRING);
if ((value == null) || value.equals("")) {
System.out.println("Item Property not found");
value = null;
}
else {
value = value.trim();
if (value.equals("")) {
System.out.println("Item Property is white space (?)");
value = null;
}
}
}
catch (XPathExpressionException e)
{ System.out.println(e); }
catch (ParserConfigurationException e)
{ System.out.println(e); }
catch (SAXException e)
{ System.out.println(e); }
catch (java.io.IOException e)
{ System.out.println(e); }
return value;
} // end of getRegItemProp()
// -------------------------- configuration paths --------------------
public static String getConfig(String nodeStr)
{
for (String nodePath : NODE_PATHS) {
String info = (String) getConfig(nodePath, nodeStr);
if (info != null)
return info;
}
System.out.println("No configuration info for " + nodeStr);
return null;
} // end of getConfig()
public static Object getConfig(String nodePath, String nodeStr)
{
XPropertySet props = getConfigProps(nodePath);
if (props == null)
return null;
else
return Props.getProperty(props, nodeStr);
} // end of getConfig()
public static XPropertySet getConfigProps(String nodePath)
{
// create the provider and remember it as a XMultiServiceFactory
XMultiServiceFactory conProv = Lo.createInstanceMCF(XMultiServiceFactory.class,
"com.sun.star.configuration.ConfigurationProvider");
if (conProv == null) {
System.out.println("Could not create configuration provider");
return null;
}
PropertyValue[] props = Props.makeProps("nodepath", nodePath);
// specifies the location of the view root in the configuration.
try {
return Lo.qi(XPropertySet.class,
conProv.createInstanceWithArguments(
"com.sun.star.configuration.ConfigurationAccess", props));
}
catch (Exception ex)
{ System.out.println("Unable to access config properties for\n \"" +
nodePath + "\"");
return null;
}
} // end of getConfigProps()
public static String getPaths(String setting)
/* access LO's predefined paths. There are two different groups of properties.
One group stores only a single path and the other group stores two or
more paths - separated by a semicolon. See
https://wiki.openoffice.org/w/index.php?title=Documentation/DevGuide/OfficeDev/Path_Settings
Some setting values (as listed in the OpenOffice docs for PathSettings):
Addin, AutoCorrect, AutoText, Backup, Basic, Bitmap,
Config, Dictionary, Favorite, Filter, Gallery,
Graphic, Help, Linguistic, Module, Palette, Plugin,
Storage, Temp, Template, UIConfig, UserConfig,
UserDictionary (deprecated), Work
Replaced by thePathSetting in LibreOffice 4.3
*/
{
XPropertySet propSet = Lo.createInstanceMCF(XPropertySet.class, "com.sun.star.util.PathSettings");
if (propSet == null) {
System.out.println("Could not access office settings");
return null;
}
// Props.showProps("Path Settings", propSet); // for debugging
try {
return (String) propSet.getPropertyValue(setting);
}
catch(Exception e)
{ System.out.println("Could not find setting for: " + setting);
return null;
}
} // end of getPaths()
public static String[] getDirs(String setting)
{
String paths = getPaths(setting);
if (paths == null) {
System.out.println("Cound not find paths for \"" + setting + "\"");
return null;
}
String[] pathsArr = paths.split(";");
if (pathsArr == null) {
System.out.println("Cound not split paths for \"" + setting + "\"");
return new String[] { paths }; // return string as a 1-element array
}
String[] dirs = new String[ pathsArr.length ];
for(int i=0; i < pathsArr.length; i++)
dirs[i] = FileIO.URI2Path(pathsArr[i]);
return dirs;
} // end of getDirs()
public static String getOfficeDir()
// returns the file path to the office dir
// e.g. "C:\Program Files (x86)\LibreOffice 4\"
{
String addinDir = getPaths("Addin");
// e.g. file:///C:/Program%20Files%20(x86)/LibreOffice%204/program/../program/addin
if (addinDir == null) {
System.out.println("Cound not find settings information");
return null;
}
String addinPath = FileIO.URI2Path(addinDir);
// e.g. C:\Program Files (x86)\LibreOffice 4\program\addin
int idx = addinPath.indexOf("program");
if (idx == -1) {
System.out.println("Cound not extract office path");
return addinPath;
}
else
return addinPath.substring(0, idx);
} // end of getOfficeDir()
public static String getGalleryDir()
/* The gallery string contains two directories: the office location in
share/gallery/, and the user one in Roaming; return the first
*/
{
String[] galleryDirs = getDirs("Gallery");
if (galleryDirs == null)
return null;
else
return galleryDirs[0];
} // end of getGallerDir()
public static XHierarchicalPropertySet createConfigurationView(String sPath)
// Create a specified read-only configuration view
{
// create the provider and remember it as a XMultiServiceFactory
XMultiServiceFactory conProv = Lo.createInstanceMCF(XMultiServiceFactory.class,
"com.sun.star.configuration.ConfigurationProvider");
if (conProv == null) {
System.out.println("Could not create configuration provider");
return null;
}
PropertyValue[] props = Props.makeProps("nodepath", sPath);
// specifies the location of the view root in the configuration.
try {
XInterface root = (XInterface) conProv.createInstanceWithArguments(
"com.sun.star.configuration.ConfigurationAccess" , props);
showServices("ConfigurationAccess", root);
return Lo.qi(XHierarchicalPropertySet.class, root);
}
catch (Exception ex)
{ // System.out.println("Unable to access Office info on " + sPath);
return null;
}
} // emd createConfigurationView()
// =================== update configuration settings ================
public static boolean setConfig(String nodePath, String nodeStr, Object val)
{
XPropertySet props = setConfigProps(nodePath);
if (props == null)
return false;
else {
Props.setProperty(props, nodeStr, val);
XChangesBatch secureChange = Lo.qi(XChangesBatch.class, props);
try {
secureChange.commitChanges();
return true;
}
catch (Exception ex)
{ System.out.println("Unable to commit config update for\n \"" +
nodePath + "\"");
return false;
}
}
} // end of setConfig()
public static XPropertySet setConfigProps(String nodePath)
{
// create the provider and remember it as a XMultiServiceFactory
XMultiServiceFactory conProv = Lo.createInstanceMCF(XMultiServiceFactory.class,
"com.sun.star.configuration.ConfigurationProvider");
if (conProv == null) {
System.out.println("Could not create configuration provider");
return null;
}
PropertyValue[] props = Props.makeProps("nodepath", nodePath);
// specifies the location of the view root in the configuration.
try {
return Lo.qi(XPropertySet.class,
conProv.createInstanceWithArguments(
"com.sun.star.configuration.ConfigurationUpdateAccess", props));
}
catch (Exception ex)
{ System.out.println("Unable to access config update properties for\n \"" +
nodePath + "\"");
return null;
}
} // end of setConfigProps()
// =================== getting info about a document ====================
public static String getName(String fnm)
// extract the file's name from the supplied string
{
int dotPos = fnm.lastIndexOf('.');
if (dotPos == -1) {
System.out.println("No extension found for " + fnm);
return fnm;
}
else if (dotPos == 0) {
System.out.println("No filename text found for " + fnm);
return null;
}
fnm = fnm.substring(0, dotPos);
// strip out directories path
// doesn't handle URLs with parameters or anchors
int slashIndex = fnm.lastIndexOf('/');
if (slashIndex > 0)
fnm = fnm.substring(slashIndex+1, fnm.length());
return fnm;
} // end of getName()
public static String getExt(String fnm)
// return extenson without the "."
{
int dotPos = fnm.lastIndexOf('.');
if (dotPos == -1) {
System.out.println("No extension found for " + fnm);
return null;
}
else if (dotPos == fnm.length()-1) {
System.out.println("No extension text found for " + fnm);
return null;
}
else
return fnm.substring(dotPos+1).toLowerCase();
} // end of getExt()
public static String getUniqueFnm(String fnm)
/* if a file called fnm already exists, then a number
is added to the name so the filename is unique
*/
{ String fName = getName(fnm);
String ext = getExt(fnm);
File f = new File(fnm);
int i = 1;
while (f.exists()) {
fnm = fName + i + ext;
f = new File(fnm);
i++;
}
return fnm;
} // end of getUniqueFnm()
public static String getDocType(String fnm)
/* use a type detector to determine
the type of the file. The commented-out code is a simple, fast
detection, while the approach used looks at the file's metadata */
{
XTypeDetection xTypeDetect =
Lo.createInstanceMCF(XTypeDetection.class, "com.sun.star.document.TypeDetection");
if (xTypeDetect == null) {
System.out.println("No type detector reference");
return null;
}
if (!FileIO.isOpenable(fnm))
return null;
String urlStr = FileIO.fnmToURL(fnm);
if (urlStr == null)
return null;
// return xTypeDetect.queryTypeByURL(urlStr); // 'flat' type detection
// uses the URL and some configuration data only
// or use queryTypeByDescriptor() and a MediaDescriptor
PropertyValue[][] mediaDescr = new PropertyValue[1][1];
mediaDescr[0][0] = new PropertyValue();
mediaDescr[0][0].Name = "URL";
mediaDescr[0][0].Value = urlStr;
return xTypeDetect.queryTypeByDescriptor(mediaDescr, true);
} // end of getDocType()
/*
public static XTypeDetection getTypeDetector()
{
if ((xcc == null) || (mcFactory == null)) {
System.out.println("No office connection found");
return null;
}
// get the file type detection interface
//try {
return createInstanceMCF(XTypeDetection.class, "com.sun.star.document.TypeDetection");
//Object typeDetectObj = mcFactory.createInstanceWithContext(
// "com.sun.star.document.TypeDetection", xcc);
//return (XTypeDetection) Lo.qi(XTypeDetection.class, typeDetectObj);
// }
// catch (Exception e) {
// System.out.println("Could not create a file type detector");
// return null;
// }
} // end of getTypeDetector()
*/
public static int reportDocType(Object doc)
{
int docType = Lo.UNKNOWN;
if (isDocType(doc, Lo.WRITER_SERVICE)) {
System.out.println("A Writer document");
docType = Lo.WRITER;
}
else if (isDocType(doc, Lo.IMPRESS_SERVICE)) {
System.out.println("An Impress document");
docType = Lo.IMPRESS;
}
else if (isDocType(doc, Lo.DRAW_SERVICE)) {
System.out.println("A Draw document");
docType = Lo.DRAW;
}
else if (isDocType(doc, Lo.CALC_SERVICE)) {
System.out.println("A Calc spreadsheet");
docType = Lo.CALC;
}
else if (isDocType(doc, Lo.BASE_SERVICE)) {
System.out.println("A Base document");
docType = Lo.BASE;
}
else if (isDocType(doc, Lo.MATH_SERVICE)) {
System.out.println("A Math document");
docType = Lo.MATH;
}
else
System.out.println("Unknown document");
return docType;
} // end of reportDocType()
public static String docTypeString(Object doc)
{
if (isDocType(doc, Lo.WRITER_SERVICE)) {
System.out.println("A Writer document");
return Lo.WRITER_SERVICE;
}
else if (isDocType(doc, Lo.IMPRESS_SERVICE)) {
System.out.println("An Impress document");
return Lo.IMPRESS_SERVICE;
}
else if (isDocType(doc, Lo.DRAW_SERVICE)) {
System.out.println("A Draw document");
return Lo.DRAW_SERVICE;
}
else if (isDocType(doc, Lo.CALC_SERVICE)) {
System.out.println("A Calc spreadsheet");
return Lo.CALC_SERVICE;
}
else if (isDocType(doc, Lo.BASE_SERVICE)) {
System.out.println("A Base document");
return Lo.BASE_SERVICE;
}
else if (isDocType(doc, Lo.MATH_SERVICE)) {
System.out.println("A Math document");
return Lo.MATH_SERVICE;
}
else {
System.out.println("Unknown document");
return Lo.UNKNOWN_SERVICE;
}
} // end of docTypeString()
public static boolean isDocType(Object obj, String docType)
// was XComponent
{
XServiceInfo si = Lo.qi(XServiceInfo.class, obj);
return si.supportsService(docType);
}
public static String getImplementationName(Object obj)
{
XServiceInfo si = Lo.qi(XServiceInfo.class, obj);
if (si == null) {
System.out.println("Could not get service information");
return null;
}
else
return si.getImplementationName();
} // end of getImplementationName()
public static String getMIMEType(String fnm)
// also see FileIO.getMimeType() for use of zipped mimetype;
// also see Office API-based Images.getMimeType() for images only
{
File f = new File(fnm);
try {
MimetypesFileTypeMap mftMap = new MimetypesFileTypeMap(
FileIO.getUtilsFolder() + MIME_FNM);
// MimetypesFileTypeMap is a Java API
// System.out.println("Mime Type for " + fnm + ": \"" +
// mftMap.getContentType(f) + "\"");
return mftMap.getContentType(f);
}
catch(java.lang.Exception e)
{ System.out.println("Could not find " + MIME_FNM);
return "application/octet-stream"; // better than nothing
}
} // end of getMIMEType()
public static int mimeDocType(String mimeType)
{
if (mimeType == null)
return Lo.UNKNOWN;
if (mimeType.contains("vnd.oasis.opendocument.text"))
return Lo.WRITER;
else if (mimeType.contains("vnd.oasis.opendocument.base"))
return Lo.BASE;
else if (mimeType.contains("vnd.oasis.opendocument.spreadsheet"))
return Lo.CALC;
else if (mimeType.contains("vnd.oasis.opendocument.graphics") ||
mimeType.contains("vnd.oasis.opendocument.image") ||
mimeType.contains("vnd.oasis.opendocument.chart"))
return Lo.DRAW;
else if (mimeType.contains("vnd.oasis.opendocument.presentation"))
return Lo.IMPRESS;
else if (mimeType.contains("vnd.oasis.opendocument.formula"))
return Lo.MATH;
else return Lo.UNKNOWN;
} // end of mimeDocType()
public static boolean isImageMime(String mimeType)
{
if (mimeType.startsWith("image/"))
return true;
if (mimeType.startsWith("application/x-openoffice-bitmap"))
return true;
return false;
} // end of isImageMime()
// ------------------------ services, interfaces, methods info ----------------------
public static String[] getServiceNames()
{
XMultiComponentFactory mcFactory = Lo.getComponentFactory();
if (mcFactory == null)
return null;
else {
String[] serviceNames = mcFactory.getAvailableServiceNames();
Arrays.sort(serviceNames);
return serviceNames;
}
} // end of getServiceNames()
public static String[] getServiceNames(String serviceName)
{
ArrayList<String> names = new ArrayList<String>();
try {
XContentEnumerationAccess enumAccess =
Lo.qi(XContentEnumerationAccess.class, Lo.getComponentFactory());
XEnumeration xEnum = enumAccess.createContentEnumeration(serviceName);
while (xEnum.hasMoreElements()) {
Object obj = xEnum.nextElement();
XServiceInfo si = Lo.qi(XServiceInfo.class, obj);
names.add( si.getImplementationName());
}
}
catch(Exception e) {
System.out.println("Could not collect service names for: " + serviceName);
return null;
}
if (names.size() == 0) {
System.out.println("No service names found for: " + serviceName);
return null;
}
String[] serviceNames = names.toArray(new String[names.size()]);
Arrays.sort(serviceNames);
return serviceNames;
} // end of getServiceNames()
public static String[] getServices(Object obj)
{
XServiceInfo si = Lo.qi(XServiceInfo.class, obj);
if (si == null) {
System.out.println("No XServiceInfo interface found");
return null;
}
String[] serviceNames = si.getSupportedServiceNames();
Arrays.sort(serviceNames);
return serviceNames;
} // end of getServices()
public static void showServices(String objName, Object obj)
{
String[] services = getServices(obj);
if (services == null) {
System.out.println("No supported services found for " + objName);
return;
}
System.out.println(objName + " Supported Services (" + services.length + ")");
for(String service : services)
System.out.println(" \"" + service + "\"");
} // end of showServices()
public static boolean supportService(Object obj, String serviceName)
{
XServiceInfo si = Lo.qi(XServiceInfo.class, obj);
if (si == null) {
System.out.println("No service info found");
return false;
}
else
return si.supportsService(serviceName);
} // end of supportService()
public static String[] getAvailableServices(Object obj)
{
XMultiServiceFactory msf = Lo.qi(XMultiServiceFactory.class, obj);
String[] serviceNames = msf.getAvailableServiceNames();
Arrays.sort(serviceNames);
return serviceNames;
} // end of getAvailableServices()
public static Type[] getInterfaceTypes(Object target)
{
Type[] types = null;
XTypeProvider typeProvider = Lo.qi(XTypeProvider.class, target);
if (typeProvider != null)
types = typeProvider.getTypes();
return types;
} // end of getInterfaceTypes()
public static String[] getInterfaces(Object target)
{
XTypeProvider typeProvider = Lo.qi(XTypeProvider.class, target);
if (typeProvider == null)
return null;
else
return getInterfaces(typeProvider);
} // end of getInterfaces()
public static String[] getInterfaces(XTypeProvider typeProvider)
{
Type[] types = typeProvider.getTypes();
// use a set to exclude duplicate names
Set<String> namesSet = new HashSet<String>();
for(int i = 0; i < types.length; i++)
namesSet.add(types[i].getTypeName());
String[] typeNames = namesSet.toArray(new String[0]);
Arrays.sort(typeNames);
return typeNames;
} // end of getInterfaces()
public static void showInterfaces(String objName, Object obj)
{
String[] intfs = getInterfaces(obj);
if (intfs == null) {
System.out.println("No interfaces found for " + objName);
return;
}
System.out.println(objName + " Interfaces (" + intfs.length + ")");
for(String intf : intfs)
System.out.println(" " + intf);
} // end of showInterfaces()
public static String[] getMethods(String interfaceName)
{
try {
return getMethods( new Type(interfaceName));
}
catch(com.sun.star.uno.RuntimeException e)
{ System.out.println("Could not find the interface name: " + interfaceName);
return null;
}
} // end of getMethods()
public static String[] getMethods(Type intf)
{
Method[] methods = intf.getZClass().getMethods(); // methods from Class class
String[] methodNames = new String[methods.length];
for (int i=0; i < methods.length; i++)
methodNames[i] = methods[i].getName();
Arrays.sort(methodNames);
return methodNames;
} // end of getMethods()
public static void showMethods(String interfaceName)
{
String[] methods = getMethods(interfaceName);
if (methods == null)
return;
System.out.println(interfaceName + " Methods (" + methods.length + ")");
for(String method : methods)
System.out.println(" " + method);
} // end of showMethods()
// -------------------------- style info --------------------------
public static String[] getStyleFamilyNames(Object doc)
// get the names of all the style families
// was XComponent
{
XStyleFamiliesSupplier xSupplier = Lo.qi(XStyleFamiliesSupplier.class, doc);
XNameAccess nameAcc = xSupplier.getStyleFamilies();
String[] names = nameAcc.getElementNames();
Arrays.sort(names);
return names;
} // end of getStyleFamilyNames()
public static XNameContainer getStyleContainer(Object doc,
String familyStyleName)
// get the container for a specified style family name
{
try {
XStyleFamiliesSupplier xSupplier = Lo.qi(XStyleFamiliesSupplier.class, doc);
XNameAccess nameAcc = xSupplier.getStyleFamilies();
return Lo.qi(XNameContainer.class, nameAcc.getByName(familyStyleName));
}
catch(Exception e)
{ System.out.println("Could not access the family style: " + familyStyleName);
return null;
}
} // end of getStyleContainer()
public static String[] getStyleNames(Object doc,
String familyStyleName)
// get all the style names for a style family
{
XNameContainer styleContainer = getStyleContainer(doc, familyStyleName);
if (styleContainer == null)
return null;
else {
String[] names = styleContainer.getElementNames();
Arrays.sort(names);
return names;
}
} // end of getStyleNames()
public static XPropertySet getPageStyleProps(Object doc)
{ return getStyleProps(doc, "PageStyles", "Standard"); }
public static XPropertySet getParagraphStyleProps(Object doc)
{ return getStyleProps(doc, "ParagraphStyles", "Standard"); }
public static XPropertySet getStyleProps(Object doc,
String familyStyleName, String propSetNm)
/* get the named property set from the given style family */
{
XNameContainer styleContainer = getStyleContainer(doc, familyStyleName);
// container is a collection of named property sets
if (styleContainer == null)
return null;
else {
XPropertySet nameProps = null;
try {
nameProps = Lo.qi( XPropertySet.class, styleContainer.getByName(propSetNm));
}
catch(Exception e)
{ System.out.println("Could not access style: " + e); }
return nameProps;
}
} // end of getStyleProps()