Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 52eea6f15518360a1110f323721ed472bb57192c (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2011-2014 Torkild U. Resheim.
 *
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse  License v1.0 which
 * accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Torkild U. Resheim - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylyn.docs.epub.core;

import java.io.File;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.eclipse.emf.ecore.util.FeatureMapUtil.FeatureEList;
import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
import org.eclipse.mylyn.docs.epub.dc.Creator;
import org.eclipse.mylyn.docs.epub.dc.Date;
import org.eclipse.mylyn.docs.epub.dc.Identifier;
import org.eclipse.mylyn.docs.epub.dc.Language;
import org.eclipse.mylyn.docs.epub.dc.Rights;
import org.eclipse.mylyn.docs.epub.dc.Subject;
import org.eclipse.mylyn.docs.epub.dc.Title;
import org.eclipse.mylyn.docs.epub.opf.Item;

/**
 * Simplified representation of an {@link Publication} - designed for common use cases and to make it easier to edit
 * instances in user interfaces etc.
 *
 * @author Torkild U. Resheim
 * @see Publication
 * @since 2.0 TODO: add tests for this type
 */
public class PublicationProxy {

	class ValueComparator implements Comparator<String> {

		Map<String, String> base;

		public ValueComparator(Map<String, String> base) {
			this.base = base;
		}

		public int compare(String a, String b) {
			String key_a = a;
			String key_b = b;

			return key_a.compareTo(key_b);
		}
	}

	private static final String EMPTY_STRING = ""; //$NON-NLS-1$

	private final static String ID = "epub-id"; //$NON-NLS-1$

	private static final String STYLING_ID = "styling"; //$NON-NLS-1$

	private static final EStructuralFeature TEXT = XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_Text();

	private final Publication publication;

	private final File markupFile;

	TreeMap<String, String> sorted_locales;

	public PublicationProxy() {
		publication = Publication.getVersion2Instance();
		markupFile = null;
	}

	public PublicationProxy(Publication publication, File markupFile) {
		this.publication = publication;
		this.markupFile = markupFile;
		String id = publication.getPackage().getUniqueIdentifier();
		if (id == null || id.trim().length() == 0) {
			publication.getPackage().setUniqueIdentifier(ID);
			publication.addIdentifier(ID, "UUID", UUID.randomUUID().toString()); //$NON-NLS-1$
		}
		// Clear everything except the metadata
		publication.getPackage().getManifest().getItems().clear();
		publication.getPackage().getGuide().getGuideItems().clear();
		publication.getPackage().getSpine().getSpineItems().clear();

		sorted_locales = new TreeMap<String, String>();
		String[] iso639s = Locale.getISOLanguages();
		for (String iso639 : iso639s) {
			Locale locale = new Locale(iso639);
			sorted_locales.put(locale.getDisplayLanguage(), locale.getLanguage());
		}
	}

	/**
	 * Returns the path to the file used as a cover image if specified, otherwise an empty string is returned.
	 *
	 * @return the cover image file
	 */
	public String getCover() {
		Item item = publication.getItemById(Publication.COVER_IMAGE_ID);
		if (item == null) {
			return EMPTY_STRING;
		} else {
			return item.getFile();
		}
	}

	/**
	 * Returns the name of the first instance of an {@link Creator} in the publication. If a value has not been assigned
	 * an empty string will be returned.
	 *
	 * @return the creator name
	 */
	@SuppressWarnings("rawtypes")
	public String getCreator() {
		EList<Creator> creators = publication.getPackage().getMetadata().getCreators();
		if (creators.size() > 0) {
			FeatureMap fm = creators.get(0).getMixed();
			Object o = fm.get(TEXT, false);
			if (o instanceof FeatureEList) {
				if (((FeatureEList) o).size() > 0) {
					return ((FeatureEList) o).get(0).toString();
				}
			}
		}
		return EMPTY_STRING;
	}

	@SuppressWarnings("rawtypes")
	public String getIdentifier() {
		EList<Identifier> identifiers = publication.getPackage().getMetadata().getIdentifiers();
		if (identifiers.size() > 0) {
			FeatureMap fm = identifiers.get(0).getMixed();
			Object o = fm.get(TEXT, false);
			if (o instanceof FeatureEList) {
				if (((FeatureEList) o).size() > 0) {
					return ((FeatureEList) o).get(0).toString();
				}
			}
		}
		return EMPTY_STRING;
	}

	@SuppressWarnings("rawtypes")
	public String getLanguage() {
		EList<Language> languages = publication.getPackage().getMetadata().getLanguages();
		if (languages.size() > 0) {
			FeatureMap fm = languages.get(0).getMixed();
			Object o = fm.get(TEXT, false);
			if (o instanceof FeatureEList) {
				if (((FeatureEList) o).size() > 0) {
					String iso639 = ((FeatureEList) o).get(0).toString();
					Locale l = new Locale(iso639);
					return l.getDisplayLanguage();
				}
			}
		}
		return EMPTY_STRING;
	}

	/**
	 * Returns a sorted map of locales that can be used to specify the {@link Language} of the publication.
	 *
	 * @return map of locales
	 */
	public Map<String, String> getLocales() {
		return sorted_locales;
	}

	public File getMarkupFile() {
		return markupFile;
	}

	public Publication getOPSPublication() {
		return publication;
	}

	@SuppressWarnings("rawtypes")
	public String getPublicationDate() {
		EList<Date> dates = publication.getPackage().getMetadata().getDates();
		if (dates.size() > 0) {
			for (Date date : dates) {
				if (date.getEvent().equals("publication")) { //$NON-NLS-1$
					FeatureMap fm = date.getMixed();
					Object o = fm.get(TEXT, false);
					if (o instanceof FeatureEList) {
						if (((FeatureEList) o).size() > 0) {
							return ((FeatureEList) o).get(0).toString();
						}
					}
				}
			}
		}
		return EMPTY_STRING;
	}

	@SuppressWarnings("rawtypes")
	public String getRights() {
		EList<Rights> rights = publication.getPackage().getMetadata().getRights();
		if (rights.size() > 0) {
			FeatureMap fm = rights.get(0).getMixed();
			Object o = fm.get(TEXT, false);
			if (o instanceof FeatureEList) {
				if (((FeatureEList) o).size() > 0) {
					return ((FeatureEList) o).get(0).toString();
				}
			}
		}
		return EMPTY_STRING;
	}

	public String getScheme() {
		Identifier ident = publication.getPackage().getMetadata().getIdentifiers().get(0);
		return ident.getScheme();
	}

	public String getStyleSheet() {
		List<Item> stylesheets = publication.getItemsByMIMEType(Publication.MIMETYPE_CSS);
		if (stylesheets.isEmpty()) {
			return EMPTY_STRING;
		} else {
			return stylesheets.get(0).getHref();
		}
	}

	@SuppressWarnings("rawtypes")
	public String getSubject() {
		EList<Subject> subjects = publication.getPackage().getMetadata().getSubjects();
		if (subjects.size() > 0) {
			FeatureMap fm = subjects.get(0).getMixed();
			Object o = fm.get(TEXT, false);
			if (o instanceof FeatureEList) {
				if (((FeatureEList) o).size() > 0) {
					return ((FeatureEList) o).get(0).toString();
				}
			}
		}
		return EMPTY_STRING;
	}

	@SuppressWarnings("rawtypes")
	public String getTitle() {
		EList<Title> titles = publication.getPackage().getMetadata().getTitles();
		if (titles.size() > 0) {
			FeatureMap fm = titles.get(0).getMixed();
			Object o = fm.get(TEXT, false);
			if (o instanceof FeatureEList) {
				if (((FeatureEList) o).size() > 0) {
					return ((FeatureEList) o).get(0).toString();
				}
			}
		}
		return EMPTY_STRING;
	}

	public void setCover(String cover) {
		if (cover.length() > 0) {
			publication.setCover(new File(cover), "Cover page"); //$NON-NLS-1$
		}
	}

	public void setCreator(String creator) {
		publication.getPackage().getMetadata().getCreators().clear();
		publication.addCreator(creator);
	}

	public void setIdentifier(String identifier) {
		String scheme = getScheme();
		publication.getPackage().getMetadata().getIdentifiers().clear();
		publication.addIdentifier(ID, scheme, identifier);
		publication.getPackage().setUniqueIdentifier(ID);
	}

	/**
	 * Sets the language of the publication. If it already contains one or more language specifications, they will be
	 * removed.
	 *
	 * @param language
	 *            the RFC-3066 format of the language code
	 */
	public void setLanguage(String language) {
		publication.getPackage().getMetadata().getLanguages().clear();
		publication.addLanguage(sorted_locales.get(language));
	}

	/**
	 * Sets the publication date. Must be represented in the form YYYY[-MM[-DD]]: a required 4-digit year, an optional
	 * 2-digit month, and if the month is given, an optional 2-digit day of month.
	 *
	 * @param date
	 *            the date in ISO-8601 format
	 */
	public void setPublicationDate(String date) {
		publication.getPackage().getMetadata().getDates().clear();
		publication.addDate(null, date, "publication"); //$NON-NLS-1$
	}

	public void setRights(String rights) {
		publication.getPackage().getMetadata().getRights().clear();
		publication.addRights(rights);
	}

	public void setScheme(String schemeName) {
		String identifier = getIdentifier();
		publication.getPackage().getMetadata().getIdentifiers().clear();
		publication.getPackage().setUniqueIdentifier(ID);
		publication.addIdentifier(ID, schemeName, identifier);
	}

	public void setStyleSheet(String css) {
		if (css.length() > 0) {
			publication.getPackage().getManifest().getItems().remove(publication.getItemById(STYLING_ID));
			publication.addItem(STYLING_ID, null, new File(css), null, null, false, false, true);
		}
	}

	public void setSubject(String subject) {
		publication.getPackage().getMetadata().getSubjects().clear();
		publication.addSubject(subject);
	}

	public void setTitle(String title) {
		publication.getPackage().getMetadata().getTitles().clear();
		publication.addTitle(title);
	}
}

Back to the top