Merge branch 'refactor' into develop
This commit is contained in:
commit
fa63861f04
src
main/java/se/su/dsv/scipro/util/xml
test/java/se/su/dsv/scipro/configuration
@ -0,0 +1,106 @@
|
||||
package se.su.dsv.scipro.util.xml;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.namespace.NamespaceContext;
|
||||
|
||||
import edu.emory.mathcs.backport.java.util.Collections;
|
||||
|
||||
/**
|
||||
* Utility class for providing configurable namespace contexts, satisfies specification defined in the NamespaceContext interface.
|
||||
* The default namespace-prefixes (DEFAULT_NS_PREFIX, XML_NS_PREFIX and XMLNS_ATTRIBUTE) are automatically mapped to defaults and are not configurable from clients.
|
||||
* Typical usage:
|
||||
* <code>
|
||||
* MutableNamespaceContext mnc = new MutableNamespaceContext();
|
||||
* mnc.setMapping("somePrefix", "someNamespaceURI");
|
||||
* XPath xp = .....
|
||||
* xp.setNamespaceContext(mnc);
|
||||
* </code>
|
||||
*
|
||||
*/
|
||||
public final class MutableNamespaceContext implements NamespaceContext {
|
||||
private final Map<String,String> nsMap = new HashMap<String,String>();
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
public MutableNamespaceContext(){
|
||||
setupDefaults();
|
||||
}
|
||||
/**
|
||||
* Attempts to resolve the given prefix into a namespaceURI.
|
||||
* @return The namespace URI or XMLConstants.NULL_NS_URI if no mapping is found.
|
||||
* @throws IllegalStateException if the given prefix is null.
|
||||
*/
|
||||
@Override
|
||||
public String getNamespaceURI(String prefix){
|
||||
if(prefix == null)
|
||||
throw new IllegalStateException("A null prefix not allowed");
|
||||
if(!nsMap.containsKey(prefix))
|
||||
return XMLConstants.NULL_NS_URI;
|
||||
else
|
||||
return nsMap.get(prefix);
|
||||
}
|
||||
/**
|
||||
* Resolves all registered prefixes for the given URI.
|
||||
* @param uri
|
||||
* @return An iterator of all registered prefixes for the given URI, the empty iterator if none are found.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Iterator<String> getPrefixes(String URI) {
|
||||
if(URI == null)
|
||||
throw new IllegalStateException("A null URI not allowed");
|
||||
List<String> list = new ArrayList<String>();
|
||||
for(Map.Entry<String, String> entry : nsMap.entrySet()){
|
||||
if(entry.getValue().equals(URI))
|
||||
list.add(entry.getKey());
|
||||
}
|
||||
return Collections.unmodifiableList(list).iterator();
|
||||
}
|
||||
/**
|
||||
* Resolves one prefix for the given URI.
|
||||
* @param uri
|
||||
* @return The registered prefix
|
||||
*/
|
||||
@Override
|
||||
public String getPrefix(String URI) {
|
||||
if(URI == null)
|
||||
throw new IllegalStateException("A null URI not allowed");
|
||||
final Iterator<String> itr = getPrefixes(URI);
|
||||
if(itr.hasNext())
|
||||
return itr.next();
|
||||
else
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* Sets a prefix to URI mapping.
|
||||
* Overwrites existing mappings if existing, does not allow setting any of the default name space prefix mappings or mapping alternative prefixes for the default name space URI's.
|
||||
* @param prefix
|
||||
* @param URI
|
||||
* @throws IllegalStateException if either argument is null.
|
||||
*/
|
||||
public void setMapping(final String prefix, final String URI){
|
||||
if(prefix==null || URI==null)
|
||||
throw new IllegalStateException("Null prefix or URI not allowed");
|
||||
if(!prefix.equals(XMLConstants.DEFAULT_NS_PREFIX) && !prefix.equals(XMLConstants.XML_NS_PREFIX) && !prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)
|
||||
&& !URI.equals(XMLConstants.XML_NS_URI) && !URI.equals(XMLConstants.XML_NS_URI) && !URI.equals(XMLConstants.NULL_NS_URI))
|
||||
nsMap.put(prefix, URI);
|
||||
}
|
||||
/**
|
||||
* Clear user defined mappings.
|
||||
*/
|
||||
public void clearMappings(){
|
||||
nsMap.clear();
|
||||
setupDefaults();
|
||||
}
|
||||
private void setupDefaults(){
|
||||
nsMap.put(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
|
||||
nsMap.put(XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
|
||||
nsMap.put(XMLConstants.DEFAULT_NS_PREFIX, XMLConstants.NULL_NS_URI);
|
||||
}
|
||||
}
|
@ -27,6 +27,8 @@ import org.w3c.dom.Node;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import se.su.dsv.scipro.util.xml.MutableNamespaceContext;
|
||||
|
||||
/**
|
||||
* Assert that deploy configuration is upheld before build can be completed.
|
||||
* @author Martin Peters - mpeters@dsv.su.se
|
||||
@ -107,21 +109,21 @@ public class TestDeployConfiguration {
|
||||
public void testExternalAuthCfg() throws XPathExpressionException, IOException{
|
||||
InputSource applicationContextXml = getInputSource(applicationContextPath);
|
||||
XPath xPath = getApplicationContextXPath();
|
||||
Assert.assertEquals("true", xPath.evaluate("/defaultNS:beans/defaultNS:bean[@id='applicationSettings']/defaultNS:property[@name='acceptExternalAuthentication']/@value", applicationContextXml));
|
||||
Assert.assertEquals("true", xPath.evaluate("/beanNS:beans/beanNS:bean[@id='applicationSettings']/beanNS:property[@name='acceptExternalAuthentication']/@value", applicationContextXml));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoteUserLookupCfg() throws XPathExpressionException, IOException{
|
||||
InputSource applicationContextXml = getInputSource(applicationContextPath);
|
||||
XPath xPath = getApplicationContextXPath();
|
||||
Assert.assertEquals("true", xPath.evaluate("/defaultNS:beans/defaultNS:bean[@id='applicationSettings']/defaultNS:property[@name='enableRemoteUserLookup']/@value", applicationContextXml));
|
||||
Assert.assertEquals("true", xPath.evaluate("/beanNS:beans/beanNS:bean[@id='applicationSettings']/beanNS:property[@name='enableRemoteUserLookup']/@value", applicationContextXml));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoteUserRemoteUserLookupUrlCfg() throws XPathExpressionException, IOException{
|
||||
InputSource applicationContextXml = getInputSource(applicationContextPath);
|
||||
XPath xPath = getApplicationContextXPath();
|
||||
Assert.assertEquals("https://thesis.dsv.su.se/match/json", xPath.evaluate("/defaultNS:beans/defaultNS:bean[@id='applicationSettings']/defaultNS:property[@name='remoteLookupUrl']/@value", applicationContextXml));
|
||||
Assert.assertEquals("https://thesis.dsv.su.se/match/json", xPath.evaluate("/beanNS:beans/beanNS:bean[@id='applicationSettings']/beanNS:property[@name='remoteLookupUrl']/@value", applicationContextXml));
|
||||
}
|
||||
|
||||
private InputSource getInputSource(String filePath) throws IOException{
|
||||
@ -142,28 +144,9 @@ public class TestDeployConfiguration {
|
||||
}
|
||||
private XPath getApplicationContextXPath(){
|
||||
XPath xPath = XPathFactory.newInstance().newXPath();
|
||||
xPath.setNamespaceContext(new NamespaceContext(){
|
||||
@Override
|
||||
public String getNamespaceURI(String prefix){
|
||||
if(prefix.equals("defaultNS"))
|
||||
return "http://www.springframework.org/schema/beans";
|
||||
else
|
||||
return "";
|
||||
}
|
||||
@Override
|
||||
public Iterator<String> getPrefixes(String val) {
|
||||
ArrayList<String> list = new ArrayList<String>();
|
||||
list.add(getPrefix(val));
|
||||
return list.iterator();
|
||||
}
|
||||
@Override
|
||||
public String getPrefix(String uri) {
|
||||
if(uri.equals("http://www.springframework.org/schema/beans"))
|
||||
return "defaultNS";
|
||||
else
|
||||
return "";
|
||||
}
|
||||
});
|
||||
MutableNamespaceContext mnc = new MutableNamespaceContext();
|
||||
mnc.setMapping("beanNS","http://www.springframework.org/schema/beans");
|
||||
xPath.setNamespaceContext(mnc);
|
||||
return xPath;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user