// According to NIMAS, href should be in the form "filename.xml#ID" or just "#ID" for within-file link
// For authors' convenience, we accept simple "ID" as well.
int hashLocation = href.indexOf('#');
if (hashLocation > 0) {
// filename#ID case
return new SectionLinkFactory().linkTo(wicketId, href.substring(0, hashLocation), href.substring(hashLocation+1));
}
// "#ID" or "ID" case:
String file = getModel().getObject().getXmlDocument().getName(); // same file as we're currently viewing
String id = href.substring(hashLocation+1); // start at index 0 or 1
return new SectionLinkFactory().linkTo(wicketId, file, id);
} else if (wicketId.startsWith("popupLink_")) {
String href = elt.getAttribute("href");
// According to NIMAS, href should be in the form "filename.xml#ID" or just "#ID" for within-file link
// For authors' convenience, we accept simple "ID" as well.
int hashLocation = href.indexOf('#');
String file;
if (hashLocation > 0) {
file = href.substring(0, hashLocation);
} else { // "#ID" or "ID" case:
file = getModel().getObject().getXmlDocument().getName(); // same file as we're currently viewing
}
XmlDocument document = xmlService.getDocument(file);
String xmlId = href.substring(hashLocation+1);
XmlSection section = document.getById(xmlId);
XmlSectionModel mSection = new XmlSectionModel(section);
return new AuthoredPopupLink(wicketId, xmlId, mSection);
} else if (wicketId.startsWith("noteBackLink_")) {
// Link back from a note to its (first) noteref.
String idref = elt.getAttribute("idref");
// Find candidate noterefs in this chapter
XmlSection sec = getModel().getObject();
XPath xPath = XPathFactory.newInstance().newXPath();
xPath.setNamespaceContext(XmlService.get().getNamespaceContext());
XmlSection linkSection = null;
String linkText = "?";
try {
String path = String.format("//dtb:noteref[@idref='#%s']", idref);
NodeList nl = (NodeList) xPath.evaluate(path, sec.getXmlDocument().getDocument().getDocumentElement(), XPathConstants.NODESET);
if (nl.getLength() > 0) {
Element node = null;
node = (Element) nl.item(0);
linkText = node.getTextContent();
// Scan parents until you find the smallest enclosing XML Section.
while (linkSection == null && node.getParentNode() != null) {
String id = node.getAttributeNS(null, "id");
if (id != null) {
linkSection = sec.getXmlDocument().getById(id);
}
node = (Element) node.getParentNode();
}
}
} catch (XPathExpressionException e) {
e.printStackTrace(); // malformed expression - shouldn't happen
}
if (linkSection != null) {
BookmarkablePageLink<ISIStandardPage> link = new SectionLinkFactory().linkTo(wicketId, linkSection, "note_"+idref);
link.add(new AttributeRemover("idref"));
link.add(new Label("text", linkText));
return link;
} else {
log.debug("Could not find noteref for note: idref={}", idref);
return new SectionLinkFactory().linkToPage(wicketId, null);
}
} else if (wicketId.startsWith("fileLink_")) {
// link to file in content directory
return new ResourceLink<Object> (wicketId, getRelativeRef(elt.getAttribute("href")));