public void characters(char[] chars, int start, int len) { XMLElement e = parser.getCurrentElement(); if (e.getNamespace().equals("http://purl.org/dc/elements/1.1/")) { String name = e.getName(); if (name.equals("identifier") || name.equals("date") || name.equals("title") || name.equals("language")) { String val = (String) e.getPrivateData(); String text = new String(chars, start, len); if (val == null) val = text; else val = val + text; e.setPrivateData(val); } } }
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"); } } } } }