private Document removeAllButAllowedLayers(org.w3c.dom.Document document, final Set allowedLayers) { new DocumentTraverser().traverseDepthFirst(document, new DocumentVisitorAdapter() { public void visit(Element element) { if (!allowedLayers.contains(element.getTextContent())) { Node layerNode = element.getParentNode(); layerNode.getParentNode().removeChild(layerNode); } } public short acceptNode(final Node n) { return isLayerNameNode(n) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP; } }); return document; }
/** * Traverses the node in breadth first order (siblings first). * * @param node * the node. * @param visitor * the visitor. */ public void traverseBreadthFirst(final Node node, final DocumentVisitor visitor) { // this is a non recursive traversal ! LinkedList<Node> queue = new LinkedList<Node>(); queue.addFirst(node); traverseBreadthFirst(visitor, queue); }
/** * Traverses the DOM tree starting at node using depth first traversal, * it skips the start node if visitStartNode==false. * * @param node * the startNode. * @param visitor * the visitor. * @param visitStartNode * the visitStartNode. */ public void traverseDepthFirst(final Node node, final DocumentVisitor visitor, final boolean visitStartNode) { if (visitStartNode) { traverseDepthFirst(node, visitor); } else { // save iteration over the children // this makes it possible to remove or append children // on the fly. Node child = node.getFirstChild(); while (child != null) { // if child is removed during visiting we must store the next sibling Node beforNext = child.getNextSibling(); // let visitor do it's work traverseDepthFirst(child, visitor); // now check if new children are appended Node afterNext = child.getNextSibling(); child = afterNext == null ? beforNext : afterNext; } } }
Node attr = attributes.item(i); int oldlength = attributes.getLength(); traverseDepthFirst(attr, visitor); i -= (oldlength - attributes.getLength()); traverseDepthFirst(child, visitor);
/** * Removes all prefixes from the given XML and change it to the default namespaces. * */ public Element removePrefixes(final Element node) { new DocumentTraverser().traverseDepthFirst(node, new DocumentVisitorAdapter() { private LinkedList<String> m_currentNamespaceStack = new LinkedList<String>(); public void preVisit(final Element element) { String currentNamespace = m_currentNamespaceStack.isEmpty() ? null : (String) m_currentNamespaceStack.getFirst(); if (currentNamespace == null || !currentNamespace.equals(element.getNamespaceURI())) { currentNamespace = element.getNamespaceURI(); element.setAttribute(XMLConstants.XMLNS_ATTRIBUTE, currentNamespace); } element.setPrefix(XMLConstants.DEFAULT_NS_PREFIX); m_currentNamespaceStack.addFirst(currentNamespace); } public void postVisit(final Element element) { m_currentNamespaceStack.removeFirst(); } }); return node; } }
/** * Traverses the node in breadth first order (siblings first). * * @param node * the node. * @param visitor * the visitor. * @param visitStartNode * the visitStartNode. */ public void traverseBreadthFirst(final Node node, final DocumentVisitor visitor, final boolean visitStartNode) { if (visitStartNode) { traverseBreadthFirst(node, visitor); } else { // this is a non recursive traversal ! LinkedList<Node> queue = new LinkedList<Node>(); NodeList childs = node.getChildNodes(); for (int i = 0, l = childs.getLength(); i < l; ++i) { Node child = childs.item(i); queue.addLast(child); } traverseBreadthFirst(visitor, queue); } }
new DocumentTraverser().traverseDepthFirst(document.getDocumentElement(), new DocumentVisitorAdapter() { @Override public void visit(final Node node) {
new DocumentTraverser().traverseDepthFirst(node, new DocumentVisitorAdapter() { private Matcher m_matcher = NSPREFIX_PATTERN.matcher("");
private TargetItemCollection compileNamedLayers(org.w3c.dom.Document document, final String checkedBaseUrl) { final TargetItemCollection availableLayers = new TargetItemCollection(); new DocumentTraverser().traverseDepthFirst(document, new DocumentVisitorAdapter() { public void visit(Element element) { String layerName = element.getTextContent(); String encodedLayerName; try { encodedLayerName = URLEncoder.encode(layerName, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new InterceptorException("Could not encode layer " + layerName, e); } String resourceId = new StringBuffer(checkedBaseUrl).append("layers/").append(encodedLayerName).toString(); availableLayers.addItem(new TargetItem(layerName, resourceId)); } public short acceptNode(final Node n) { return isLayerNameNode(n) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP; } }); return availableLayers; }
private Map<String, String> readParamsFromXML() throws DOMParserException, IOException { final Map<String, String> requestParameters = new HashMap<String, String>(); Document requestDoc = DOMParser.createNew().parse(new InputSource(m_servletRequest.getInputStream())); final Element documentElement = requestDoc.getDocumentElement(); String requestParamValue = documentElement.getLocalName(); requestParameters.put(WSSRequestContext.PARAM_REQUEST, requestParamValue); new DocumentTraverser().traverseDepthFirst(documentElement, new DocumentVisitorAdapter() { @Override public void visit(Text text) { if (text.getParentNode().equals(documentElement)) { return; } requestParameters.put(text.getParentNode().getLocalName().toUpperCase(), text.getNodeValue()); } @Override public void visit(CDATASection cdata) { requestParameters.put(cdata.getParentNode().getLocalName().toUpperCase(), cdata.getWholeText()); } @Override public void visit(Attr attr) { if (attr.getPrefix() != null && attr.getPrefix().equals("xmlns")) { return; } String paramName = attr.getLocalName(); String paramValue = attr.getNodeValue(); requestParameters.put(paramName.toUpperCase(), paramValue); } }); return requestParameters; }
private String replaceUsingXPath(final String newURL, String message, String encoding) { // WMS 1.? // TODO extend XPath for POST nodes: //DCPType/HTTP/*[self::Get // or self::Post]/OnlineResource // WFS 1.1 // TODO extend XPath for POST nodes: // //ows:Operation/ows:DCP/ows:HTTP/*[self::ows:Get or // self::ows:Post] // WFS 1.0 // TODO add XPath for WFS 1.0.0: //DCPType/HTTP/*[self::Get or // self::Post]/@onlineResource final org.w3c.dom.Document doc = DOMParser.createNew().parse(new InputSource(new StringReader(message))); new DocumentTraverser().traverseDepthFirst(doc, new DocumentVisitorAdapter() { @Override public void visit(org.w3c.dom.Element element) { element.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", newURL); } public short acceptNode(final Node n) { return isOnlineResourceNode(n) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP; } }); return DOMSerializer.createNew().serializeToString(doc); }
/** * Method creates session info from XML. * * @param sessionStream * the SessionStream (non namespace xml). * @return SessionInfo. */ public static SessionInfo createFromXML(final InputStream sessionStream) { try { Document doc = DOMParser.createNew(DOMParserOptions.getDefault()).parse( new InputSource(sessionStream)); SessionInfoVisitor sessionInfoVisitor = new SessionInfoVisitor(); new DocumentTraverser().traverseDepthFirst(doc, sessionInfoVisitor); return sessionInfoVisitor.getSessionInfo(); } catch (Exception e) { throw new SessionCreationException("Error while creating SessionInfo from XML: " + e, e); } }