/** * Set a property of a resource to a value. * * @param name the property name * @param value the property value * @exception com.ibm.webdav.WebDAVException */ public void setProperty(String name, Element value) throws WebDAVException { // load the properties Document propertiesDocument = resource.loadProperties(); Element properties = propertiesDocument.getDocumentElement(); String ns = value.getNamespaceURI(); Element property = null; if (ns == null) { property = (Element) ((Element) properties).getElementsByTagName(value.getTagName()).item(0); } else { property = (Element) properties.getElementsByTagNameNS(ns, value.getLocalName()).item(0); } if (property != null) { try { properties.removeChild(property); } catch (DOMException exc) { } } properties.appendChild(propertiesDocument.importNode(value, true)); // write out the properties resource.saveProperties(propertiesDocument); }
public static Element[] filterChildElements(Element parent, String ns, String lname) { /* way too noisy if (LOGGER.isDebugEnabled()) { StringBuilder buf = new StringBuilder(100); buf.append("XmlaUtil.filterChildElements: "); buf.append(" ns=\""); buf.append(ns); buf.append("\", lname=\""); buf.append(lname); buf.append("\""); LOGGER.debug(buf.toString()); } */ List<Element> elems = new ArrayList<Element>(); NodeList nlst = parent.getChildNodes(); for (int i = 0, nlen = nlst.getLength(); i < nlen; i++) { Node n = nlst.item(i); if (n instanceof Element) { Element e = (Element) n; if ((ns == null || ns.equals(e.getNamespaceURI())) && (lname == null || lname.equals(e.getLocalName()))) { elems.add(e); } } } return elems.toArray(new Element[elems.size()]); }
/** * Returns a sibling element that matches a given definition, or <tt>null</tt> if no match is * found. * * @param sibling the sibling DOM element to begin the search * @param target the node to search for * @return the matching element, or <tt>null</tt> if not found */ public static Element findSibling(Element sibling, XmlNode target) { String xmlName = target.getLocalName(); String xmlNamespace = target.getNamespace(); Node node = sibling; if (node == null) { return null; } while ((node = node.getNextSibling()) != null) { if (node.getNodeType() != Node.ELEMENT_NODE) { continue; } Element element = (Element) node; if (!element.getLocalName().equals(xmlName)) { continue; } if (target.isNamespaceAware()) { String ns = element.getNamespaceURI(); if (ns == null) { if (xmlNamespace != null) { continue; } } else { if (!ns.equals(xmlNamespace)) { continue; } } } return element; } return null; }
/** * This will invoke the <code>startElement</code> callback in the <code>ContentHandler</code>. * * @param element <code>Element</code> used in callbacks. * @param nsAtts <code>List</code> of namespaces to declare with the element or <code>null</code>. */ private void startElement(Element element, Attributes nsAtts) throws JDOMException { String namespaceURI = element.getNamespaceURI(); String localName = element.getName(); String rawName = element.getQualifiedName(); // Allocate attribute list. AttributesImpl atts = (nsAtts != null) ? new AttributesImpl(nsAtts) : new AttributesImpl(); List attributes = element.getAttributes(); Iterator i = attributes.iterator(); while (i.hasNext()) { Attribute a = (Attribute) i.next(); atts.addAttribute( a.getNamespaceURI(), a.getName(), a.getQualifiedName(), getAttributeTypeName(a.getAttributeType()), a.getValue()); } try { contentHandler.startElement(namespaceURI, localName, rawName, atts); } catch (SAXException se) { throw new JDOMException("Exception in startElement", se); } }
/** * This will invoke the <code>endElement</code> callback in the <code>ContentHandler</code>. * * @param element <code>Element</code> used in callbacks. */ private void endElement(Element element) throws JDOMException { String namespaceURI = element.getNamespaceURI(); String localName = element.getName(); String rawName = element.getQualifiedName(); try { contentHandler.endElement(namespaceURI, localName, rawName); } catch (SAXException se) { throw new JDOMException("Exception in endElement", se); } }
protected final void init(Element xmlaRoot) throws XmlaException { if (NS_XMLA.equals(xmlaRoot.getNamespaceURI())) { String lname = xmlaRoot.getLocalName(); if ("Discover".equals(lname)) { method = Method.DISCOVER; initDiscover(xmlaRoot); } else if ("Execute".equals(lname)) { method = Method.EXECUTE; initExecute(xmlaRoot); } else { // Note that is code will never be reached because // the error will be caught in // DefaultXmlaServlet.handleSoapBody first StringBuilder buf = new StringBuilder(100); buf.append(MSG_INVALID_XMLA); buf.append(": Bad method name \""); buf.append(lname); buf.append("\""); throw new XmlaException( CLIENT_FAULT_FC, HSB_BAD_METHOD_CODE, HSB_BAD_METHOD_FAULT_FS, Util.newError(buf.toString())); } } else { // Note that is code will never be reached because // the error will be caught in // DefaultXmlaServlet.handleSoapBody first StringBuilder buf = new StringBuilder(100); buf.append(MSG_INVALID_XMLA); buf.append(": Bad namespace url \""); buf.append(xmlaRoot.getNamespaceURI()); buf.append("\""); throw new XmlaException( CLIENT_FAULT_FC, HSB_BAD_METHOD_NS_CODE, HSB_BAD_METHOD_NS_FAULT_FS, Util.newError(buf.toString())); } }
/** * Finds the Nth matching child of a DOM element. * * @param parent the parent DOM node * @param target the node to search for * @param offset the occurrence of the matching node * @return the matching element, or <tt>null</tt> if no match is found */ public static Element findChild(Node parent, XmlNode target, int offset) { Node node = parent; if (node != null) { node = node.getFirstChild(); } if (node == null) { return null; } String xmlName = target.getLocalName(); String xmlNamespace = target.getNamespace(); int count = 0; do { if (node.getNodeType() != Node.ELEMENT_NODE) { continue; } Element element = (Element) node; if (!element.getLocalName().equals(xmlName)) { continue; } if (target.isNamespaceAware()) { String ns = element.getNamespaceURI(); if (ns == null) { if (xmlNamespace != null && xmlNamespace.length() != 0) { continue; } } else { if (!ns.equals(xmlNamespace)) { continue; } } } if (count == offset) { return element; } ++count; } while ((node = node.getNextSibling()) != null); return null; }
private void initProperties(Element propertiesRoot) throws XmlaException { Map<String, String> properties = new HashMap<String, String>(); Element[] childElems = XmlaUtil.filterChildElements(propertiesRoot, NS_XMLA, "PropertyList"); if (childElems.length == 1) { NodeList nlst = childElems[0].getChildNodes(); for (int i = 0, nlen = nlst.getLength(); i < nlen; i++) { Node n = nlst.item(i); if (n instanceof Element) { Element e = (Element) n; if (NS_XMLA.equals(e.getNamespaceURI())) { String key = e.getLocalName(); String value = XmlaUtil.textInElement(e); if (LOGGER.isDebugEnabled()) { LOGGER.debug( "DefaultXmlaRequest.initProperties: " + " key=\"" + key + "\", value=\"" + value + "\""); } properties.put(key, value); } } } } else if (childElems.length > 1) { StringBuilder buf = new StringBuilder(100); buf.append(MSG_INVALID_XMLA); buf.append(": Wrong number of PropertyList elements: "); buf.append(childElems.length); throw new XmlaException( CLIENT_FAULT_FC, HSB_BAD_PROPERTIES_LIST_CODE, HSB_BAD_PROPERTIES_LIST_FAULT_FS, Util.newError(buf.toString())); } else { } this.properties = Collections.unmodifiableMap(properties); }
/** * Runs the test case. * * @throws Throwable Any uncaught exception causes test to fail */ public void runTest() throws Throwable { Document doc; DOMImplementation domImpl; Document newDoc; String namespaceURI; DocumentType nullDocType = null; Element docElem; String rootNS; String rootName; String qname; doc = (Document) load("hc_staff", false); docElem = doc.getDocumentElement(); rootNS = docElem.getNamespaceURI(); rootName = docElem.getTagName(); domImpl = doc.getImplementation(); qname = "dom3:" + rootName; newDoc = domImpl.createDocument(rootNS, qname, nullDocType); namespaceURI = newDoc.lookupNamespaceURI("dom3"); assertEquals("nodelookupnamespaceuri02", rootNS, namespaceURI); }
@Test public void testNamespaceURIPrefixLocalName() throws Exception { // builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); String xml = "<?xml version=\"1.0\"?>" + "<t:root xmlns=\"http://void.com/\" xmlns:t=\"http://t.com/\">" + "<t:item/>" + "<child />" + "<t:item/>" + "</t:root>"; DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes())); Element root = doc.getDocumentElement(); Assert.assertEquals("namespace uri", "http://t.com/", root.getNamespaceURI()); Assert.assertEquals("local name", "root", root.getLocalName()); Assert.assertEquals("prefix", "t", root.getPrefix()); Assert.assertEquals("node name", "t:root", root.getNodeName()); }
// DOM output public void setupNamespace(Element element) { String uri = element.getNamespaceURI(); String myPrefix = getPrefixByUri(uri); element.setPrefix(myPrefix); if (myPrefix != null) { IRNSContainer parent = (IRNSContainer) rnode_.getParentRNode(); if (parent == null) { addPrefixDecl(element, myPrefix, uri); return; } RNSContext parentContext = parent.getRNSContext(); String parentPrefix = parentContext.getPrefixByUri(uri); if (!myPrefix.equals(parentPrefix)) { addPrefixDecl(element, myPrefix, uri); } } }
private void initRestrictions(Element restrictionsRoot) throws XmlaException { Map<String, List<String>> restrictions = new HashMap<String, List<String>>(); Element[] childElems = XmlaUtil.filterChildElements(restrictionsRoot, NS_XMLA, "RestrictionList"); if (childElems.length == 1) { NodeList nlst = childElems[0].getChildNodes(); for (int i = 0, nlen = nlst.getLength(); i < nlen; i++) { Node n = nlst.item(i); if (n instanceof Element) { Element e = (Element) n; if (NS_XMLA.equals(e.getNamespaceURI())) { String key = e.getLocalName(); String value = XmlaUtil.textInElement(e); List<String> values; if (restrictions.containsKey(key)) { values = restrictions.get(key); } else { values = new ArrayList<String>(); restrictions.put(key, values); } if (LOGGER.isDebugEnabled()) { LOGGER.debug( "DefaultXmlaRequest.initRestrictions: " + " key=\"" + key + "\", value=\"" + value + "\""); } values.add(value); } } } } else if (childElems.length > 1) { StringBuilder buf = new StringBuilder(100); buf.append(MSG_INVALID_XMLA); buf.append(": Wrong number of RestrictionList elements: "); buf.append(childElems.length); throw new XmlaException( CLIENT_FAULT_FC, HSB_BAD_RESTRICTION_LIST_CODE, HSB_BAD_RESTRICTION_LIST_FAULT_FS, Util.newError(buf.toString())); } // If there is a Catalog property, // we have to consider it a constraint as well. String key = org.olap4j.metadata.XmlaConstants.Literal.CATALOG_NAME.name(); if (this.properties.containsKey(key) && !restrictions.containsKey(key)) { List<String> values; values = new ArrayList<String>(); restrictions.put(this.properties.get(key), values); if (LOGGER.isDebugEnabled()) { LOGGER.debug( "DefaultXmlaRequest.initRestrictions: " + " key=\"" + key + "\", value=\"" + this.properties.get(key) + "\""); } } this.restrictions = (Map) Collections.unmodifiableMap(restrictions); }
public XArchInstanceMetadata getInstanceMetadata() { return new XArchInstanceMetadata(XArchUtils.getPackageTitle(elt.getNamespaceURI())); }
private boolean shallowEquals(NodeInfo n1, Node n2) { if (n1 == n2) return true; if (n1 == null || n2 == null) return false; int type1 = n1.getNodeKind(); if (type1 == Node.CDATA_SECTION_NODE) type1 = Node.TEXT_NODE; else if (type1 == NodeType.NAMESPACE) type1 = Node.ATTRIBUTE_NODE; int type2 = n2.getNodeType(); if (type2 == Node.CDATA_SECTION_NODE) type2 = Node.TEXT_NODE; if (type1 != type2) return false; switch (type1) { case Node.PROCESSING_INSTRUCTION_NODE: ProcessingInstruction pi2 = (ProcessingInstruction) n2; String target1 = n1.getDisplayName(); String target2 = pi2.getTarget(); if (!target1.equals(target2)) return false; String data1 = n1.getStringValue(); String data2 = pi2.getData(); if (!data1.equals(data2)) return false; break; case Node.COMMENT_NODE: Comment comment2 = (Comment) n2; data1 = n1.getStringValue(); data2 = comment2.getData(); if (!data1.equals(data2)) return false; break; case Node.ELEMENT_NODE: Element element2 = (Element) n2; String namespaceURI1 = n1.getURI(); if (namespaceURI1 == null) namespaceURI1 = ""; String namespaceURI2 = element2.getNamespaceURI(); if (namespaceURI2 == null) namespaceURI2 = ""; if (!namespaceURI1.equals(namespaceURI2)) return false; String localName1 = n1.getLocalPart(); String localName2 = element2.getLocalName(); if (!localName1.equals(localName2)) return false; NodeInfoSequence attrs1 = new NodeInfoSequence(n1, Axis.ATTRIBUTE); NamedNodeMap attrs2 = element2.getAttributes(); BitSet bitSet = new BitSet(); NodeInfo attr1; while ((attr1 = attrs1.findNext()) != null) { if (isNamespaceDeclaration(attr1)) continue; namespaceURI1 = attr1.getURI(); if (namespaceURI1 == null) namespaceURI1 = ""; localName1 = attr1.getLocalPart(); String value1 = attr1.getStringValue(); int found = -1; for (int i = 0; i < attrs2.getLength(); i++) { Attr attr2 = (Attr) attrs2.item(i); namespaceURI2 = attr2.getNamespaceURI(); if (namespaceURI2 == null) namespaceURI2 = ""; localName2 = attr2.getLocalName(); if (namespaceURI1.equals(namespaceURI2) && localName1.equals(localName2)) { String value2 = attr2.getNodeValue(); if (!value1.equals(value2)) return false; found = i; break; } } if (found == -1) return false; else bitSet.set(found); } for (int i = 0; i < attrs2.getLength(); i++) { if (!bitSet.get(i)) { Attr attr2 = (Attr) attrs2.item(i); if (!DOMUtil.isNamespaceDeclaration(attr2)) return false; } } break; case Node.ATTRIBUTE_NODE: Attr attr2 = (Attr) n2; namespaceURI1 = isNamespaceDeclaration(n1) ? Namespaces.URI_XMLNS : n1.getURI(); if (namespaceURI1 == null) namespaceURI1 = ""; namespaceURI2 = attr2.getNamespaceURI(); if (namespaceURI2 == null) namespaceURI2 = ""; if (!namespaceURI1.equals(namespaceURI2)) return false; localName1 = n1.getLocalPart(); localName2 = attr2.getLocalName(); if (!localName1.equals(localName2)) return false; String value1 = n1.getStringValue(); String value2 = attr2.getNodeValue(); if (!value1.equals(value2)) return false; break; case Node.TEXT_NODE: value1 = n1.getStringValue(); value2 = n2.getNodeValue(); if (!value1.equals(value2)) return false; } return true; }
/** * Edit the properties of a resource. The updates must refer to a Document containing a WebDAV * propertyupdates element as the document root. * * @param updates an XML Document containing propertyupdate elements * @return the result of making the updates describing the edits to be made. * @exception com.ibm.webdav.WebDAVException */ public MultiStatus setProperties(Document propertyUpdates) throws WebDAVException { // create a MultiStatus to hold the results. It will hold a MethodResponse // for each update, and one for the method as a whole MultiStatus multiStatus = new MultiStatus(); boolean errorsOccurred = false; // first, load the properties so they can be edited Document propertiesDocument = resource.loadProperties(); Element properties = (Element) propertiesDocument.getDocumentElement(); // be sure the updates have at least one update Element propertyupdate = (Element) propertyUpdates.getDocumentElement(); String tagName = propertyupdate.getNamespaceURI() + propertyupdate.getLocalName(); if (!tagName.equals("DAV:propertyupdate")) { throw new WebDAVException( WebDAVStatus.SC_UNPROCESSABLE_ENTITY, "missing propertyupdate element"); } NodeList updates = propertyupdate.getChildNodes(); if (updates.getLength() == 0) { throw new WebDAVException(WebDAVStatus.SC_UNPROCESSABLE_ENTITY, "no updates in request"); } Vector propsGood = new Vector(); // a list of properties that // were patched correctly or would have been if another // property hadn't gone bad. // apply the updates Node temp = null; for (int i = 0; i < updates.getLength(); i++) { temp = updates.item(i); // skip any ignorable TXText elements if (!(temp.getNodeType() == Node.ELEMENT_NODE)) { continue; } Element update = (Element) temp; int updateCommand = -1; tagName = update.getNamespaceURI() + update.getLocalName(); if (tagName.equals("DAV:set")) { updateCommand = set; } else if (tagName.equals("DAV:remove")) { updateCommand = remove; } else { throw new WebDAVException( WebDAVStatus.SC_UNPROCESSABLE_ENTITY, update.getTagName() + " is not a valid property update request"); } // iterate through the props in the set or remove element and update the // properties as directed Element prop = (Element) update.getElementsByTagNameNS("DAV:", "prop").item(0); if (prop == null) { throw new WebDAVException( WebDAVStatus.SC_UNPROCESSABLE_ENTITY, "no propeprties in update request"); } NodeList propsToUpdate = prop.getChildNodes(); for (int j = 0; j < propsToUpdate.getLength(); j++) { temp = propsToUpdate.item(j); // skip any TXText elements?? if (!(temp.getNodeType() == Node.ELEMENT_NODE)) { continue; } Element propToUpdate = (Element) temp; // find the property in the properties element Element property = null; PropertyName propertyName = new PropertyName(propToUpdate); if (((Element) propToUpdate).getNamespaceURI() != null) { property = (Element) properties .getElementsByTagNameNS( propToUpdate.getNamespaceURI(), propToUpdate.getLocalName()) .item(0); } else { property = (Element) properties.getElementsByTagName(propToUpdate.getTagName()).item(0); } boolean liveone = isLive(propertyName.asExpandedString()); if (liveone) { errorsOccurred = true; PropertyResponse response = new PropertyResponse(resource.getURL().toString()); response.addProperty(propertyName, propToUpdate, WebDAVStatus.SC_FORBIDDEN); multiStatus.addResponse(response); } // do the update if (updateCommand == set) { if (property != null) { try { properties.removeChild(property); } catch (DOMException exc) { } } if (!liveone) { // I don't think we're allowed to update live properties // here. Doing so effects the cache. A case in // point is the lockdiscoveryproperty. properties // is actually the properites cache "document" of this // resource. Even though we don't "save" the request // if it includes live properties, we don't remove // it from the cache after we'd set it here, so it // can affect other queries. (jlc 991002) properties.appendChild(propertiesDocument.importNode(propToUpdate, true)); propsGood.addElement(propToUpdate); } } else if (updateCommand == remove) { try { if (property != null) { properties.removeChild(property); propsGood.addElement(propToUpdate); } } catch (DOMException exc) { } } } } { Enumeration els = propsGood.elements(); for (; els.hasMoreElements(); ) { Object ob1 = els.nextElement(); Element elProp = (Element) ob1; PropertyName pn = new PropertyName(elProp); PropertyResponse response = new PropertyResponse(resource.getURL().toString()); response.addProperty( pn, (Element) elProp.cloneNode(false), (errorsOccurred ? WebDAVStatus.SC_FAILED_DEPENDENCY : WebDAVStatus.SC_OK)); // todo: add code for responsedescription multiStatus.addResponse(response); } } // write out the properties if (!errorsOccurred) { resource.saveProperties(propertiesDocument); } return multiStatus; }
public void startElement( String namespaceURI, String localName, String qualifiedName, org.xml.sax.Attributes attributes) { flushText(); Element element = Element.build(qualifiedName, namespaceURI, localName); if (parent == document) { // root document.setRootElement(element); inProlog = false; } current = element; // Need to push this, even if it's null parents.add(element); if (parent != document) { // a.k.a. parent not instanceof Document parent.fastInsertChild(element, parent.getChildCount()); } // This is optimized for the very common case where // everything in the document has the same actual base URI. // It may add redundant base URIs in cases like XInclude // where different parts of the document have different // base URIs. String baseURI = locator.getSystemId(); if (baseURI != null && !baseURI.equals(documentBaseURI)) { element.setActualBaseURI(baseURI); } // Attach the attributes; this must be done before the // namespaces are attached. int length = attributes.getLength(); // We've got a pretty good guess at how many attributes there // will be here; we could ensureCapacity up to that length. // However, that might waste memory because we wouldn't use // the ones for namespace declarations. We could always // trimToSize when we're done, but it's probably not worth // the effort. for (int i = 0; i < length; i++) { String qName = attributes.getQName(i); if (qName.startsWith("xmlns:") || qName.equals("xmlns")) { continue; } else { String namespace = attributes.getURI(i); String value = attributes.getValue(i); Attribute attribute = Attribute.build( qName, namespace, value, convertStringToType(attributes.getType(i)), attributes.getLocalName(i)); element.fastAddAttribute(attribute); } } // Attach the namespaces for (int i = 0; i < length; i++) { String qName = attributes.getQName(i); if (qName.startsWith("xmlns:")) { String namespaceName = attributes.getValue(i); String namespacePrefix = qName.substring(6); String currentValue = element.getNamespaceURI(namespacePrefix); if (!namespaceName.equals(currentValue)) { element.addNamespaceDeclaration(namespacePrefix, namespaceName); } } else if (qName.equals("xmlns")) { String namespaceName = attributes.getValue(i); String namespacePrefix = ""; String currentValue = element.getNamespaceURI(namespacePrefix); if (!namespaceName.equals(currentValue)) { element.addNamespaceDeclaration(namespacePrefix, namespaceName); } } } // this is the new parent parent = element; }
protected static String nodeToString(Node node, Set<String> parentPrefixes, String namespaceURI) throws Exception { StringBuilder b = new StringBuilder(); if (node == null) { return ""; } if (node instanceof Element) { Element element = (Element) node; b.append("<"); b.append(element.getNodeName()); Map<String, String> thisLevelPrefixes = new HashMap<>(); if (element.getPrefix() != null && !parentPrefixes.contains(element.getPrefix())) { thisLevelPrefixes.put(element.getPrefix(), element.getNamespaceURI()); } if (element.hasAttributes()) { NamedNodeMap map = element.getAttributes(); for (int i = 0; i < map.getLength(); i++) { Node attr = map.item(i); if (attr.getNodeName().startsWith("xmlns")) continue; if (attr.getPrefix() != null && !parentPrefixes.contains(attr.getPrefix())) { thisLevelPrefixes.put(attr.getPrefix(), element.getNamespaceURI()); } b.append(" "); b.append(attr.getNodeName()); b.append("=\""); b.append(attr.getNodeValue()); b.append("\""); } } if (namespaceURI != null && !thisLevelPrefixes.containsValue(namespaceURI) && !namespaceURI.equals(element.getParentNode().getNamespaceURI())) { b.append(" xmlns=\"").append(namespaceURI).append("\""); } for (Map.Entry<String, String> entry : thisLevelPrefixes.entrySet()) { b.append(" xmlns:") .append(entry.getKey()) .append("=\"") .append(entry.getValue()) .append("\""); parentPrefixes.add(entry.getKey()); } NodeList children = element.getChildNodes(); boolean hasOnlyAttributes = true; for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() != Node.ATTRIBUTE_NODE) { hasOnlyAttributes = false; break; } } if (!hasOnlyAttributes) { b.append(">"); for (int i = 0; i < children.getLength(); i++) { b.append( nodeToString(children.item(i), parentPrefixes, children.item(i).getNamespaceURI())); } b.append("</"); b.append(element.getNodeName()); b.append(">"); } else { b.append("/>"); } for (String thisLevelPrefix : thisLevelPrefixes.keySet()) { parentPrefixes.remove(thisLevelPrefix); } } else if (node.getNodeValue() != null) { b.append(encodeText(node.getNodeValue(), node instanceof Attr)); } return b.toString(); }