/** * Add namespace declaration to <code>elem</code> if needed. Necessary since JDOM uses a simple * list. * * @param elem the element where namespaces should be available. * @param c the namespaces to add. * @see Element#addNamespaceDeclaration(Namespace) */ public static void addNamespaces(final Element elem, final Collection<Namespace> c) { if (c instanceof RandomAccess && c instanceof List) { final List<Namespace> list = (List<Namespace>) c; final int stop = c.size() - 1; for (int i = 0; i < stop; i++) { final Namespace ns = list.get(i); if (elem.getNamespace(ns.getPrefix()) == null) elem.addNamespaceDeclaration(ns); } } else { for (final Namespace ns : c) { if (elem.getNamespace(ns.getPrefix()) == null) elem.addNamespaceDeclaration(ns); } } }
/** * Bubbles the namespaces in element and in all its children prudently upstream. More * specifically, all conflicting elements are determined prior to bubbling, and only unconflicting * elements are bubbled immediately to the specified element. * * <p< * * <p>Default namespaces are left where they are found. */ public static NamespacesBubbler.Results bubble_namespaces_prudent(Element element) { // The return variable NamespacesBubbler.Results rval = new NamespacesBubbler.Results(); // Collect all namespaces List<Namespace> all = collect_namespaces(element, null); // Partition the namespaces for (Namespace ns : all) { // Not equal but similar ones are put into conflict category, // and others into bubble. if (contains_similar_ns(all, ns)) { rval.conflict.add(ns); } else { rval.bubble.add(ns); } // if-else } // for: each namespace // Now bubble those that can be bubbled. remove_namespaces(element, rval.bubble); // And populate the current element.. for (Namespace ns : rval.bubble) { element.addNamespaceDeclaration(ns); } // for return rval; } // bubble_namespaces_prudent
/** * This will take the supplied <code>{@link Element}</code> and transfer its namespaces to the * global namespace storage. * * @param element <code>Element</code> to read namespaces from. */ private void transferNamespaces(Element element) { Iterator i = declaredNamespaces.iterator(); while (i.hasNext()) { Namespace ns = (Namespace) i.next(); if (ns != element.getNamespace()) { element.addNamespaceDeclaration(ns); } } declaredNamespaces.clear(); }
/** {@inheritDoc} */ public Document encodeXML() throws InvalidLLRPMessageException { try { Namespace ns = Namespace.getNamespace("llrp", LLRPConstants.LLRPNAMESPACE); Element root = new Element("GET_ACCESSSPECS_RESPONSE", ns); // Element root = new Element("GET_ACCESSSPECS_RESPONSE"); root.addNamespaceDeclaration(Namespace.getNamespace("llrp", LLRPConstants.LLRPNAMESPACE)); if (version == null) { throw new InvalidLLRPMessageException("Version not set"); } else { root.setAttribute("Version", version.toInteger().toString()); } if (messageID == null) { throw new InvalidLLRPMessageException("MessageID not set"); } else { root.setAttribute("MessageID", messageID.toString(10)); } // parameters if (lLRPStatus == null) { LOGGER.info("lLRPStatus not set"); throw new MissingParameterException("lLRPStatus not set"); } else { root.addContent(lLRPStatus.encodeXML(lLRPStatus.getClass().getSimpleName(), ns)); } if (accessSpecList == null) { LOGGER.info("accessSpecList not set"); } else { for (AccessSpec field : accessSpecList) { root.addContent( field.encodeXML( field .getClass() .getName() .replaceAll(field.getClass().getPackage().getName() + ".", ""), ns)); } } Document doc = new Document(root); if (isValidXMLMessage(doc, LLRPConstants.LLRPMESSAGESCHEMAPATH)) { return doc; } else { return null; } } catch (IllegalArgumentException e) { throw new InvalidLLRPMessageException(e.getMessage()); } catch (MissingParameterException e) { throw new InvalidLLRPMessageException(e.getMessage()); } }
/** * Wrap the xml with a message tag * * @throws IOException * @throws JDOMException */ public String wrapMessage(InputStream input, String t24User, String t24Password, String branch) throws JDOMException, IOException { SAXBuilder builder = new SAXBuilder(); try { Document doc = (Document) builder.build(input); Element rootNode = doc.getRootElement(); // create the message element (the wrapping tag) Element installer = new Element( "message", Namespace.getNamespace("installer", "http://www.odcgroup.com/t24/installer")); installer.addNamespaceDeclaration( Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")); installer.setAttribute(new Attribute("uid", t24User)); installer.setAttribute(new Attribute("pwd", t24Password)); installer.setAttribute(new Attribute("branch", branch)); installer.setAttribute(new Attribute("object", getObjectName(rootNode.getName()))); // replace root element doc.setRootElement(installer); installer.addContent(rootNode); T24DeployConsole deployBuilderConsole = T24ServerUIExternalCore.getDefault().getDeployBuilderConsole(); if (deployBuilderConsole.isDisplayDebug()) { StringWriter prettyResult = new StringWriter(); new XMLOutputter(Format.getPrettyFormat()).output(doc, prettyResult); deployBuilderConsole.printDebug( "Credential used for the message wrapper:" + t24User + "/" + t24Password); deployBuilderConsole.printDebug("Message prepared for sending:"); deployBuilderConsole.printDebug(prettyResult.toString()); } // format the result StringWriter result = new StringWriter(); new XMLOutputter(Format.getCompactFormat()).output(doc, result); return result.toString(); } finally { IOUtils.closeQuietly(input); } }
@Override public void generate(Module module, Element element) { if (!(module instanceof SSEModule)) { return; } SSEModule sseModule = (SSEModule) module; if (sseModule instanceof Sharing) { Sharing sharing = (Sharing) sseModule; // add sse namespace Element root = element; while ((root.getParent() != null) && root.getParent() instanceof Element) { root = (Element) root.getParent(); } root.addNamespaceDeclaration(SSEModule.SSE_NS); generateSharing(sharing, root); } else if (sseModule instanceof Sync) { generateSync((Sync) sseModule, element); } }
protected void generateModuleNamespaceDefs(Element root) { for (int i = 0; i < _allModuleNamespaces.length; i++) { root.addNamespaceDeclaration(_allModuleNamespaces[i]); } }
/** * Bubbles all namespaces present in the element and in its children as close to the element as * possible. The process attempts to bubble all namespaces as up as possible. If at some level the * direct children define conflicting namespace prefixes or multiple prefixes for a single * namespace URI, those namespaces are left there. * * <p> * * @param element bubbles namespaces in the children recursively upwards. * @return The namespaces which can be bubbled from the specified element upstream and which * namespaces caused conflicts in the prefixes. */ public static List<Namespace> bubble_namespaces_greedy(Element element) { // Depth first Map<Element, List<Namespace>> map = new LinkedHashMap<Element, List<Namespace>>(); for (Object obj : element.getContent()) { if ((obj instanceof Element) == false) { continue; } // Depth-first recursion Element c = (Element) obj; List<Namespace> rval = null; rval = bubble_namespaces_greedy(c); map.put(c, rval); } // for // Create a set containing all different namespaces in the children List<Namespace> set = new LinkedList<Namespace>(); for (Map.Entry<Element, List<Namespace>> entry : map.entrySet()) { for (Namespace a : entry.getValue()) { // Pick these into local variables for convenience // and to avoid frequently calling the member methods.. String uri = a.getURI(); String prefix = a.getPrefix(); boolean already = false; for (Namespace b : set) { boolean uri_equal = uri.equals(b.getURI()); boolean prefix_equal = prefix.equals(b.getPrefix()); if (uri_equal & prefix_equal) { // already included already = true; break; } } // for: all total if (already == false) { set.add(a); } // if: not already } // for each ns } // for // The list "set" contains now all namespaces present in all // children. Next the conflicting ones need to be singled out. // Return namespaces for this element List<Namespace> pset = new LinkedList<Namespace>(); Namespace pns = element.getNamespace(); if (pns != null) { pset.add(pns); } for (Object obj : element.getAdditionalNamespaces()) { pset.add((Namespace) obj); } // for // The list "pset" contains now all namespaces present // in the parent element itself. List<Namespace> set2 = new LinkedList<Namespace>(); for (Namespace ns1 : set) { String uri = ns1.getURI(); String prefix = ns1.getPrefix(); boolean conflicting = false; for (Namespace ns2 : set) { if (ns1 == ns2) { continue; } boolean uri_eq = uri.equals(ns2.getURI()); boolean p_eq = prefix.equals(ns2.getPrefix()); if (p_eq != uri_eq) { // Conflict. Drop both ns1 and ns2. set2.add(ns1); conflicting = true; // The ns2 will come.. break; } // if } // for if (conflicting) { continue; } // Make sure that ns1 does not conflict with the parent either for (Namespace ns2 : pset) { if (ns1 == ns2) { continue; } boolean uri_eq = uri.equals(ns2.getURI()); boolean p_eq = prefix.equals(ns2.getPrefix()); if (p_eq != uri_eq) { // Conflict. Drop both ns1 only; it cannot be // propagated more upwards. set2.add(ns1); conflicting = true; break; } // if } } // for // the list "set2" is now a list of all conflicting nodes // in the children set.removeAll(set2); // At this point: // set: a list of all namespaces which can be propagated // upwards without conflicts. // set2: a list of all namespaces which are conflicting for (Map.Entry<Element, List<Namespace>> entry : map.entrySet()) { // see which namespaces can be propagated to this.. List<Namespace> list = entry.getValue(); Element child = entry.getKey(); for (Namespace ns : list) { // ------- Find if ns belongs in set boolean contains = false; String uri = ns.getURI(); String p = ns.getPrefix(); for (Namespace x : set) { boolean uri_eq = uri.equals(x.getURI()); boolean p_eq = p.equals(x.getPrefix()); if (p_eq && uri_eq) { contains = true; break; } } // for // if "ns" is contained in "set", // it can be removed from the child if (contains) { // Namespace "ns" can be propagated child.removeNamespaceDeclaration(ns); } // if: contains } // for } // for // Add all not in pset to the parent List<Namespace> rval = new LinkedList<Namespace>(); for (Namespace ns : set) { String uri = ns.getURI(); String p = ns.getPrefix(); boolean contains = false; for (Namespace x : pset) { boolean uri_eq = uri.equals(x.getURI()); boolean p_eq = p.equals(x.getPrefix()); if (p_eq && uri_eq) { contains = true; break; } } // for if (!contains) { element.addNamespaceDeclaration(ns); rval.add(ns); } } // for rval.addAll(pset); return rval; } // bubble_namespaces_greedy()
public static void writeKML(String path) { Element root = new Element("kml"); root.setNamespace(Namespace.getNamespace(null, "http://www.opengis.net/kml/2.2")); root.addNamespaceDeclaration(Namespace.getNamespace("gx", "http://www.google.com/kml/ext/2.2")); root.addNamespaceDeclaration(Namespace.getNamespace("kml", "http://www.opengis.net/kml/2.2")); root.addNamespaceDeclaration(Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom")); Document doc = new Document(root); Element document = new Element("Document"); root.addContent(document); Element style1 = new Element("Style"); style1.setAttribute("id", "shp_centerpoint"); document.addContent(style1); Element iconstyle = new Element("IconStyle"); style1.addContent(iconstyle); Element scale1 = new Element("scale"); scale1.setText("0.8"); iconstyle.addContent(scale1); Element icon = new Element("Icon"); iconstyle.addContent(icon); Element href = new Element("href"); href.setText("http://maps.google.com/mapfiles/kml/paddle/red-circle.png"); icon.addContent(href); Element labelstyle = new Element("LabelStyle"); style1.addContent(labelstyle); Element scale2 = new Element("scale"); scale2.setText("1"); labelstyle.addContent(scale2); Element color1 = new Element("color"); color1.setText("FFFFFFFF"); labelstyle.addContent(color1); Element filename = new Element("name"); String[] split = path.split("/"); String tmp = split[split.length - 1]; filename.setText(tmp.substring(0, tmp.length() - 4)); document.addContent(filename); Element folder = new Element("Folder"); document.addContent(folder); Element name1 = new Element("name"); name1.setText("Labels"); folder.addContent(name1); Element description1 = new Element("description"); folder.addContent(description1); Element open1 = new Element("open"); open1.setText("0"); folder.addContent(open1); GraphNode gn; for (Integer z : popcentroids.keySet()) { gn = popcentroids.get(z); Element placemark = new Element("Placemark"); placemark.setAttribute("id", gn.getNodeID() + "lbl"); folder.addContent(placemark); Element snippet = new Element("Snippet"); snippet.setAttribute("maxLines", "0"); placemark.addContent(snippet); Element name = new Element("name"); name.setText(gn.getNodeID() + ""); placemark.addContent(name); Element description = new Element("description"); description.setText(gn.getTAZ() + " " + gn.getTract() + " " + gn.getBG()); placemark.addContent(description); Element visibility = new Element("visibility"); visibility.setText("1"); placemark.addContent(visibility); Element open = new Element("open"); open.setText("0"); placemark.addContent(open); Element point = new Element("Point"); placemark.addContent(point); Element extrude = new Element("extrude"); extrude.setText("0"); point.addContent(extrude); Element tess = new Element("tessalate"); tess.setText("1"); point.addContent(tess); Element alt = new Element("altitudeMode"); alt.setText("clampedToGround"); point.addContent(alt); Element coord = new Element("coordinates"); coord.setText(gn.getCentroid().getX() + "," + gn.getCentroid().getY() + ",0"); point.addContent(coord); Element styleUrl = new Element("styleUrl"); styleUrl.setText("#shp_centerpoint"); placemark.addContent(styleUrl); XMLOutputter outputter = new XMLOutputter(); try { outputter.output(doc, new FileOutputStream(path)); } catch (IOException e) { System.err.println(e); } } }
/** {@inheritDoc} */ public Document encodeXML() throws InvalidLLRPMessageException { try { Namespace ns = Namespace.getNamespace("llrp", LLRPConstants.LLRPNAMESPACE); Element root = new Element("SET_READER_CONFIG", ns); // Element root = new Element("SET_READER_CONFIG"); root.addNamespaceDeclaration(Namespace.getNamespace("llrp", LLRPConstants.LLRPNAMESPACE)); if (version == null) { throw new InvalidLLRPMessageException("Version not set"); } else { root.setAttribute("Version", version.toInteger().toString()); } if (messageID == null) { throw new InvalidLLRPMessageException("MessageID not set"); } else { root.setAttribute("MessageID", messageID.toString(10)); } if (resetToFactoryDefault == null) { LOGGER.warn(" resetToFactoryDefault not set"); throw new MissingParameterException(" resetToFactoryDefault not set"); } else { root.addContent(resetToFactoryDefault.encodeXML("ResetToFactoryDefault", ns)); } // root.addContent(reserved0.encodeXML("reserved",ns)); // parameters if (readerEventNotificationSpec == null) { LOGGER.info("readerEventNotificationSpec not set"); } else { root.addContent( readerEventNotificationSpec.encodeXML( readerEventNotificationSpec.getClass().getSimpleName(), ns)); } if (antennaPropertiesList == null) { LOGGER.info("antennaPropertiesList not set"); } else { for (AntennaProperties field : antennaPropertiesList) { root.addContent( field.encodeXML( field .getClass() .getName() .replaceAll(field.getClass().getPackage().getName() + ".", ""), ns)); } } if (antennaConfigurationList == null) { LOGGER.info("antennaConfigurationList not set"); } else { for (AntennaConfiguration field : antennaConfigurationList) { root.addContent( field.encodeXML( field .getClass() .getName() .replaceAll(field.getClass().getPackage().getName() + ".", ""), ns)); } } if (rOReportSpec == null) { LOGGER.info("rOReportSpec not set"); } else { root.addContent(rOReportSpec.encodeXML(rOReportSpec.getClass().getSimpleName(), ns)); } if (accessReportSpec == null) { LOGGER.info("accessReportSpec not set"); } else { root.addContent( accessReportSpec.encodeXML(accessReportSpec.getClass().getSimpleName(), ns)); } if (keepaliveSpec == null) { LOGGER.info("keepaliveSpec not set"); } else { root.addContent(keepaliveSpec.encodeXML(keepaliveSpec.getClass().getSimpleName(), ns)); } if (gPOWriteDataList == null) { LOGGER.info("gPOWriteDataList not set"); } else { for (GPOWriteData field : gPOWriteDataList) { root.addContent( field.encodeXML( field .getClass() .getName() .replaceAll(field.getClass().getPackage().getName() + ".", ""), ns)); } } if (gPIPortCurrentStateList == null) { LOGGER.info("gPIPortCurrentStateList not set"); } else { for (GPIPortCurrentState field : gPIPortCurrentStateList) { root.addContent( field.encodeXML( field .getClass() .getName() .replaceAll(field.getClass().getPackage().getName() + ".", ""), ns)); } } if (eventsAndReports == null) { LOGGER.info("eventsAndReports not set"); } else { root.addContent( eventsAndReports.encodeXML(eventsAndReports.getClass().getSimpleName(), ns)); } if (customList == null) { LOGGER.info("customList not set"); } else { for (Custom field : customList) { root.addContent( field.encodeXML( field .getClass() .getName() .replaceAll(field.getClass().getPackage().getName() + ".", ""), ns)); } } Document doc = new Document(root); if (isValidXMLMessage(doc, LLRPConstants.LLRPMESSAGESCHEMAPATH)) { return doc; } else { return null; } } catch (IllegalArgumentException e) { throw new InvalidLLRPMessageException(e.getMessage()); } catch (MissingParameterException e) { throw new InvalidLLRPMessageException(e.getMessage()); } }
public void writeDefaultNamespace(String namespace) throws XMLStreamException { currentNode.addNamespaceDeclaration(Namespace.getNamespace("", namespace)); }
public void writeNamespace(String prefix, String namespace) throws XMLStreamException { Namespace decNS = currentNode.getNamespace(prefix); if (decNS == null || !decNS.getURI().equals(namespace)) currentNode.addNamespaceDeclaration(Namespace.getNamespace(prefix, namespace)); }
public static void addNamespaces(final Element elem, final Namespace... l) { for (final Namespace ns : l) { if (elem.getNamespace(ns.getPrefix()) == null) elem.addNamespaceDeclaration(ns); } }