/** * Initialized a collector for the current thread */ static void init() { threadLocal.set(new ResultDocModifier()); }
/** * * @param id the of the element to shich the nodes are to be added * @param nodes an object of which the toSTring method returns a well * balanced xhtml snipped */ public void addNodes2Elem(String id, Object nodes) { addNodes2Elem(id, nodes.toString()); }
private void addElems(Document document, String id, List<Node> nodes) { Element element = getElementById(document.getDocumentElement(), id); if (element == null) { logger.error("No element found with id \"" + id + "\""); return; } for (Node node : nodes) { // if using adoptNode instead of importNode attributes of the node // are not correctly adopted. element.appendChild(document.importNode(node, true)); } }
ResultDocModifier.init(); if (!(rendererMediaType.getType().equals("application") && rendererMediaType.getSubtype().equals("xhtml+xml"))) { try { renderer.render(node, getUserContext(), mode, uriInfo, headers, httpHeaders, sharedRenderingValues, baos); final byte[] bytes = baos.toByteArray(); if (!ResultDocModifier.getInstance().isModified()) { entityStream.write(bytes); return; return; ResultDocModifier.getInstance().addToDocument(document); Transformer transformer; try { throw ex; } finally { ResultDocModifier.dispose();
void addToDocument(Document document) { Node head = getHead(document); if (!titleText.isEmpty()) { String pageTitle = titleText; pageTitle += " - " + subTitleText; Element title = (Element) getTitle(document); if (title == null) { title = document.createElementNS(XHTMLNS, "title"); Element body = (Element) getBody(document); String existingOnLoadScript = body.getAttribute("onload"); if (!existingOnLoadScript.isEmpty()) { addElems(document, entry.getKey(), entry.getValue());
private Element getElementById(Element element, String id) { NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node instanceof Element) { Element subElement = (Element) node; NamedNodeMap attrs = subElement.getAttributes(); if (attrs != null) { Node idNode = attrs.getNamedItem("id"); if ((idNode != null) && (id.equals(idNode.getTextContent()))) { return subElement; } } Element subResult = getElementById(subElement, id); if (subResult != null) { return subResult; } } } return null; } }
public void addNodes2Elem(String id, String nodes) { try { String miniDoc = "<doc xmlns=\"http://www.w3.org/1999/xhtml\">" + nodes + "</doc>"; Document document = documentBuilder.parse( new InputSource(new StringReader(miniDoc))); addNodes2Elem(id, document.getDocumentElement().getChildNodes()); } catch (Exception ex) { throw new RuntimeException(ex); } }