public static Element appendNewElementIfNotNull(Document document, Element parent, String element, Object content, String namespace) { if (content == null) return parent; return appendNewElement(document, parent, element, content, namespace); }
private void generateActionArgument(ActionArgument actionArgument, Document descriptor, Element actionElement) { Element actionArgumentElement = appendNewElement(descriptor, actionElement, ELEMENT.argument); appendNewElementIfNotNull(descriptor, actionArgumentElement, ELEMENT.name, actionArgument.getName()); appendNewElementIfNotNull(descriptor, actionArgumentElement, ELEMENT.direction, actionArgument.getDirection().toString().toLowerCase(Locale.ROOT)); if (actionArgument.isReturnValue()) { // TODO: UPNP VIOLATION: WMP12 will discard RenderingControl service if it contains <retval> tags log.warning("UPnP specification violation: Not producing <retval> element to be compatible with WMP12: " + actionArgument); // appendNewElement(descriptor, actionArgumentElement, ELEMENT.retval); } appendNewElementIfNotNull(descriptor, actionArgumentElement, ELEMENT.relatedStateVariable, actionArgument.getRelatedStateVariableName()); }
protected void generateIconList(Namespace namespace, Device deviceModel, Document descriptor, Element deviceElement) { if (!deviceModel.hasIcons()) return; Element iconListElement = appendNewElement(descriptor, deviceElement, ELEMENT.iconList); for (Icon icon : deviceModel.getIcons()) { Element iconElement = appendNewElement(descriptor, iconListElement, ELEMENT.icon); appendNewElementIfNotNull(descriptor, iconElement, ELEMENT.mimetype, icon.getMimeType()); appendNewElementIfNotNull(descriptor, iconElement, ELEMENT.width, icon.getWidth()); appendNewElementIfNotNull(descriptor, iconElement, ELEMENT.height, icon.getHeight()); appendNewElementIfNotNull(descriptor, iconElement, ELEMENT.depth, icon.getDepth()); if (deviceModel instanceof RemoteDevice) { appendNewElementIfNotNull(descriptor, iconElement, ELEMENT.url, icon.getUri()); } else if (deviceModel instanceof LocalDevice) { appendNewElementIfNotNull(descriptor, iconElement, ELEMENT.url, namespace.getIconPath(icon)); } } }
public static Element appendNewElement(Document document, Element parent, String element, Object content) { return appendNewElement(document, parent, element, content, null); }
protected void generateSpecVersion(Namespace namespace, Device deviceModel, Document descriptor, Element rootElement) { Element specVersionElement = appendNewElement(descriptor, rootElement, ELEMENT.specVersion); appendNewElementIfNotNull(descriptor, specVersionElement, ELEMENT.major, deviceModel.getVersion().getMajor()); appendNewElementIfNotNull(descriptor, specVersionElement, ELEMENT.minor, deviceModel.getVersion().getMinor()); }
public static Element appendNewElement(Document document, Element parent, Enum el) { return appendNewElement(document, parent, el.toString()); }
private void generateAction(Action action, Document descriptor, Element actionListElement) { Element actionElement = appendNewElement(descriptor, actionListElement, ELEMENT.action); appendNewElementIfNotNull(descriptor, actionElement, ELEMENT.name, action.getName()); if (action.hasArguments()) { Element argumentListElement = appendNewElement(descriptor, actionElement, ELEMENT.argumentList); for (ActionArgument actionArgument : action.getArguments()) { generateActionArgument(actionArgument, descriptor, argumentListElement); } } }
protected void writeFaultElement(Document d, Element bodyElement, ActionInvocation actionInvocation) { Element faultElement = d.createElementNS(Constants.SOAP_NS_ENVELOPE, "s:Fault"); bodyElement.appendChild(faultElement); // This stuff is really completely arbitrary nonsense... let's hope they fired the guy who decided this XMLUtil.appendNewElement(d, faultElement, "faultcode", "s:Client"); XMLUtil.appendNewElement(d, faultElement, "faultstring", "UPnPError"); Element detailElement = d.createElement("detail"); faultElement.appendChild(detailElement); Element upnpErrorElement = d.createElementNS(Constants.NS_UPNP_CONTROL_10, "UPnPError"); detailElement.appendChild(upnpErrorElement); int errorCode = actionInvocation.getFailure().getErrorCode(); String errorDescription = actionInvocation.getFailure().getMessage(); log.fine("Writing fault element: " + errorCode + " - " + errorDescription); XMLUtil.appendNewElement(d, upnpErrorElement, "errorCode", Integer.toString(errorCode)); XMLUtil.appendNewElement(d, upnpErrorElement, "errorDescription", errorDescription); }
protected void generateServiceList(Namespace namespace, Device deviceModel, Document descriptor, Element deviceElement) { if (!deviceModel.hasServices()) return; Element serviceListElement = appendNewElement(descriptor, deviceElement, ELEMENT.serviceList); for (Service service : deviceModel.getServices()) { Element serviceElement = appendNewElement(descriptor, serviceListElement, ELEMENT.service); appendNewElementIfNotNull(descriptor, serviceElement, ELEMENT.serviceType, service.getServiceType()); appendNewElementIfNotNull(descriptor, serviceElement, ELEMENT.serviceId, service.getServiceId()); if (service instanceof RemoteService) { RemoteService rs = (RemoteService) service; appendNewElementIfNotNull(descriptor, serviceElement, ELEMENT.SCPDURL, rs.getDescriptorURI()); appendNewElementIfNotNull(descriptor, serviceElement, ELEMENT.controlURL, rs.getControlURI()); appendNewElementIfNotNull(descriptor, serviceElement, ELEMENT.eventSubURL, rs.getEventSubscriptionURI()); } else if (service instanceof LocalService) { LocalService ls = (LocalService) service; appendNewElementIfNotNull(descriptor, serviceElement, ELEMENT.SCPDURL, namespace.getDescriptorPath(ls)); appendNewElementIfNotNull(descriptor, serviceElement, ELEMENT.controlURL, namespace.getControlPath(ls)); appendNewElementIfNotNull(descriptor, serviceElement, ELEMENT.eventSubURL, namespace.getEventSubscriptionPath(ls)); } } }
protected void generateEventedValue(EventedValue eventedValue, Document descriptor, Element parentElement) { String name = eventedValue.getName(); Map.Entry<String, String>[] attributes = eventedValue.getAttributes(); if (attributes != null && attributes.length > 0) { Element evElement = appendNewElement(descriptor, parentElement, name); for (Map.Entry<String, String> attr : attributes) { evElement.setAttribute(attr.getKey(), DOMParser.escape(attr.getValue())); } } }
private void generateSpecVersion(Service serviceModel, Document descriptor, Element rootElement) { Element specVersionElement = appendNewElement(descriptor, rootElement, ELEMENT.specVersion); appendNewElementIfNotNull(descriptor, specVersionElement, ELEMENT.major, serviceModel.getDevice().getVersion().getMajor()); appendNewElementIfNotNull(descriptor, specVersionElement, ELEMENT.minor, serviceModel.getDevice().getVersion().getMinor()); }
private void generateServiceStateTable(Service serviceModel, Document descriptor, Element scpdElement) { Element serviceStateTableElement = appendNewElement(descriptor, scpdElement, ELEMENT.serviceStateTable); for (StateVariable stateVariable : serviceModel.getStateVariables()) { generateStateVariable(stateVariable, descriptor, serviceStateTableElement); } }
private void generateActionList(Service serviceModel, Document descriptor, Element scpdElement) { Element actionListElement = appendNewElement(descriptor, scpdElement, ELEMENT.actionList); for (Action action : serviceModel.getActions()) { if (!action.getName().equals(QueryStateVariableAction.ACTION_NAME)) generateAction(action, descriptor, actionListElement); } }
protected void writeProperties(Document d, Element propertysetElement, OutgoingEventRequestMessage message) { for (StateVariableValue stateVariableValue : message.getStateVariableValues()) { Element propertyElement = d.createElementNS(Constants.NS_UPNP_EVENT_10, "e:property"); propertysetElement.appendChild(propertyElement); XMLUtil.appendNewElement( d, propertyElement, stateVariableValue.getStateVariable().getName(), stateVariableValue.toString() ); } }
protected void generateDescMetadata(DescMeta descMeta, Document descriptor, Element parent) { if (descMeta.getId() == null) { throw new RuntimeException("Missing id of description metadata: " + descMeta); } if (descMeta.getNameSpace() == null) { throw new RuntimeException("Missing namespace of description metadata: " + descMeta); } Element descElement = appendNewElement(descriptor, parent, "desc"); descElement.setAttribute("id", descMeta.getId()); descElement.setAttribute("nameSpace", descMeta.getNameSpace().toString()); if (descMeta.getType() != null) descElement.setAttribute("type", descMeta.getType()); populateDescMetadata(descElement, descMeta); }
protected void generateDeviceList(Namespace namespace, Device deviceModel, Document descriptor, Element deviceElement, RemoteClientInfo info) { if (!deviceModel.hasEmbeddedDevices()) return; Element deviceListElement = appendNewElement(descriptor, deviceElement, ELEMENT.deviceList); for (Device device : deviceModel.getEmbeddedDevices()) { generateDevice(namespace, device, descriptor, deviceListElement, info); } }
protected void writeActionInputArguments(Document d, Element actionRequestElement, ActionInvocation actionInvocation) { for (ActionArgument argument : actionInvocation.getAction().getInputArguments()) { log.fine("Writing action input argument: " + argument.getName()); String value = actionInvocation.getInput(argument) != null ? actionInvocation.getInput(argument).toString() : ""; XMLUtil.appendNewElement(d, actionRequestElement, argument.getName(), value); } }
protected void generateResource(Res resource, Document descriptor, Element parent) { if (resource.getValue() == null) { throw new RuntimeException("Missing resource URI value" + resource); } if (resource.getProtocolInfo() == null) { throw new RuntimeException("Missing resource protocol info: " + resource); } Element resourceElement = appendNewElement(descriptor, parent, "res", resource.getValue()); resourceElement.setAttribute("protocolInfo", resource.getProtocolInfo().toString()); if (resource.getImportUri() != null) resourceElement.setAttribute("importUri", resource.getImportUri().toString()); if (resource.getSize() != null) resourceElement.setAttribute("size", resource.getSize().toString()); if (resource.getDuration() != null) resourceElement.setAttribute("duration", resource.getDuration()); if (resource.getBitrate() != null) resourceElement.setAttribute("bitrate", resource.getBitrate().toString()); if (resource.getSampleFrequency() != null) resourceElement.setAttribute("sampleFrequency", resource.getSampleFrequency().toString()); if (resource.getBitsPerSample() != null) resourceElement.setAttribute("bitsPerSample", resource.getBitsPerSample().toString()); if (resource.getNrAudioChannels() != null) resourceElement.setAttribute("nrAudioChannels", resource.getNrAudioChannels().toString()); if (resource.getColorDepth() != null) resourceElement.setAttribute("colorDepth", resource.getColorDepth().toString()); if (resource.getProtection() != null) resourceElement.setAttribute("protection", resource.getProtection()); if (resource.getResolution() != null) resourceElement.setAttribute("resolution", resource.getResolution()); }
protected void writeActionOutputArguments(Document d, Element actionResponseElement, ActionInvocation actionInvocation) { for (ActionArgument argument : actionInvocation.getAction().getOutputArguments()) { log.fine("Writing action output argument: " + argument.getName()); String value = actionInvocation.getOutput(argument) != null ? actionInvocation.getOutput(argument).toString() : ""; XMLUtil.appendNewElement(d, actionResponseElement, argument.getName(), value); } }
protected void generateInstanceIDs(Event event, Document descriptor, Element rootElement) { for (InstanceID instanceID : event.getInstanceIDs()) { if (instanceID.getId() == null) continue; Element instanceIDElement = appendNewElement(descriptor, rootElement, CONSTANTS.InstanceID.name()); instanceIDElement.setAttribute(CONSTANTS.val.name(), instanceID.getId().toString()); for (EventedValue eventedValue : instanceID.getValues()) { generateEventedValue(eventedValue, descriptor, instanceIDElement); } } }