1 | /**
|
---|
2 | * HTMLWriter.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.io;
|
---|
20 |
|
---|
21 | import java.awt.Image;
|
---|
22 | import java.awt.image.BufferedImage;
|
---|
23 | import java.io.IOException;
|
---|
24 | import java.util.List;
|
---|
25 |
|
---|
26 | import org.expeditee.actions.Misc;
|
---|
27 | import org.expeditee.gui.FrameIO;
|
---|
28 | import org.expeditee.items.FramePicture;
|
---|
29 | import org.expeditee.items.Item;
|
---|
30 | import org.expeditee.items.Picture;
|
---|
31 | import org.expeditee.items.Text;
|
---|
32 |
|
---|
33 | public class HTMLWriter extends AbstractHTMLWriter {
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | protected void writeTitle(Text toWrite, List<Item> items)
|
---|
37 | throws IOException {
|
---|
38 | int indent = getIndent();
|
---|
39 | if (indent == 0)
|
---|
40 | return;
|
---|
41 |
|
---|
42 | String heading = toWrite.getText();
|
---|
43 | String tag = "h" + indent;
|
---|
44 | // indenting for tag
|
---|
45 | indent();
|
---|
46 | _writer.write("<" + tag + ">" + heading + "</" + tag + ">"
|
---|
47 | + ItemWriter.NEW_LINE);
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | protected void writeText(Text text) throws IOException {
|
---|
52 | indent();
|
---|
53 | _writer.write("<p>");
|
---|
54 | _writer.write(text.getText().replace('\n', ' '));
|
---|
55 | _writer.write("</p>" + ItemWriter.NEW_LINE);
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | protected void writePicture(Picture pic) throws IOException {
|
---|
60 | // TODO handle different colored @f's of the same frame
|
---|
61 | // TODO handle cropped images
|
---|
62 | Image image = null;
|
---|
63 |
|
---|
64 | if (pic instanceof FramePicture || pic.isCropped()) {
|
---|
65 | image = pic.getImage();
|
---|
66 | // Crop the image
|
---|
67 | BufferedImage bufferedImage = new BufferedImage(pic
|
---|
68 | .getUnscaledWidth(), pic.getUnscaledHeight(),
|
---|
69 | BufferedImage.TYPE_INT_ARGB);
|
---|
70 | bufferedImage.getGraphics().drawImage(image, 0, 0,
|
---|
71 | pic.getUnscaledWidth(), pic.getUnscaledHeight(),
|
---|
72 | pic.getStart().x, pic.getStart().y, pic.getEnd().x,
|
---|
73 | pic.getEnd().y, null);
|
---|
74 | image = bufferedImage;
|
---|
75 | } else {
|
---|
76 | image = pic.getImage();
|
---|
77 | }
|
---|
78 |
|
---|
79 | String filesFolder = getFilesFolder();
|
---|
80 | String fileName;
|
---|
81 | // If its a bufferedImage then just write it out to the files directory
|
---|
82 | // This means it is probably a FrameImage
|
---|
83 | if (image instanceof BufferedImage) {
|
---|
84 | String link = pic.getAbsoluteLink();
|
---|
85 | // Account for the possiblitly of an unlinked buffered image
|
---|
86 | fileName = link == null ? ("Image" + pic.getID()) : link;
|
---|
87 | fileName = Misc.SaveImage((BufferedImage) image, "PNG",
|
---|
88 | FrameIO.EXPORTS_DIR + filesFolder, fileName);
|
---|
89 | } else {// It is a normal Image stored somewhere
|
---|
90 | fileName = pic.getName();
|
---|
91 |
|
---|
92 | String oldImageName = FrameIO.IMAGES_PATH + fileName;
|
---|
93 | String newImageName = FrameIO.EXPORTS_DIR + filesFolder + fileName;
|
---|
94 | try {
|
---|
95 | FrameIO.copyFile(oldImageName, newImageName);
|
---|
96 | } catch (Exception e) {
|
---|
97 | filesFolder = "";
|
---|
98 | }
|
---|
99 | if (filesFolder.equals("")) {
|
---|
100 | filesFolder = "../" + FrameIO.IMAGES_FOLDER;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | indent();
|
---|
104 | StringBuffer imageTag = new StringBuffer("<img src=\"");
|
---|
105 | imageTag.append(filesFolder).append(fileName);
|
---|
106 | imageTag.append("\" height=").append(pic.getHeight());
|
---|
107 | imageTag.append(" width=").append(pic.getWidth());
|
---|
108 | imageTag.append(" border=1>");
|
---|
109 | _writer.write(imageTag.toString());
|
---|
110 | _writer.write(ItemWriter.NEW_LINE);
|
---|
111 | }
|
---|
112 | }
|
---|