File size: 17,521 Bytes
fab29d7 |
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 |
using System.Collections.Generic;
using System.Xml.Linq;
using VersOne.Epub.Schema;
using VersOne.Epub.Utils;
namespace VersOne.Epub.Internal
{
internal static class MetadataReader
{
public static EpubMetadata ReadMetadata(XElement metadataNode)
{
List<EpubMetadataTitle> titles = new();
List<EpubMetadataCreator> creators = new();
List<EpubMetadataSubject> subjects = new();
List<EpubMetadataDescription> descriptions = new();
List<EpubMetadataPublisher> publishers = new();
List<EpubMetadataContributor> contributors = new();
List<EpubMetadataDate> dates = new();
List<EpubMetadataType> types = new();
List<EpubMetadataFormat> formats = new();
List<EpubMetadataIdentifier> identifiers = new();
List<EpubMetadataSource> sources = new();
List<EpubMetadataLanguage> languages = new();
List<EpubMetadataRelation> relations = new();
List<EpubMetadataCoverage> coverages = new();
List<EpubMetadataRights> rights = new();
List<EpubMetadataLink> links = new();
List<EpubMetadataMeta> metaItems = new();
foreach (XElement metadataItemNode in metadataNode.Elements())
{
switch (metadataItemNode.GetLowerCaseLocalName())
{
case "title":
EpubMetadataTitle title = ReadTitle(metadataItemNode);
titles.Add(title);
break;
case "creator":
EpubMetadataCreator creator = ReadCreator(metadataItemNode);
creators.Add(creator);
break;
case "subject":
EpubMetadataSubject subject = ReadSubject(metadataItemNode);
subjects.Add(subject);
break;
case "description":
EpubMetadataDescription description = ReadDescription(metadataItemNode);
descriptions.Add(description);
break;
case "publisher":
EpubMetadataPublisher publisher = ReadPublisher(metadataItemNode);
publishers.Add(publisher);
break;
case "contributor":
EpubMetadataContributor contributor = ReadContributor(metadataItemNode);
contributors.Add(contributor);
break;
case "date":
EpubMetadataDate date = ReadDate(metadataItemNode);
dates.Add(date);
break;
case "type":
EpubMetadataType type = ReadType(metadataItemNode);
types.Add(type);
break;
case "format":
EpubMetadataFormat format = ReadFormat(metadataItemNode);
formats.Add(format);
break;
case "identifier":
EpubMetadataIdentifier identifier = ReadIdentifier(metadataItemNode);
identifiers.Add(identifier);
break;
case "source":
EpubMetadataSource source = ReadSource(metadataItemNode);
sources.Add(source);
break;
case "language":
EpubMetadataLanguage language = ReadLanguage(metadataItemNode);
languages.Add(language);
break;
case "relation":
EpubMetadataRelation relation = ReadRelation(metadataItemNode);
relations.Add(relation);
break;
case "coverage":
EpubMetadataCoverage coverage = ReadCoverage(metadataItemNode);
coverages.Add(coverage);
break;
case "rights":
EpubMetadataRights rightsItem = ReadRightsItem(metadataItemNode);
rights.Add(rightsItem);
break;
case "link":
EpubMetadataLink link = ReadLink(metadataItemNode);
links.Add(link);
break;
case "meta":
EpubMetadataMeta meta = ReadMeta(metadataItemNode);
metaItems.Add(meta);
break;
}
}
return new(titles, creators, subjects, descriptions, publishers, contributors, dates, types, formats, identifiers, sources,
languages, relations, coverages, rights, links, metaItems);
}
public static EpubMetadataLink ReadLink(XElement linkNode)
{
string? href = null;
string? id = null;
string? mediaType = null;
List<EpubMetadataLinkProperty>? properties = null;
string? refines = null;
List<EpubMetadataLinkRelationship>? relationships = null;
string? hrefLanguage = null;
foreach (XAttribute linkNodeAttribute in linkNode.Attributes())
{
string attributeValue = linkNodeAttribute.Value;
switch (linkNodeAttribute.GetLowerCaseLocalName())
{
case "href":
href = attributeValue;
break;
case "id":
id = attributeValue;
break;
case "media-type":
mediaType = attributeValue;
break;
case "properties":
properties = EpubMetadataLinkPropertyParser.ParsePropertyList(attributeValue);
break;
case "refines":
refines = attributeValue;
break;
case "rel":
relationships = EpubMetadataLinkRelationshipParser.ParseRelationshipList(attributeValue);
break;
case "hreflang":
hrefLanguage = attributeValue;
break;
}
}
if (href == null)
{
throw new EpubPackageException("Incorrect EPUB metadata link: href is missing.");
}
if (relationships == null)
{
throw new EpubPackageException("Incorrect EPUB metadata link: rel is missing.");
}
return new(href, id, mediaType, properties, refines, relationships, hrefLanguage);
}
private static EpubMetadataTitle ReadTitle(XElement titleNode)
{
ReadMetadataItemWithIdDirLangAndContent(titleNode, out string? id, out EpubTextDirection? textDirection, out string? language, out string title);
return new(title, id, textDirection, language);
}
private static EpubMetadataCreator ReadCreator(XElement creatorNode)
{
string? id = null;
string? fileAs = null;
string? role = null;
EpubTextDirection? textDirection = null;
string? language = null;
foreach (XAttribute creatorNodeAttribute in creatorNode.Attributes())
{
string attributeValue = creatorNodeAttribute.Value;
switch (creatorNodeAttribute.GetLowerCaseLocalName())
{
case "id":
id = attributeValue;
break;
case "file-as":
fileAs = attributeValue;
break;
case "role":
role = attributeValue;
break;
case "dir":
textDirection = EpubTextDirectionParser.Parse(attributeValue);
break;
case "lang":
language = attributeValue;
break;
}
}
string creator = creatorNode.Value;
return new(creator, id, fileAs, role, textDirection, language);
}
private static EpubMetadataSubject ReadSubject(XElement subjectNode)
{
ReadMetadataItemWithIdDirLangAndContent(subjectNode, out string? id, out EpubTextDirection? textDirection, out string? language, out string subject);
return new(subject, id, textDirection, language);
}
private static EpubMetadataDescription ReadDescription(XElement descriptionNode)
{
ReadMetadataItemWithIdDirLangAndContent(descriptionNode, out string? id, out EpubTextDirection? textDirection, out string? language, out string description);
return new(description, id, textDirection, language);
}
private static EpubMetadataPublisher ReadPublisher(XElement publisherNode)
{
ReadMetadataItemWithIdDirLangAndContent(publisherNode, out string? id, out EpubTextDirection? textDirection, out string? language, out string publisher);
return new(publisher, id, textDirection, language);
}
private static EpubMetadataContributor ReadContributor(XElement contributorNode)
{
string? id = null;
string? fileAs = null;
string? role = null;
EpubTextDirection? textDirection = null;
string? language = null;
foreach (XAttribute contributorNodeAttribute in contributorNode.Attributes())
{
string attributeValue = contributorNodeAttribute.Value;
switch (contributorNodeAttribute.GetLowerCaseLocalName())
{
case "id":
id = attributeValue;
break;
case "file-as":
fileAs = attributeValue;
break;
case "role":
role = attributeValue;
break;
case "dir":
textDirection = EpubTextDirectionParser.Parse(attributeValue);
break;
case "lang":
language = attributeValue;
break;
}
}
string contributor = contributorNode.Value;
return new(contributor, id, fileAs, role, textDirection, language);
}
private static EpubMetadataDate ReadDate(XElement dateNode)
{
string date;
string? id = null;
string? @event = null;
foreach (XAttribute dateNodeAttribute in dateNode.Attributes())
{
string attributeValue = dateNodeAttribute.Value;
switch (dateNodeAttribute.GetLowerCaseLocalName())
{
case "id":
id = attributeValue;
break;
case "event":
@event = attributeValue;
break;
}
}
date = dateNode.Value;
return new EpubMetadataDate(date, id, @event);
}
private static EpubMetadataType ReadType(XElement typeNode)
{
ReadMetadataItemWithIdAndContent(typeNode, out string? id, out string type);
return new(type, id);
}
private static EpubMetadataFormat ReadFormat(XElement formatNode)
{
ReadMetadataItemWithIdAndContent(formatNode, out string? id, out string format);
return new(format, id);
}
private static EpubMetadataIdentifier ReadIdentifier(XElement identifierNode)
{
string? id = null;
string? scheme = null;
foreach (XAttribute identifierNodeAttribute in identifierNode.Attributes())
{
string attributeValue = identifierNodeAttribute.Value;
switch (identifierNodeAttribute.GetLowerCaseLocalName())
{
case "id":
id = attributeValue;
break;
case "scheme":
scheme = attributeValue;
break;
}
}
string identifier = identifierNode.Value;
return new(identifier, id, scheme);
}
private static EpubMetadataSource ReadSource(XElement sourceNode)
{
ReadMetadataItemWithIdAndContent(sourceNode, out string? id, out string source);
return new(source, id);
}
private static EpubMetadataLanguage ReadLanguage(XElement languageNode)
{
ReadMetadataItemWithIdAndContent(languageNode, out string? id, out string language);
return new(language, id);
}
private static EpubMetadataRelation ReadRelation(XElement relationNode)
{
ReadMetadataItemWithIdDirLangAndContent(relationNode, out string? id, out EpubTextDirection? textDirection, out string? language, out string relation);
return new(relation, id, textDirection, language);
}
private static EpubMetadataCoverage ReadCoverage(XElement coverageNode)
{
ReadMetadataItemWithIdDirLangAndContent(coverageNode, out string? id, out EpubTextDirection? textDirection, out string? language, out string coverage);
return new(coverage, id, textDirection, language);
}
private static EpubMetadataRights ReadRightsItem(XElement rightsNode)
{
ReadMetadataItemWithIdDirLangAndContent(rightsNode, out string? id, out EpubTextDirection? textDirection, out string? language, out string rights);
return new(rights, id, textDirection, language);
}
private static EpubMetadataMeta ReadMeta(XElement metaNode)
{
string? name = null;
string? content = null;
string? id = null;
string? refines = null;
string? property = null;
string? scheme = null;
EpubTextDirection? textDirection = null;
string? language = null;
foreach (XAttribute metaNodeAttribute in metaNode.Attributes())
{
string attributeValue = metaNodeAttribute.Value;
switch (metaNodeAttribute.GetLowerCaseLocalName())
{
case "name":
name = attributeValue;
break;
case "content":
content = attributeValue;
break;
case "id":
id = attributeValue;
break;
case "refines":
refines = attributeValue;
break;
case "property":
property = attributeValue;
break;
case "scheme":
scheme = attributeValue;
break;
case "dir":
textDirection = EpubTextDirectionParser.Parse(attributeValue);
break;
case "lang":
language = attributeValue;
break;
}
}
content ??= metaNode.Value;
return new(name, content, id, refines, property, scheme, textDirection, language);
}
private static void ReadMetadataItemWithIdAndContent(XElement metadataItemNode, out string? id, out string content)
{
XAttribute idAttribute = metadataItemNode.Attribute("id");
id = idAttribute?.Value;
content = metadataItemNode.Value;
}
private static void ReadMetadataItemWithIdDirLangAndContent(XElement metadataItemNode, out string? id, out EpubTextDirection? textDirection, out string? language,
out string content)
{
id = null;
textDirection = null;
language = null;
foreach (XAttribute metadataItemNodeAttribute in metadataItemNode.Attributes())
{
string attributeValue = metadataItemNodeAttribute.Value;
switch (metadataItemNodeAttribute.GetLowerCaseLocalName())
{
case "id":
id = attributeValue;
break;
case "dir":
textDirection = EpubTextDirectionParser.Parse(attributeValue);
break;
case "lang":
language = attributeValue;
break;
}
}
content = metadataItemNode.Value;
}
}
}
|