|
Lines 1-243
Link Here
|
| 1 |
/******************************************************************************* |
|
|
| 2 |
* Copyright (c) 2011 Torkild U. Resheim. |
| 3 |
* |
| 4 |
* All rights reserved. This program and the accompanying materials are made |
| 5 |
* available under the terms of the Eclipse Public License v1.0 which |
| 6 |
* accompanies this distribution, and is available at |
| 7 |
* http://www.eclipse.org/legal/epl-v10.html |
| 8 |
* |
| 9 |
* Contributors: Torkild U. Resheim - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.mylyn.docs.epub.ant; |
| 12 |
|
| 13 |
import java.io.File; |
| 14 |
import java.util.ArrayList; |
| 15 |
|
| 16 |
import org.apache.tools.ant.BuildException; |
| 17 |
import org.apache.tools.ant.DirectoryScanner; |
| 18 |
import org.apache.tools.ant.Project; |
| 19 |
import org.apache.tools.ant.Task; |
| 20 |
import org.eclipse.mylyn.docs.epub.core.EPUB; |
| 21 |
import org.eclipse.mylyn.docs.epub.core.OPSPublication; |
| 22 |
import org.eclipse.mylyn.docs.epub.opf.Role; |
| 23 |
import org.eclipse.mylyn.docs.epub.opf.Type; |
| 24 |
|
| 25 |
/** |
| 26 |
* Assemble a new EPUB. |
| 27 |
* |
| 28 |
* |
| 29 |
* @author Torkild U. Resheim |
| 30 |
* @ant.task name="epub" category="epub" |
| 31 |
*/ |
| 32 |
public class EpubTask extends Task { |
| 33 |
|
| 34 |
private final OPSPublication ops = OPSPublication.getVersion2Instance(); |
| 35 |
|
| 36 |
private final ArrayList<FileSetType> filesets; |
| 37 |
|
| 38 |
private TocType toc = null; |
| 39 |
|
| 40 |
private File workingFolder; |
| 41 |
|
| 42 |
private File epubFile; |
| 43 |
|
| 44 |
public EpubTask() { |
| 45 |
super(); |
| 46 |
filesets = new ArrayList<FileSetType>(); |
| 47 |
} |
| 48 |
|
| 49 |
public void addConfiguredContributor(ContributorType item) { |
| 50 |
if (item.role == null) { |
| 51 |
ops.addContributor(item.id, item.lang, item.name, null, item.fileAs); |
| 52 |
} else { |
| 53 |
ops.addContributor(item.id, item.lang, item.name, Role.get(item.role), item.fileAs); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public void addConfiguredCover(CoverType item) { |
| 58 |
ops.setCover(new File(item.image), item.value); |
| 59 |
} |
| 60 |
|
| 61 |
public void addConfiguredCoverage(CoverageType coverage) { |
| 62 |
ops.addCoverage(coverage.id, coverage.lang, coverage.text); |
| 63 |
} |
| 64 |
|
| 65 |
public void addConfiguredCreator(CreatorType item) { |
| 66 |
if (item.role == null) { |
| 67 |
ops.addCreator(item.id, item.lang, item.name, null, item.fileAs); |
| 68 |
} else { |
| 69 |
ops.addCreator(item.id, item.lang, item.name, Role.get(item.role), item.fileAs); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public void addConfiguredDate(DateType item) { |
| 74 |
ops.addDate(item.id, item.date, item.event); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* The FileSet sub-element is used to add EPUB artifacts that are not a part |
| 79 |
* of the main text. This can be graphical items and styling (CSS). |
| 80 |
* |
| 81 |
* @param fs |
| 82 |
* the fileset to add |
| 83 |
*/ |
| 84 |
public void addConfiguredFileSet(FileSetType fs) { |
| 85 |
filesets.add(fs); |
| 86 |
} |
| 87 |
|
| 88 |
public void addConfiguredFormat(FormatType format) { |
| 89 |
ops.addFormat(format.id, format.text); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* @ant.required |
| 94 |
*/ |
| 95 |
public void addConfiguredIdentifier(IdentifierType identifier) { |
| 96 |
ops.addIdentifier(identifier.id, identifier.scheme, identifier.value); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* @ant.required |
| 101 |
*/ |
| 102 |
public void addConfiguredItem(ItemType item) { |
| 103 |
ops.addItem(item.id, item.lang, item.file, item.dest, item.type, item.spine, item.linear, item.noToc); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* @ant.required |
| 108 |
*/ |
| 109 |
public void addConfiguredLanguage(LanguageType language) { |
| 110 |
ops.addLanguage(language.id, language.code); |
| 111 |
} |
| 112 |
|
| 113 |
public void addConfiguredMeta(MetaType item) { |
| 114 |
ops.addMeta(item.name, item.content); |
| 115 |
} |
| 116 |
|
| 117 |
public void addConfiguredPublisher(PublisherType publisher) { |
| 118 |
ops.addPublisher(publisher.id, publisher.lang, publisher.text); |
| 119 |
} |
| 120 |
|
| 121 |
public void addConfiguredReference(ReferenceType reference) { |
| 122 |
Type type = Type.get(reference.type); |
| 123 |
if (type == null) { |
| 124 |
throw new BuildException("Unknown reference type " + reference.type); |
| 125 |
} |
| 126 |
ops.addReference(reference.href, reference.title, type); |
| 127 |
} |
| 128 |
|
| 129 |
public void addConfiguredRelation(RelationType relation) { |
| 130 |
ops.addRelation(relation.id, relation.lang, relation.text); |
| 131 |
} |
| 132 |
|
| 133 |
public void addConfiguredRights(RightsType rights) { |
| 134 |
ops.addRights(rights.id, rights.lang, rights.text); |
| 135 |
} |
| 136 |
|
| 137 |
public void addConfiguredSource(SourceType source) { |
| 138 |
ops.addSource(source.id, source.lang, source.text); |
| 139 |
} |
| 140 |
|
| 141 |
public void addConfiguredSubject(SubjectType subject) { |
| 142 |
ops.addSubject(subject.id, subject.lang, subject.text); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* @ant.required |
| 147 |
*/ |
| 148 |
public void addConfiguredTitle(TitleType title) { |
| 149 |
ops.addTitle(title.id, title.lang, title.text); |
| 150 |
} |
| 151 |
|
| 152 |
public void addConfiguredToc(TocType toc) { |
| 153 |
if (this.toc != null) { |
| 154 |
throw new BuildException("Only one table of contents (toc) declaration is allowed."); |
| 155 |
} |
| 156 |
this.toc = toc; |
| 157 |
} |
| 158 |
|
| 159 |
public void addConfiguredType(org.eclipse.mylyn.docs.epub.ant.TypeType type) { |
| 160 |
ops.addType(type.id, type.text); |
| 161 |
} |
| 162 |
|
| 163 |
private void addFilesets() { |
| 164 |
for (FileSetType fs : filesets) { |
| 165 |
if (fs.getProject() == null) { |
| 166 |
log("Deleting fileset with no project specified;" + " assuming executing project", Project.MSG_VERBOSE); |
| 167 |
fs = (FileSetType) fs.clone(); |
| 168 |
fs.setProject(getProject()); |
| 169 |
} |
| 170 |
final File fsDir = fs.getDir(); |
| 171 |
if (fsDir == null) { |
| 172 |
throw new BuildException("File or Resource without directory or file specified"); |
| 173 |
} else if (!fsDir.isDirectory()) { |
| 174 |
throw new BuildException("Directory does not exist:" + fsDir); |
| 175 |
} |
| 176 |
DirectoryScanner ds = fs.getDirectoryScanner(); |
| 177 |
String[] includedFiles = ds.getIncludedFiles(); |
| 178 |
for (int i = 0; i < includedFiles.length; i++) { |
| 179 |
String filename = includedFiles[i].replace('\\', '/'); |
| 180 |
filename = filename.substring(filename.lastIndexOf("/") + 1); |
| 181 |
File base = ds.getBasedir(); |
| 182 |
File found = new File(base, includedFiles[i]); |
| 183 |
ops.addItem(null, fs.lang, found, fs.dest, null, false, true, false); |
| 184 |
} |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public void execute() throws BuildException { |
| 192 |
validate(); |
| 193 |
addFilesets(); |
| 194 |
if (toc != null) { |
| 195 |
if (toc.generate) { |
| 196 |
ops.setGenerateToc(true); |
| 197 |
} else if (toc.file != null) { |
| 198 |
ops.setTableOfContents(toc.file); |
| 199 |
} |
| 200 |
} |
| 201 |
try { |
| 202 |
EPUB epub = new EPUB(); |
| 203 |
epub.add(ops); |
| 204 |
if (workingFolder == null) { |
| 205 |
epub.pack(epubFile); |
| 206 |
} else { |
| 207 |
epub.pack(epubFile, workingFolder); |
| 208 |
} |
| 209 |
|
| 210 |
} catch (Exception e) { |
| 211 |
throw new BuildException(e); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* @ant.not-required Automatically add referenced resources. |
| 217 |
*/ |
| 218 |
public void setIncludeReferenced(boolean automatic) { |
| 219 |
ops.setIncludeReferencedResources(automatic); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* |
| 224 |
* |
| 225 |
* @param file |
| 226 |
* path to the generated EPUB file. |
| 227 |
*/ |
| 228 |
public void setFile(File file) { |
| 229 |
this.epubFile = file; |
| 230 |
} |
| 231 |
|
| 232 |
public void setIdentifierId(String identifierId) { |
| 233 |
ops.setIdentifierId(identifierId); |
| 234 |
} |
| 235 |
|
| 236 |
public void setWorkingFolder(File workingFolder) { |
| 237 |
this.workingFolder = workingFolder; |
| 238 |
} |
| 239 |
|
| 240 |
private void validate() { |
| 241 |
} |
| 242 |
|
| 243 |
} |