Update of /cvsroot/sunxacml/sunxacml/com/sun/xacml/attr
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31308/com/sun/xacml/attr
Added Files:
StandardAttributeFactory.java
Log Message:
introduced new, still unstable, factories for standard 1.0/1.1 behavior
--- NEW FILE: StandardAttributeFactory.java ---
/*
* @(#)StandardAttributeFactory
*
* Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed or intended for use in
* the design, construction, operation or maintenance of any nuclear facility.
*/
package com.sun.xacml.attr;
import com.sun.xacml.ParsingException;
import com.sun.xacml.UnknownIdentifierException;
import java.net.URI;
import java.util.HashMap;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
/**
* This factory supports the standard set of datatypes specified in XACML
* 1.0 and 1.1. It is the default factory used by the system.
*
* @author Seth Proctor
*/
public class StandardAttributeFactory extends AttributeFactory
{
// the one instance of this factory
private static StandardAttributeFactory factoryInstance = null;
// the map of proxies
private HashMap attributeMap;
// dummy object used as a lock for the getFactory routine
// FIXME: this needs a better mechanism
private static Object factoryLock = new Object();
/**
* Private constructor that sets up proxies for all of the standard
* datatypes.
*/
private StandardAttributeFactory() {
attributeMap = new HashMap();
// boolean
attributeMap.put(BooleanAttribute.identifier, new AttributeProxy() {
public AttributeValue getInstance(Node root) throws Exception {
return BooleanAttribute.getInstance(root);
}
public AttributeValue getInstance(String value) throws Exception {
return BooleanAttribute.getInstance(value);
}
});
// date
attributeMap.put(DateAttribute.identifier, new AttributeProxy() {
public AttributeValue getInstance(Node root) throws Exception {
return DateAttribute.getInstance(root);
}
public AttributeValue getInstance(String value) throws Exception {
return DateAttribute.getInstance(value);
}
});
// time
attributeMap.put(TimeAttribute.identifier, new AttributeProxy() {
public AttributeValue getInstance(Node root) throws Exception {
return TimeAttribute.getInstance(root);
}
public AttributeValue getInstance(String value) throws Exception {
return TimeAttribute.getInstance(value);
}
});
// dateTime
attributeMap.put(DateTimeAttribute.identifier, new AttributeProxy() {
public AttributeValue getInstance(Node root) throws Exception {
return DateTimeAttribute.getInstance(root);
}
public AttributeValue getInstance(String value) throws Exception {
return DateTimeAttribute.getInstance(value);
}
});
// dayTimeDuration
attributeMap.put(DayTimeDurationAttribute.identifier,
new AttributeProxy() {
public AttributeValue getInstance(Node root) throws Exception {
return DayTimeDurationAttribute.getInstance(root);
}
public AttributeValue getInstance(String value) throws Exception {
return DayTimeDurationAttribute.getInstance(value);
}
});
// yearMonthDuration
attributeMap.put(YearMonthDurationAttribute.identifier,
new AttributeProxy() {
public AttributeValue getInstance(Node root) throws Exception {
return YearMonthDurationAttribute.getInstance(root);
}
public AttributeValue getInstance(String value) throws Exception {
return YearMonthDurationAttribute.getInstance(value);
}
});
// double
attributeMap.put(DoubleAttribute.identifier, new AttributeProxy() {
public AttributeValue getInstance(Node root) throws Exception {
return DoubleAttribute.getInstance(root);
}
public AttributeValue getInstance(String value) throws Exception {
return DoubleAttribute.getInstance(value);
}
});
// integer
attributeMap.put(IntegerAttribute.identifier, new AttributeProxy() {
public AttributeValue getInstance(Node root) throws Exception {
return IntegerAttribute.getInstance(root);
}
public AttributeValue getInstance(String value) throws Exception {
return IntegerAttribute.getInstance(value);
}
});
// string
attributeMap.put(StringAttribute.identifier, new AttributeProxy() {
public AttributeValue getInstance(Node root) {
return StringAttribute.getInstance(root);
}
public AttributeValue getInstance(String value) {
return StringAttribute.getInstance(value);
}
});
// anyURI
attributeMap.put(AnyURIAttribute.identifier, new AttributeProxy() {
public AttributeValue getInstance(Node root) throws Exception {
return AnyURIAttribute.getInstance(root);
}
public AttributeValue getInstance(String value) throws Exception {
return AnyURIAttribute.getInstance(value);
}
});
// hexBinary
attributeMap.put(HexBinaryAttribute.identifier, new AttributeProxy() {
public AttributeValue getInstance(Node root) throws Exception {
return HexBinaryAttribute.getInstance(root);
}
public AttributeValue getInstance(String value) throws Exception {
return HexBinaryAttribute.getInstance(value);
}
});
// base64Binary
attributeMap.put(Base64BinaryAttribute.identifier,
new AttributeProxy() {
public AttributeValue getInstance(Node root) throws Exception {
return Base64BinaryAttribute.getInstance(root);
}
public AttributeValue getInstance(String value) throws Exception {
return Base64BinaryAttribute.getInstance(value);
}
});
// x500Name
attributeMap.put(X500NameAttribute.identifier, new AttributeProxy() {
public AttributeValue getInstance(Node root) throws Exception {
return X500NameAttribute.getInstance(root);
}
public AttributeValue getInstance(String value) throws Exception {
return X500NameAttribute.getInstance(value);
}
});
// rfc822Name
attributeMap.put(RFC822NameAttribute.identifier, new AttributeProxy() {
public AttributeValue getInstance(Node root) {
return RFC822NameAttribute.getInstance(root);
}
public AttributeValue getInstance(String value) {
return RFC822NameAttribute.getInstance(value);
}
});
}
/**
* Returns the single instance of this factory, creating it if it doesn't
* exist yet.
*
* @return the factory instance
*/
public static AttributeFactory getFactory() {
if (factoryInstance == null) {
synchronized (factoryLock) {
if (factoryInstance == null)
factoryInstance = new StandardAttributeFactory();
}
}
return factoryInstance;
}
/**
* Adds a proxy to the factory, which in turn will allow new attribute
* types to be created using the factory. Typically the proxy is
* provided as an anonymous class that simply calls the getInstance
* methods (or something similar) of some <code>AttributeValue</code>
* class.
*
* @param id the name of the attribute type
* @param proxy the proxy used to create new attributes of the given type
*/
public void addDatatype(String id, AttributeProxy proxy) {
attributeMap.put(id, proxy);
}
/**
* Creates a value based on the given DOM root node. The type of the
* attribute is assumed to be present in the node as an XAML attribute
* named <code>DataType</code>, as is the case with the
* AttributeValueType in the policy schema. The value is assumed to be
* the first child of this node.
*
* @param root the DOM root of an attribute value
*
* @return a new <code>AttributeValue</code>
*
* @throws UnknownIdentifierException if the type in the node isn't
* known to the factory
* @throws ParsingException if the node is invalid or can't be parsed
* by the appropriate proxy
*/
public AttributeValue createValue(Node root)
throws UnknownIdentifierException, ParsingException
{
Node node = root.getAttributes().getNamedItem("DataType");
return createValue(root, node.getNodeValue());
}
/**
* Creates a value based on the given DOM root node and data type.
*
* @param root the DOM root of an attribute value
* @param dataType the type of the attribute
*
* @return a new <code>AttributeValue</code>
*
* @throws UnknownIdentifierException if the data type isn't known to
* the factory
* @throws ParsingException if the node is invalid or can't be parsed
* by the appropriate proxy
*/
public AttributeValue createValue(Node root, URI dataType)
throws UnknownIdentifierException, ParsingException
{
return createValue(root, dataType.toString());
}
/**
* Creates a value based on the given DOM root node and data type.
*
* @param root the DOM root of an attribute value
* @param type the type of the attribute
*
* @return a new <code>AttributeValue</code>
*
* @throws UnknownIdentifierException if the type isn't known to
* the factory
* @throws ParsingException if the node is invalid or can't be parsed
* by the appropriate proxy
*/
public AttributeValue createValue(Node root, String type)
throws UnknownIdentifierException, ParsingException
{
AttributeProxy proxy = (AttributeProxy)(attributeMap.get(type));
if (proxy != null) {
try {
return proxy.getInstance(root);
} catch (Exception e) {
throw new ParsingException("couldn't create " + type +
" attribute based on DOM node");
}
} else {
throw new UnknownIdentifierException("Attributes of type " + type +
" aren't supported.");
}
}
/**
* Creates a value based on the given data type and text-encoded value.
* Used primarily by code that does an XPath query to get an
* attribute value, and then needs to turn the resulting value into
* an Attribute class.
*
* @param dataType the type of the attribute
* @param value the text-encoded representation of an attribute's value
*
* @return a new <code>AttributeValue</code>
*
* @throws UnknownIdentifierException if the data type isn't known to
* the factory
* @throws ParsingException if the text is invalid or can't be parsed
* by the appropriate proxy
*/
public AttributeValue createValue(URI dataType, String value)
throws UnknownIdentifierException, ParsingException
{
String type = dataType.toString();
AttributeProxy proxy = (AttributeProxy)(attributeMap.get(type));
if (proxy != null) {
try {
return proxy.getInstance(value);
} catch (Exception e) {
throw new ParsingException("couldn't create " + type +
" attribute from input: " + value);
}
} else {
throw new UnknownIdentifierException("Attributes of type " + type +
" aren't supported.");
}
}
}
|