-
Notifications
You must be signed in to change notification settings - Fork 3
/
listing1.html
executable file
·1059 lines (892 loc) · 44.4 KB
/
listing1.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<!-- BEGIN META TAG INFO -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="home" href="http://developer.apple.com/">
<link rel="find" href="http://developer.apple.com/search/">
<link rel="stylesheet" type="text/css" href="../../documentation/css/adcstyle.css" title="fonts">
<script language="JavaScript" src="../../documentation/js/adc.js" type="text/javascript"></script>
<!-- END META TAG INFO -->
<!-- BEGIN TITLE -->
<title>PictureShow - /PictureShow.java</title>
<!-- END TITLE -->
<script language="JavaScript">
function JumpToNewPage() {
window.location=document.scpopupmenu.gotop.value;
return true;
}
</script>
</head>
<!-- BEGIN BODY OPEN -->
<body>
<!--END BODY OPEN -->
<!-- START CENTER OPEN -->
<center>
<!-- END CENTER OPEN -->
<!-- BEGIN LOGO AND SEARCH -->
<!--#include virtual="/includes/adcnavbar"-->
<!-- END LOGO AND SEARCH -->
<!-- START BREADCRUMB -->
<div id="breadcrumb">
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td scope="row"><img width="340" height="10" src="images/1dot.gif" alt=""></td>
<td><img width="340" height="10" src="images/1dot.gif" alt=""></td>
</tr>
<tr valign="middle">
<td align="left" colspan="2">
<a href="http://developer.apple.com/">ADC Home</a> > <a href="../../referencelibrary/index.html">Reference Library</a> > <a href="../../samplecode/index.html">Sample Code</a> > <a href="../../samplecode/LegacyTechnologies/index.html">Legacy Documents</a> > <a href="../../samplecode/LegacyTechnologies/idxAppleApplications-date.html">Apple Applications</a> > <A HREF="javascript:location.replace('index.html');">PictureShow</A> >
</td>
</tr>
<tr>
<td colspan="2" scope="row"><img width="680" height="35" src="images/1dot.gif" alt=""></td>
</tr>
</table>
</div>
<!-- END BREADCRUMB -->
<!-- START MAIN CONTENT -->
<!-- START TITLE GRAPHIC AND INTRO-->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td><h1><div id="pagehead">PictureShow</div></h1></td>
</tr>
</table>
<!-- END TITLE GRAPHIC AND INTRO -->
<!-- START WIDE COLUMN -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td id="scdetails">
<h2>/PictureShow.java</h2>
<form name="scpopupmenu" onSubmit="return false;" method=post>
<p><strong>View Source Code:</strong>
<select name="gotop" onChange="JumpToNewPage();" style="width:340px"><option selected value="ingnore">Select File</option>
<option value="listing1.html">/PictureShow.java</option></select>
</p>
</form>
<p><strong><a href="PictureShow.zip">Download Sample</a></strong> (“PictureShow.zip”, 604.0K)<BR>
<strong><a href="PictureShow.dmg">Download Sample</a></strong> (“PictureShow.dmg”, 693.5K)</p>
<!--
<p><strong><a href="#">Download Sample</a></strong> (“filename.sit”, 500K)</p>
-->
</td>
</tr>
<tr>
<td scope="row"><img width="680" height="10" src="images/1dot.gif" alt=""><br>
<img height="1" width="680" src="images/1dot_919699.gif" alt=""><br>
<img width="680" height="20" src="images/1dot.gif" alt=""></td>
</tr>
<tr>
<td scope="row">
<!--googleon: index -->
<pre class="sourcecodebox">/*
File: PictureShow.java
Description: PictureShow is a Java command line tool that takes a list of images and creates a
Keynote document with a slide for each image, with the image's name below it.
By default it will create a simple, white-on-black theme. Optionally a Keynote theme
document (or a regular Keynote document) may be specified whose theme will be applied to
the document created.
Author: <JAG>
Copyright: © Copyright 2002 Apple Computer, Inc. All rights reserved.
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
("Apple") in consideration of your agreement to the following terms, and your
use, installation, modification or redistribution of this Apple software
constitutes acceptance of these terms. If you do not agree with these terms,
please do not use, install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and subject
to these terms, Apple grants you a personal, non-exclusive license, under Apple's
copyrights in this original Apple software (the "Apple Software"), to use,
reproduce, modify and redistribute the Apple Software, with or without
modifications, in source and/or binary forms; provided that if you redistribute
the Apple Software in its entirety and without modifications, you must retain
this notice and the following text and disclaimers in all such redistributions of
the Apple Software. Neither the name, trademarks, service marks or logos of
Apple Computer, Inc. may be used to endorse or promote products derived from the
Apple Software without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or implied,
are granted by Apple herein, including but not limited to any patent rights that
may be infringed by your derivative works or by other works in which the Apple
Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Change History (most recent first):
version 1.0: Feb 21 2003: Initial version released to public.
*/
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import com.apple.cocoa.application.NSImageRep;
import org.w3c.dom.*;
import org.w3c.dom.ls.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
/**
* A class which can create a Keynote slideshow from a series of source
* images. The resultant show contains a single slide for each image
* with the image's file name below it.
*/
public class PictureShow {
/** A reference to a file whose theme we will use (null if none) */
private File templateBundle = null;
/** The name of the output bundle */
private String outputBundleName = null;
/** An array of VALID images for which we will create slides */
private ImageInfo[] imageFiles = null;
/** The W3C DOM document builder class. */
private DocumentBuilder docBuilder = null;
/** The W3C DOM document object representing the target document */
private Document document = null;
/** The dimesions of slides in our target document */
private Dimension slideSize = new Dimension(0,0);
/** The id of the master slide to reference when creating new blank slides */
private String blankMasterId = null;
/**
* Main entry point for the PictureShow command line tool.
*
* @see #printHelpAndExit
*/
public static void main (String[] args) throws Exception {
PictureShow show = new PictureShow();
show.parseArgs(args);
show.generateShow();
}
/**
* Displayes the usage information for the tool and exits.
*/
private void printHelpAndExit() {
System.err.println("Usage: PictureShow [-options] <imagefile>... <output-bundle>");
System.err.println(" <imagefile>... = A list of image files to use as the source for the picture show");
System.err.println(" <output-bundle> = A name to be used for the output bundle.");
System.err.println(" -t[heme] <themefile> = Use the theme from the indicated file");
System.err.println(" (May be a .key file or a .kth file.)");
System.err.println(" -help = this message");
System.exit(1);
}
/**
* The main body of the program that creates the document containing the
* image slides.
*/
private void generateShow() throws Exception {
// Create and configure a document builder factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// If namespace aware, the parser will complain when reading APXL since
// the plugin: namespace isn't explicitly defined.
dbf.setNamespaceAware(false);
// We turn off validation so that we don't need to reference the schema
dbf.setValidating(false);
// The white space inside elements is significant for Keynote.
dbf.setIgnoringElementContentWhitespace(false);
// We want to leave any CDATA/text nodes separate so that if we
// need to serialise the theme data, we don't introduce invalid
// characters in a text section
dbf.setCoalescing(false);
docBuilder = dbf.newDocumentBuilder();
if (templateBundle == null) {
createBlankTheme();
} else {
readTheme(templateBundle);
}
// Loop through the valid images and create a slide for each one
for (int i=0; i < imageFiles.length; i++) {
createImageSlide(imageFiles[i]);
}
writeDocument(outputBundleName);
}
/**
* Creates a new, blank slide (calling createBlankSlide()) and adds the
* specified image to it. The file name of the image is added beneath
* as a title.
*
* @see createBlankSlide
*/
private void createImageSlide(ImageInfo sourceImage) {
Element slide = createBlankSlide();
// createBlankSlide creates the drawables group for us.
Element drawables = getFirstChildElementNamed(slide, "drawables");
// Below is an example of what the image element looks like that we're
// creating:
//
// <image image-data="image%20URL" transformation="1 0 0 1 286 144"/>
Element image = document.createElement("image");
// We replace all space characters with "%20", since Keynote needs proper
// URL escaping for the image-data attribute. (We should probably escape
// other characters as well, but they aren't so likely to appear in file
// names.)
String name = sourceImage.getFile().getName();
StringBuffer escapedName = new StringBuffer();
for (int i=0; i < name.length(); i++) {
char currentCharacter = name.charAt(i);
if (currentCharacter == ' ') {
escapedName.append("%20");
} else {
escapedName.append(currentCharacter);
}
}
image.setAttribute("image-data", escapedName.toString());
// determine the size/scale and location to place the image
float scale = 1;
Dimension imageSize = sourceImage.getSize();
// we'll center the image in top part of the slide, and make sure there
// is at least a 25-pixel border on all sides, 125 along the bottom for
// the label.
Dimension targetSize = new Dimension(slideSize);
targetSize.width -= 50;
targetSize.height -= 150;
// Scale the image down if it's too big
if (imageSize.width > targetSize.width) {
float scaleForWidth = (float)targetSize.width / imageSize.width;
scale *= scaleForWidth;
imageSize.width *= scaleForWidth;
imageSize.height *= scaleForWidth;
}
if (imageSize.height > targetSize.height) {
float scaleForHeight = (float)targetSize.height / imageSize.height;
scale *= scaleForHeight;
imageSize.width *= scaleForHeight;
imageSize.height *= scaleForHeight;
}
int xTranslate = ((targetSize.width - imageSize.width) / 2) + 25;
int yTranslate = ((targetSize.height - imageSize.height) / 2) + 25;
// For non-rotated objects, the transformation matrix is:
// "<xscale> 0 0 <yscale> <xtranslate> <ytranslate>"
image.setAttribute("transformation", Float.toString(scale) + " 0 0 " +
Float.toString(scale) + " " +
Integer.toString(xTranslate) + " " +
Integer.toString(yTranslate));
drawables.appendChild(image);
// Move the title placeholder so that it is underneath the image, we put
// it 100 pixels up from the bottom of the slide, 90 pixels tall. We
// make it as wide as the slide (less 10 pixels on either side). The
// height is based on the font/font-size choosen.
//
// Here is a sample of XML of what the title will look like after we
// change it:
//
// <title location="10 500" size="780 90"
// vertical-alignment="tracks-master" visibility="visible"/>
Element titlePlaceholder = getFirstChildElementNamed(drawables, "title");
titlePlaceholder.setAttribute("location", "10 " +
Integer.toString(slideSize.height - 100));
titlePlaceholder.setAttribute("size",
Integer.toString(slideSize.width - 20) + " 90");
titlePlaceholder.setAttribute("visibility", "visible");
// Add a bullet point of level zero (the title) with the name of the
// image file in it.
//
// Here is a sample of XML of what the bullets section will look
// like when we're done:
//
// <bullets>
// <bullet level="0" marker-type="inherited">
// <content font-color="g1" font-name="Helvetica" font-size="48"
// paragraph-alignment="center">
// <![CDATA[image name]]>
// </content>
// </bullet>
// </bullets>
Element bulletGroup = document.createElement("bullets");
slide.appendChild(bulletGroup);
Element titleBullet = document.createElement("bullet");
titleBullet.setAttribute("level", "0");
titleBullet.setAttribute("marker-type", "inherited");
bulletGroup.appendChild(titleBullet);
// The default style for any styled text content is Helvetica 12, black.
// Text styles are *not* inherited (in terms of the file format). So we
// must set them explicitly here.
Element titleContent = document.createElement("content");
titleContent.setAttribute("font-name", "Helvetica");
titleContent.setAttribute("font-size", "48");
titleContent.setAttribute("font-color", "g1");
titleContent.setAttribute("paragraph-alignment", "center");
// NOTE: We use a CDATA section instead of a plain text node since the
// file name might contain special characters (like "<", ">" and "&").
titleContent.appendChild(
document.createCDATASection(sourceImage.getFile().getName()));
titleBullet.appendChild(titleContent);
}
/**
* Creates a new document with a minimal, default white-on-black theme. This
* theme contains only one master slide, for the "Blank" layout. The id of
* this master slide will be put in blankMasterId.
*/
private void createBlankTheme() {
// get the dom implementation
DOMImplementation impl = docBuilder.getDOMImplementation();
// What follows below is an example of the XML that we will be generating. Note
// that we're actually creating a whole document, not just the theme, but what
// Keynote calls a theme is simply a document with an empty slide list.
//
// <?xml version="1.0" encoding="UTF-8"?>
// <presentation version="36">
// <theme slide-size="800 600">
// <master-slides>
// <master-slide id="master-slide-1" name="Blank">
// <drawables>
// <title location="78 16" opacity="1" size="644 150"
// stroke-color="g1" stroke-width="1"
// vertical-alignment="middle" visibility="hidden">
// <styles>
// <fill-style fill-type="none"/>
// <dash-style pattern="none"/>
// <shadow-style opacity="0" radius="0"/>
// </styles>
// </title>
//
// <body bullet-indentation="0 20 48 75 102 130" location="78 170"
// size="644 352" ... other attributes same as title ... >
// <styles>
// ... same styles as title
// </styles>
// </body>
//
// <page-number location="380 570" size="39 23"
// ... other attributes same as title ... >
// <styles>
// ... same styles as title
// </styles>
// <text-attributes/>
// </page-number>
// </drawables>
//
// <prototype-bullets>
// <bullet level="0" marker-type="none" spacing="0">
// <content/>
// </bullet>
// ... more bullets w/ levels 1-5, otherwise identical
// </prototype-bullets>
//
// <background-fill-style fill-color="g0" fill-type="color"/>
//
// <transition-style duration="0" type="none" />
// </master-slide>
// </master-slides>
// </theme>
//
// <slide-list/>
// <ui-state/>
// </presentation>
// create a new, empty document
document = impl.createDocument("", "presentation",
impl.createDocumentType("APXL", null, null));
Element presentation = document.getDocumentElement();
presentation.setAttribute("version", "36");
// create the theme
Element theme = document.createElement("theme");
slideSize.setSize(800, 600);
theme.setAttribute("slide-size", Integer.toString(slideSize.width) +
" " + Integer.toString(slideSize.height));
presentation.appendChild(theme);
// create an empty slide-list
Element slideList = document.createElement("slide-list");
presentation.appendChild(slideList);
// create an empty UI state (The ui-state element is required, but
// is allowed to be empty.)
Element uiState = document.createElement("ui-state");
presentation.appendChild(uiState);
// create the master slides group
Element masterSlidesGroup = document.createElement("master-slides");
theme.appendChild(masterSlidesGroup);
// create a master slide (we need at least one)
//
// NOTE: All master slides must have an "id". The id must be unique
// within ALL elements in the document.
Element masterSlide = document.createElement("master-slide");
masterSlide.setAttribute("name", "Blank");
blankMasterId = "master-slide-1";
masterSlide.setAttribute("id", blankMasterId);
masterSlidesGroup.appendChild(masterSlide);
// create the drawables group
Element drawables = document.createElement("drawables");
masterSlide.appendChild(drawables);
// Every master MUST have a title, body and page-number placeholder,
// which must be explicitly styled. We'll use a convenience function
// to set the styles common to each type.
//
// NOTE: the placeholder locations and sizes used are copied from one
// of the default themes at 800x600
Element title = document.createElement("title");
setDefaultPlaceholderStyles(title);
title.setAttribute("location", "78 16");
title.setAttribute("size", "644 150");
drawables.appendChild(title);
Element body = document.createElement("body");
setDefaultPlaceholderStyles(body);
body.setAttribute("location", "78 170");
body.setAttribute("size", "644 352");
// Bullet indentation indicates how many pixels each level of
// bullets is indented. The values here are taken from a
// sample Keynote document.
body.setAttribute("bullet-indentation", "0 20 48 75 102 130");
drawables.appendChild(body);
// Page number needs a text attributes child, but it can be empty.
Element pageNumber = document.createElement("page-number");
setDefaultPlaceholderStyles(pageNumber);
pageNumber.setAttribute("location", "380 570");
pageNumber.setAttribute("size", "39 23");
pageNumber.appendChild(document.createElement("text-attributes"));
drawables.appendChild(pageNumber);
// Create the required 6 prototype bullets. (A bullet of level 0 is
// the slide's title.
Element prototypeBullets = document.createElement("prototype-bullets");
masterSlide.appendChild(prototypeBullets);
addPrototypeBullet(prototypeBullets, "0");
addPrototypeBullet(prototypeBullets, "1");
addPrototypeBullet(prototypeBullets, "2");
addPrototypeBullet(prototypeBullets, "3");
addPrototypeBullet(prototypeBullets, "4");
addPrototypeBullet(prototypeBullets, "5");
// Set a black background
Element background = document.createElement("background-fill-style");
background.setAttribute("fill-type", "color");
background.setAttribute("fill-color", "g0");
masterSlide.appendChild(background);
// Set a null transition
Element transition = document.createElement("transition-style");
transition.setAttribute("type", "none");
transition.setAttribute("duration", "0");
masterSlide.appendChild(transition);
}
/**
* A convenience function used by createBlankTheme() which sets the
* required attributes/styles of a master-slide placeholder to
* reasonable defaults.
*
* @see #createBlankTheme
*/
private void setDefaultPlaceholderStyles(Element placeholder) {
// NOTE: We need to set the stroke color and width
placeholder.setAttribute("opacity", "1");
placeholder.setAttribute("visibility", "hidden");
placeholder.setAttribute("stroke-color", "g1");
placeholder.setAttribute("stroke-width", "1");
placeholder.setAttribute("vertical-alignment", "middle");
Element styles = document.createElement("styles");
placeholder.appendChild(styles);
// No fill
Element fill = document.createElement("fill-style");
fill.setAttribute("fill-type", "none");
styles.appendChild(fill);
// No stroke
Element dash = document.createElement("dash-style");
dash.setAttribute("pattern", "none");
styles.appendChild(dash);
// No shadow
Element shadow = document.createElement("shadow-style");
shadow.setAttribute("opacity", "0");
shadow.setAttribute("radius", "0");
styles.appendChild(shadow);
}
/**
* A convenience function used by createBlankTheme() which creates
* a minimal, prototype bullet-point with the level specified.
*
* @return The newly created bullet element
*/
private Element addPrototypeBullet(Element prototypeBullets,
String level) {
Element bullet = document.createElement("bullet");
bullet.setAttribute("marker-type", "none");
bullet.setAttribute("spacing", "0");
bullet.setAttribute("level", level);
// The content element is required, even if empty. Text styles can be
// applied to the content to set the default styling for bullets of
// that level.
Element content = document.createElement("content");
bullet.appendChild(content);
prototypeBullets.appendChild(bullet);
return bullet;
}
/**
* Creates a new, blank slide with the minimum required styling/content. The
* master slide referenced by the new slide will be the one whose id is in
* blankMasterId.
*/
private Element createBlankSlide() {
Element slideList = getFirstChildElementNamed(
document.getDocumentElement(), "slide-list");
// Below is an example of the XML generated by this function
// <slide master-slide-id="(blankMasterId)">
// <drawables>
// <title vertical-alignment="tracks-master" visibility="hidden"/>
// <body vertical-alignment="tracks-master" visibility="hidden"/>
// </drawables>
//
// <transition-style type="inherited" />
// </slide>
Element slide = document.createElement("slide");
slide.setAttribute("master-slide-id", blankMasterId);
slideList.appendChild(slide);
Element drawables = document.createElement("drawables");
slide.appendChild(drawables);
// Title and body placeholders are required in every slide, but can
// be invisible
Element title = document.createElement("title");
title.setAttribute("visibility", "hidden");
title.setAttribute("vertical-alignment", "tracks-master");
drawables.appendChild(title);
Element body = document.createElement("body");
body.setAttribute("visibility", "hidden");
body.setAttribute("vertical-alignment", "tracks-master");
drawables.appendChild(body);
Element transition = document.createElement("transition-style");
transition.setAttribute("type", "inherited");
slide.appendChild(transition);
return slide;
}
/**
* Creates a new, empty document containing the theme from the referenced
* file. This is actually achieved by reading in the document and modifying
* it place to remove all slides, plus the contents UI state (which may
* refer to objects in the now-missing slides).
*/
private void readTheme(File themeFile) throws IOException, SAXException {
// Parse the document
document = docBuilder.parse(new File(themeFile, "presentation.apxl"));
// Get the slide size from the theme
Element theme = getFirstChildElementNamed(
document.getDocumentElement(), "theme");
StringTokenizer sizeTokens = new StringTokenizer(
theme.getAttribute("slide-size"), " ");
String width = sizeTokens.nextToken();
String height = sizeTokens.nextToken();
slideSize.setSize(Integer.parseInt(width), Integer.parseInt(height));
// Look for a master slide named "Blank"
Element masters = getFirstChildElementNamed(theme, "master-slides");
Element blankMaster = null;
Node currentChild = masters.getFirstChild();
while (currentChild != null) {
if ((currentChild.getNodeType() == Node.ELEMENT_NODE) &&
(currentChild.getNodeName().equals("master-slide")) &&
(((Element)currentChild).getAttribute("name").equals("Blank"))) {
blankMaster = (Element)currentChild;
break;
}
currentChild = currentChild.getNextSibling();
}
// no master named "Blank"? Just use the first master in the theme.
if (blankMaster == null) {
blankMaster = getFirstChildElementNamed(masters, "master-slide");
}
blankMasterId = blankMaster.getAttribute("id");
// Remove any slides in the slide list
Element slideList = getFirstChildElementNamed(
document.getDocumentElement(), "slide-list");
currentChild = slideList.getFirstChild();
while (currentChild != null) {
Node nextChild = currentChild.getNextSibling();
slideList.removeChild(currentChild);
currentChild = nextChild;
}
// Remove the content of the UI state
Element uiState = getFirstChildElementNamed(
document.getDocumentElement(), "ui-state");
currentChild = uiState.getFirstChild();
while (currentChild != null) {
Node nextChild = currentChild.getNextSibling();
uiState.removeChild(currentChild);
currentChild = nextChild;
}
}
/**
* Writes out the document bundle. This includes copying in the image data
* from the source images, plus copying any extra media from a source theme,
* and, of course, writing the document out as XML.
*/
private void writeDocument(String outputBundleName) throws IOException {
// First: ensure removal of any file that may already be in the way, and
// create the target bundle directory
File outputBundle = new File(outputBundleName);
boolean okay = true;
if (outputBundle.exists()) {
if (outputBundle.isDirectory()) {
okay = recursiveDelete(outputBundle);
} else {
okay = outputBundle.delete();
}
}
if (okay) {
okay = outputBundle.mkdir();
}
if (!okay) {
throw new IOException("Unable to create output bundle.");
}
// Copy the image data into the bundle, both the images that make up the
// slide-show, plus any media from the theme.
for (int i=0; i < imageFiles.length; i++) {
copyFileIntoDirectory(imageFiles[i].getFile(), outputBundle);
}
if (templateBundle != null) {
File[] templateFiles = templateBundle.listFiles();
for (int i=0; i < templateFiles.length; i++) {
copyFileIntoDirectory(templateFiles[i], outputBundle);
}
}
// NOTE: we write the presentation.apxl AFTER copying the data from the
// theme, because the theme will contain a presentation.apxl of its own.
// If we do the copy afterwards, we'd have to special case
// "presentation.apxl", or we'll blow away the XML we've just created!
File outputXML = new File(outputBundle, "presentation.apxl");
// Write out the DOM tree
DOMImplementationLS domls =
(DOMImplementationLS)docBuilder.getDOMImplementation();
DOMWriter writer = domls.createDOMWriter();
writer.setEncoding("UTF-8");
// pretty printing should give more text-editor friendly content, but
// unfortunately the current implementation of the DOMWriter doesn't
// support the setFeature functionality.
// writer.setFeature("pretty-print", true);
writer.writeNode(new FileOutputStream(outputXML), document);
}
/**
* Parses the argument list and fills in the appropriate member variables.
*/
private void parseArgs(String args[]) {
ArrayList imageList = new ArrayList();
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("-t")) {
if (i == args.length - 1) {
printHelpAndExit();
}
i++;
templateBundle = new File(args[i]);
} else if (args[i].equals("-help")) {
printHelpAndExit();
} else {
if (i == args.length - 1) {
// If we're on the last argument, then this is the name of
// the output bundle
outputBundleName = args[i];
} else {
// If not the last argument, then this is a source image. If
// it is a valid image file, add it to the image list.
// NOTE: It might be useful to recursively search any
// specified directories for images.
ImageInfo theInfo = new ImageInfo(args[i]);
if (theInfo.isValid()) {
imageList.add(theInfo);
} else {
System.err.println("Skipping \"" + args[i] +
"\": not a recognised image file.");
}
}
}
}
if ((outputBundleName == null) || (imageList.isEmpty())) {
printHelpAndExit();
}
if (!outputBundleName.endsWith(".key")) {
outputBundleName += ".key";
}
imageFiles = (ImageInfo[])imageList.toArray(new ImageInfo[0]);
}
/**
* Walks the immediate decendents of a DOM element and returns the first one
* with the given tagname.
*/
private static Element getFirstChildElementNamed(Element inParent,
String inString) {
Element result = null;
Node currentChild = inParent.getFirstChild();
while (currentChild != null) {
if ((currentChild.getNodeType() == Node.ELEMENT_NODE) &&
(currentChild.getNodeName().equals(inString))) {
result = (Element)currentChild;
break;
}
currentChild = currentChild.getNextSibling();
}
return result;
}
/**
* Copies the specified file or directory into the specified directory,
* keeping the original file name. Works recursively on directories.
*/
private static void copyFileIntoDirectory(File file, File directory)
throws IOException {
File target = new File(directory, file.getName());
if (file.isDirectory()) {
target.mkdir();
File[] contents = file.listFiles();
for (int i=0; i<contents.length; i++) {
copyFileIntoDirectory(contents[i], target);
}
} else {
FileInputStream streamIn = new FileInputStream(file);
FileOutputStream streamOut = new FileOutputStream(target);
// 16k is likely to be a multiple of the file block size.
byte[] buffer = new byte[16 * 1024];
int readBytes = 0;
while((readBytes=streamIn.read(buffer)) != -1) {
streamOut.write(buffer, 0, readBytes);
}
streamIn.close();
streamOut.close();
}
}
/**
* Deletes a directory and all its contents. The argument must refer to a
* directory.
*/
private static boolean recursiveDelete(File directory) throws IOException {
File[] contents = directory.listFiles();
boolean deleteOkay = true;
for (int i=0; i<contents.length; i++) {
if (contents[i].isDirectory()) {
deleteOkay = recursiveDelete(contents[i]);
} else {
deleteOkay = contents[i].delete();
}
if (!deleteOkay) {
break;
}
}
if (deleteOkay) {
deleteOkay = directory.delete();
}
return deleteOkay;
}
/**
* A private class that abstracts the functionality of checking to see if
* a particular file is a valid image file, and if so, what its dimensions
* are. Currently it is implemented in terms of NSImage, although it would
* relatively straight-forward to include a fallback case that uses
* java.awt.Image on platforms where NSImage is not available. (Or some
* other image library, like JIMI.) NSImage was used here since it supports
* all of the image types that Keynote does, whereas AWT's Image class only
* supports a subset of that (GIF, JPEG and PNG).
*
* If you wished to use some other image library, you would add code in the
* catch block of the constructor that used the alternate classes to check
* the image file. In order to get the NSImage code to compile on other
* platforms, however, you would need to use introspection to call the
* methods instead of simply calling them directly.
*/
private static class ImageInfo {
Dimension size = null;
File file = null;
static {
try {
// NOTE: NSImageReps don't work correctly unless you access the
// NSImage class first. So, in the static initialiser, we create
// a throw-away image instance (supressing all exceptions and
// errors; we'll deal with any problems when they come up later).
new com.apple.cocoa.application.NSImage();
} catch(Throwable t) {}
}
/**
* Creates a new ImageInfo instance, which immediately analyzes the image
* file to see if it is a valid image and, if so, what its dimensions
* are.
*/
public ImageInfo(String imageFilename) {
try {
// NOTE: When calling a static member functions, the class in
// question is loaded as soon as the function is called. Thus,
// we must put the call to imageRepWithContentsOfFile() in a
// sub-function, or else the constructor itself will trigger
// the NoClassDefFoundError.
examineImageWithCocoa(imageFilename);
} catch (NoClassDefFoundError e) {
System.err.println("NSImageRep class not found. PictureShow requires Cocoa for Java (Mac OS X).");
System.err.println("If running on Mac OS X, ensure that your classpath includes /System/Library/Java.");
System.exit(-1);
// For code that needs to run on other platforms, you should
// fall back to an alternate image library here.
}
if (size != null) {
file = new File(imageFilename);
}
}
/**
* Tries to read the image file using NSImageRep. Determines the
* dimensions of the image if succesful.
*/
private void examineImageWithCocoa(String imageFilename) {
NSImageRep imageRep = NSImageRep.imageRepWithContentsOfFile(imageFilename);
if (imageRep != null) {
// When possible, we use the pixels wide/high metric, since that
// is correct for bitmaps that use non-standard DPI. However, in
// the case of PDF files, there is no pixels wide/high metric, so
// we use the normal size in that case.
if ((imageRep.pixelsWide() == 0) || (imageRep.pixelsHigh() == 0)) {
size = imageRep.size().toAWTDimension();
} else {
size = new Dimension(imageRep.pixelsWide(),
imageRep.pixelsHigh());
}
}
}
/**
* Returns true if the path passed in the constructer references a valid
* image file, of a known type.
*/
public boolean isValid() {
return (size != null);