public void endElement() { XMLElement e = parser.getCurrentElement(); if (e.getNamespace().equals("http://purl.org/dc/elements/1.1/")) { String name = e.getName(); if (name.equals("identifier")) { String idAttr = e.getAttribute("id"); if (idAttr != null && !idAttr.equals("") && idAttr.equals(uniqueIdent)) { // String idval = (String) e.getPrivateData(); // if (idval != null && ocf != null) // ocf.setUniqueIdentifier(idval); } } else if (name.equals("date")) { String dateval = (String) e.getPrivateData(); boolean valid = true; String detail = null; if (dateval == null || "".equals(dateval)) { valid = false; detail = "zero-length string"; } else { DateParser dateParser = new DateParser(); try { Date date = dateParser.parse(dateval.trim()); /* * mg: DateParser does not enforce four-digit years, * which http://www.w3.org/TR/NOTE-datetime seems to want */ String year = new SimpleDateFormat("yyyy").format(date); if (year.length() > 4) throw new InvalidDateException(year); } catch (InvalidDateException d) { valid = false; detail = d.getMessage(); } } if (!valid) { if (this.version == EPUBVersion.VERSION_3) { report.warning( path, parser.getLineNumber(), parser.getColumnNumber(), "date value '" + (dateval == null ? "" : dateval) + "' does not follow recommended syntax as per http://www.w3.org/TR/NOTE-datetime:" + detail); } else { report.error( path, parser.getLineNumber(), parser.getColumnNumber(), "date value '" + (dateval == null ? "" : dateval) + "' is not valid as per http://www.w3.org/TR/NOTE-datetime:" + detail); } } } else if (name.equals("title") || name.equals("language")) { // issue 138: issue a warning if dc:title and dc:language is empty for 2.0 and 2.0.1 // note that an empty dc:identifier is checked in opf20.rng and will // therefore be reported as an error, that may or may not be a good idea. if (version == EPUBVersion.VERSION_2) { String value = (String) e.getPrivateData(); if (value == null || value.trim().length() < 1) { report.warning( path, parser.getLineNumber(), parser.getColumnNumber(), name + " element is empty"); } } } } }
public void startElement() { boolean registerEntry = true; XMLElement e = parser.getCurrentElement(); String ns = e.getNamespace(); if (ns == null || ns.equals("") || ns.equals("http://openebook.org/namespaces/oeb-package/1.0/") || ns.equals("http://www.idpf.org/2007/opf")) { String name = e.getName(); if (name.equals("package")) { if (!ns.equals("http://www.idpf.org/2007/opf")) { report.warning( path, parser.getLineNumber(), parser.getColumnNumber(), "OPF file is using OEBPS 1.2 syntax allowing backwards compatibility"); opf12PackageFile = true; } /* * This section checks to see the value of the unique-identifier * attribute and stores it in the String uniqueIdent or reports * an error if the unique-identifier attribute is missing or * does not have a value */ String uniqueIdentAttr = e.getAttribute("unique-identifier"); if (uniqueIdentAttr != null && !uniqueIdentAttr.equals("")) { uniqueIdent = uniqueIdentAttr; } else { report.error( path, parser.getLineNumber(), parser.getColumnNumber(), "unique-identifier attribute in package element must be present and have a value"); } } else if (name.equals("item")) { String id = e.getAttribute("id"); String href = e.getAttribute("href"); if (href != null && !(version == EPUBVersion.VERSION_3 && href.startsWith("http://"))) { try { href = PathUtil.resolveRelativeReference(path, href, null); } catch (IllegalArgumentException ex) { report.error(path, parser.getLineNumber(), parser.getColumnNumber(), ex.getMessage()); href = null; } } String mimeType = e.getAttribute("media-type"); String fallback = e.getAttribute("fallback"); String fallbackStyle = e.getAttribute("fallback-style"); String namespace = e.getAttribute("island-type"); String properties = e.getAttribute("properties"); if (properties != null) properties = properties.replaceAll("[\\s]+", " "); if (version == EPUBVersion.VERSION_3 && href.startsWith("http://") && !OPFChecker30.isBlessedAudioType(mimeType)) if (OPFChecker30.isCoreMediaType(mimeType)) { report.error( path, parser.getLineNumber(), parser.getColumnNumber(), "Only audio and video remote resources are permitted"); } else { // mgy 20120414: this shouldn't even be a warning // report.warning( // path, // parser.getLineNumber(), // parser.getColumnNumber(), // "Remote resource not validated"); } OPFItem item = new OPFItem( id, href, mimeType, fallback, fallbackStyle, namespace, properties, parser.getLineNumber(), parser.getColumnNumber()); if (id != null) itemMapById.put(id, item); if (properties != null) { String propertyArray[] = properties.split(" "); for (int i = 0; i < propertyArray.length; i++) { if (propertyArray[i].equals("nav")) item.setNav(true); if (propertyArray[i].equals("scripted")) item.setScripted(true); } } if (href != null && registerEntry) { itemMapByPath.put(href, item); items.add(item); } } else if (name.equals("reference")) { String type = e.getAttribute("type"); String title = e.getAttribute("title"); String href = e.getAttribute("href"); if (href != null && xrefChecker != null) { try { href = PathUtil.resolveRelativeReference(path, href, null); xrefChecker.registerReference( path, parser.getLineNumber(), parser.getColumnNumber(), href, XRefChecker.RT_GENERIC); } catch (IllegalArgumentException ex) { report.error(path, parser.getLineNumber(), parser.getColumnNumber(), ex.getMessage()); href = null; } } OPFReference ref = new OPFReference(type, title, href, parser.getLineNumber(), parser.getColumnNumber()); refs.add(ref); } else if (name.equals("spine")) { String idref = e.getAttribute("toc"); if (idref != null) { toc = (OPFItem) itemMapById.get(idref); if (toc == null) report.error( path, parser.getLineNumber(), parser.getColumnNumber(), "item with id '" + idref + "' not found"); else { toc.setNcx(true); if (toc.getMimeType() != null && !toc.getMimeType().equals("application/x-dtbncx+xml")) report.error( path, parser.getLineNumber(), parser.getColumnNumber(), "toc attribute references resource with non-NCX mime type; \"application/x-dtbncx+xml\" is expected"); } } } else if (name.equals("itemref")) { String idref = e.getAttribute("idref"); if (idref != null) { OPFItem item = getItemById(idref); if (item != null) { spine.add(item); item.setInSpine(true); String linear = e.getAttribute("linear"); if (linear != null && "no".equals(linear.trim())) { item.setSpineLinear(false); } else { item.setSpineLinear(true); } } else { report.error( path, parser.getLineNumber(), parser.getColumnNumber(), "item with id '" + idref + "' not found"); } } } else if (name.equals("dc-metadata") || name.equals("x-metadata")) { if (!opf12PackageFile) report.error( path, parser.getLineNumber(), parser.getColumnNumber(), "use of deprecated element '" + name + "'"); } } else if (ns.equals("http://purl.org/dc/elements/1.1/")) { // in the DC metadata, when the <identifier> element is parsed, if // it has a non-null and non-empty id attribute value that is the // same as the value of the unique-identifier attribute of the // package element, set uniqueIdentExists = true (to make sure that // the unique-identifier attribute references an existing // <identifier> id attribute String name = e.getName(); if (name.equals("identifier")) { String idAttr = e.getAttribute("id"); if (idAttr != null && !idAttr.equals("") && idAttr.equals(uniqueIdent)) uniqueIdentExists = true; } else if (name.equals("creator")) { String role = e.getAttributeNS("http://www.idpf.org/2007/opf", "role"); if (role != null && !role.equals("")) { if (!isValidRole(role)) report.error( path, parser.getLineNumber(), parser.getColumnNumber(), "role value '" + role + "' is not valid"); } } } }