📄 upnpatomicgroundingimpl.java
字号:
// The MIT License
//
// Copyright (c) 2004 Evren Sirin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
package org.mindswap.owls.grounding.impl;
import org.cybergarage.upnp.Action;
import org.cybergarage.upnp.Argument;
import org.cybergarage.upnp.ArgumentList;
import org.cybergarage.upnp.ControlPoint;
import org.cybergarage.upnp.Device;
import org.cybergarage.upnp.Service;
import org.cybergarage.upnp.UPnPStatus;
import org.mindswap.owls.OWLSFactory;
import org.mindswap.owls.grounding.MessageMap;
import org.mindswap.owls.grounding.UPnPAtomicGrounding;
import org.mindswap.owls.process.Parameter;
import org.mindswap.owls.process.ValueMap;
import org.mindswap.utils.Utils;
import org.mindswap.utils.XSLTEngine;
import org.w3c.dom.Node;
import com.hp.hpl.jena.rdf.model.Resource;
/**
* @author Evren Sirin
*
*/
public class UPnPAtomicGroundingImpl extends AtomicGroundingImpl implements UPnPAtomicGrounding {
protected String upnpDescription;
protected String upnpService;
protected String upnpAction;
/**
*
*/
public UPnPAtomicGroundingImpl(Resource resource) {
super(resource);
}
/* (non-Javadoc)
* @see org.mindswap.owls.grounding.UPnPAtomicGrounding#setUPnPService(java.lang.String)
*/
public void setUPnPService(String service) {
upnpService = service;
}
/* (non-Javadoc)
* @see org.mindswap.owls.grounding.UPnPAtomicGrounding#getUPnPService()
*/
public String getUPnPService() {
return upnpService;
}
/* (non-Javadoc)
* @see org.mindswap.owls.grounding.UPnPAtomicGrounding#setUPnPAction(java.lang.String)
*/
public void setUPnPAction(String action) {
upnpAction = action;
}
/* (non-Javadoc)
* @see org.mindswap.owls.grounding.UPnPAtomicGrounding#getUPnPAction()
*/
public String getUPnPAction() {
return upnpAction;
}
/* (non-Javadoc)
* @see org.mindswap.owls.grounding.AtomicGrounding#getDescriptionURL()
*/
public String getDescriptionURL() {
ControlPoint cp = new ControlPoint();
Device device = cp.getDevice(upnpDescription);
return device.getPresentationURL();
}
/* (non-Javadoc)
* @see org.mindswap.owls.grounding.UPnPAtomicGrounding#setUPnPDescription(java.lang.String)
*/
public void setUPnPDescription(String description) {
upnpDescription = description;
}
/* (non-Javadoc)
* @see org.mindswap.owls.grounding.UPnPAtomicGrounding#getUPnPDescription()
*/
public String getUPnPDescription() {
return upnpDescription;
}
/* (non-Javadoc)
* @see org.mindswap.owls.grounding.AtomicGrounding#invoke(org.mindswap.owls.process.ValueMap)
*/
public ValueMap invoke(ValueMap values) {
ControlPoint cp = new ControlPoint();
Device device = cp.getDevice(upnpDescription);
Service service = device.getService(upnpService);
Action action = service.getAction(upnpAction);
ValueMap results = OWLSFactory.createValueMap();
ArgumentList inArguments = action.getInputArgumentList();
for(int i = 0; i < inArguments.size(); i++) {
Argument in = inArguments.getArgument(i);
MessageMap mp = getInputMap().getMessageMap(in.getName());
Parameter param = mp.getOWLSParameter();
Object value = values.getValue(param);
Object inputValue = value;
System.out.println("value = " + value);
if(mp.getTransformation() != null) {
value = XSLTEngine.transform(value.toString(), mp.getTransformation());
Node node = Utils.getAsNode(value.toString());
inputValue = node.getFirstChild().getNodeValue();
System.out.println("input value = " + value);
}
action.setArgumentValue(mp.getGroundingParameter(), inputValue.toString());
}
boolean ctrlRes = action.postControlAction();
if (ctrlRes == false) {
UPnPStatus err = action.getControlStatus();
throw new RuntimeException(err.getDescription() + " (" + Integer.toString(err.getCode()) + ")");
}
ArgumentList outArguments = action.getOutputArgumentList();
for(int i = 0; i < outArguments.size(); i++) {
Argument out = outArguments.getArgument(i);
MessageMap mp = getOutputMap().getMessageMap(out.getName());
if(mp == null) continue;
Parameter param = mp.getOWLSParameter();
Object outputValue = null;
if(mp.getTransformation() == null)
outputValue = out.getValue();
else
outputValue = XSLTEngine.transform(out.getValue().toString(), mp.getTransformation());
results.setValue(param, outputValue);
}
return results;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -