Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * #%L
   * =====================================================
   *   _____                _     ____  _   _       _   _
   *  |_   _|_ __ _   _ ___| |_  / __ \| | | | ___ | | | |
   *    | | | '__| | | / __| __|/ / _` | |_| |/ __|| |_| |
   *    | | | |  | |_| \__ \ |_| | (_| |  _  |\__ \|  _  |
   *    |_| |_|   \__,_|___/\__|\ \__,_|_| |_||___/|_| |_|
   *                             \____/
  * 
  * =====================================================
  * 
  * Hochschule Hannover
  * (University of Applied Sciences and Arts, Hannover)
  * Faculty IV, Dept. of Computer Science
  * Ricklinger Stadtweg 118, 30459 Hannover, Germany
  * 
  * Email: trust@f4-i.fh-hannover.de
  * Website: http://trust.f4.hs-hannover.de/
  * 
  * This file is part of ifmapj, version 2.2.0, implemented by the Trust@HsH
  * research group at the Hochschule Hannover.
  * %%
  * Copyright (C) 2010 - 2014 Trust@HsH
  * %%
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * #L%
  */
 package de.hshannover.f4.trust.ifmapj.identifier;
 
 
 import java.util.List;
 import java.util.Map;
 
 
Provides access to Identifier associated classes.

Author(s):
aw
Since:
0.1.4
 
 public final class Identifiers {
 
 
 	@SuppressWarnings("deprecation")
 	private static Map<Class<? extends Identifier>,
 
 	private Identifiers() { }
 
 	@SuppressWarnings("deprecation")
 	public static IdentifierFactory getIdentifierFactory() {
 		if ( == null) {
 		}
 
 	}
 
 	public static void registerIdentifierHandler(IdentifierHandler<? extends Identifierih) {
 		if (ih == null) {
 			throw new NullPointerException("ih is null");
 		}
 
 		if (ih.handles() == null) {
 			throw new NullPointerException("ih.handles() returns null");
 		}
 
 		if ( == null) {
 		}
 
			throw new RuntimeException("IdentifierHandler already registered for "
ih.handles());
		}
	}
	public static IdentifierHandler<? extends IdentifiergetHandlerFor(Identifier i) {
		if ( == null) {
		}
		for (Entry<Class<? extends Identifier>,
				IdentifierHandler<? extends Identifier>> entry
			if (entry.getKey().isInstance(i)) {
				return entry.getValue();
			}
		}
		return null;
	}
	private static void initializeDefaultHandlers() {
	}
	public static Identifier tryFromElement(Element el)
		Identifier ret = null;
		if ( == null) {
		}
		for (IdentifierHandler<? extends Identifierhandler
			ret = handler.fromElement(el);
			if (ret != null) {
				break;
			}
		}
		return ret;
	}
	public static Identifier fromElement(Element el)
		if (ret == null) {
			throw new UnmarshalException("No IdentifierHandler could parse element with name "
el.getLocalName() + " in namespace " + el.getNamespaceURI());
		}
		return ret;
	}
	public static Element tryToElement(Identifier iDocument doc) {
		IdentifierHandler<? extends Identifierih = null;
		Element elId = null;
		ih = Identifiers.getHandlerFor(i);
		if (ih == null) {
			IfmapJLog.warn("No IdentifierHandler for " + i.getClass());
			return null;
		}
		try {
			elId = ih.toElement(idoc);
catch (MarshalException e) {
			IfmapJLog.warn("IdentifierHandler for " + i.getClass()
" threw exception:" + e.getMessage());
			return null;
		}
		if (elId == null) {
			IfmapJLog.warn("IdentifierHandler for " + i.getClass()
" returned null!");
		}
		return elId;
	}
	public static Element toElement(Identifier iDocument doc)
		Element ret = tryToElement(idoc);
		if (ret == null) {
			throw new MarshalException("No IdentifierHandler or error during conversion of Identifier to Element");
		}
		return ret;
	}
	public static IdentityType getIdentityType(String s) {
		for (IdentityType i : IdentityType.values()) {
			if (i.toString().equals(s)) {
				return i;
			}
		}
		return null;
	}

Create an access-request identifier with an random UUID as name.

Returns:
the new AccessRequest instance.
	public static AccessRequest createArRandomUuid() {
		return new AccessRequest(java.util.UUID.randomUUID().toString());
	}

Create an access-request identifier with an random UUID as name and administrative-domain as given.

Returns:
the new AccessRequest instance.
	public static AccessRequest createArRandomUuid(String admDom) {
		return new AccessRequest(java.util.UUID.randomUUID().toString(), admDom);
	}


Create an access-request identifier with a random string of length 16 as name.

Returns:
the new AccessRequest instance.
	public static AccessRequest createArRandom() {
		return new AccessRequest(StringHelpers.getRandomString(16));
	}

Create an access-request identifier with a random string of length 16 as name and administrative-domain as given.

Returns:
the new AccessRequest instance.
	public static AccessRequest createArRandom(String admDom) {
		return new AccessRequest(StringHelpers.getRandomString(16), admDom);
	}

Create an access-request identifier with the given name. You may optionally set the administrative-domain with IdentifierWithAd.setAdministrativeDomain(java.lang.String) too (default is left out).

Parameters:
name the name of the access-request
Returns:
the new AccessRequest instance
	public static AccessRequest createAr(String name) {
		return new AccessRequest(name);
	}

Create an access-request identifier with the given name and the given administrative-domain.

Parameters:
name the name of the access-request
admDom the administrative-domain of the access-request
Returns:
the new AccessRequest instance
	public static AccessRequest createAr(String nameString admDom) {
		return new AccessRequest(nameadmDom);
	}

Create an access-request identifier with with "pubId:name" as name and the given administrative-domain.

Parameters:
name the name of the access-request
admDom the administrative-domain of the access-request
pubId the publisher id
Returns:
the new AccessRequest instance
	public static AccessRequest createArPubPrefixed(String name,  String pubId,
			String admDom) {
		return new AccessRequest(pubId + ":" + nameadmDom);
	}

Create an access-request identifier with with "pubId:name" as name.

Parameters:
name the name of the access-request
pubId the publisher id
Returns:
the new AccessRequest instance
	public static AccessRequest createArPubPrefixed(String nameString pubId) {
		return new AccessRequest(pubId + ":" + name);
	}

Create a device identifier with a random UUID as name.

Returns:
the new Device instance
	public static Device createDevRandomUuid() {
		return new Device(java.util.UUID.randomUUID().toString());
	}

Create a device identifier with a random string of length 16.

Returns:
the new Device instance
	public static Device createDevRandom() {
		return new Device(StringHelpers.getRandomString(16));
	}

Create a device identifier with the given name.

Parameters:
name the name of the device identifier
Returns:
the new Device instance
	public static Device createDev(String name) {
		return new Device(name);
	}

Create a device identifier with the given name and prefix it with the publisher id. (pubID:UID format)

Parameters:
name the name of the device identifier
pubId the devices publisher id
Returns:
the new Device instance
	public static Device createDevPubPrefixed(String nameString pubId) {
		return new Device(pubId + ":" + name);
	}

Create an identity identifier with the given name and the given type.

Parameters:
type the type of the identity identifier
name the name of the identity identifier
Returns:
the new Identity instance
	public static Identity createIdentity(IdentityType typeString name) {
		return createIdentity(typenamenullnull);
	}

Create an identity identifier with the given parameters.

Parameters:
type the type of the identity identifier
name the name of the identity identifier
admDom the administrative-domain of the identity identifier
Returns:
the new Identity instance
	public static Identity createIdentity(IdentityType typeString nameString admDom) {
		return createIdentity(typenameadmDomnull);
	}

Create an other identity identifier. Note: The type is set to IdentityType.other by default.

Parameters:
name the name of the identity identifier
admDom the administrative-domain of the identity identifier
otherTypeDef vendor specific java.lang.String
Returns:
the new Identity instance
	public static Identity createOtherIdentity(String nameString admDom,
			String otherTypeDef) {
		return createIdentity(.nameadmDomotherTypeDef);
	}

Create an other identity identifier. Note: The type is set to IdentityType.other by default.

Parameters:
name the name of the identity identifier
admDom the administrative-domain of the identity identifier
otherTypeDef vendor specific java.lang.String
vendorId the vendor id to prefix the identity name with
Returns:
the new Identity instance
	public static Identity createOtherIdentity(String nameString admDom,
			String otherTypeDefString vendorId) {
		return createIdentity(.vendorId + ":" + name,
				admDomotherTypeDef);
	}

Create an identity identifier.

Note: otherTypeDef != null should imply type IdentityType.other.

Parameters:
type the type of the identity identifier
name the name of the identity identifier
admDom the administrative-domain of the identity identifier
otherTypeDef vendor specific java.lang.String
Returns:
the new Identity instance
	public static Identity createIdentity(IdentityType typeString nameString admDom,
			String otherTypeDef) {
		return new Identity(typenameadmDomotherTypeDef);
	}

Create an extended Identity identifier with empty administrative domain.

Returns:
the new extended Identity instance
Throws:
TransformerConfigurationException
	public static Identity createExtendedIdentity(Document docthrows MarshalException {
		String idName = DomHelpers.prepareExtendedIdentifier(doc);
		return createIdentity(.idNamenull,
	}

Create an extended Identity identifier with empty administrative domain.

Parameters:
extendedIdentifier the extended identifier XML
Returns:
the new extended Identity instance
Throws:
TransformerConfigurationException
	public static Identity createExtendedIdentity(InputStream extendedIdentifier)
		return createExtendedIdentity(DomHelpers.toDocument(extendedIdentifier));
	}

Create an extended Identity identifier with empty administrative domain.

Parameters:
extendedIdentifier the extended identifier XML
Returns:
the new extended Identity instance
Throws:
TransformerConfigurationException
	public static Identity createExtendedIdentity(String extendedIdentifier)
		return createExtendedIdentity(extendedIdentifiernull);
	}

Create an extended Identity identifier.

Parameters:
extendedIdentifier the extended identifier XML
c charset used for encoding the string
Returns:
the new extended identity instance
Throws:
IfmapException
	public static Identity createExtendedIdentity(String extendedIdentifier,
			Charset cthrows MarshalException {
		return createExtendedIdentity(DomHelpers.toDocument(extendedIdentifierc));
	}

Create an ip-address identifier for IPv4 with the given value.

Parameters:
value a java.lang.String that represents a valid IPv4 address
Returns:
the new ip-address identifier
	public static IpAddress createIp4(String value) {
		return createIp4(valuenull);
	}

Create an ip-address identifier for IPv4 with the given value and the given administrative-domain.

Parameters:
value a java.lang.String that represents a valid IPv4 address
admDom the administrative-domain
Returns:
the new ip-address identifier
	public static IpAddress createIp4(String valueString admDom) {
		return createIp(.valueadmDom);
	}

Create an ip-address identifier for IPv6 with the given value.

Parameters:
value a java.lang.String that represents a valid IPv6 address
Returns:
the new ip-address identifier
	public static IpAddress createIp6(String value) {
		return createIp6(valuenull);
	}

Create an ip-address identifier for IPv6 with the given parameters.

Parameters:
value a java.lang.String that represents a valid IPv4 address
admDom the administrative-domain
Returns:
the new ip-address identifier
	public static IpAddress createIp6(String valueString admDom) {
		return createIp(.valueadmDom);
	}

Create an ip-address identifier with the given parameters.

Parameters:
type the type of the ip-address identifier
value a java.lang.String that represents a valid IPv4/6 address
admDom the administrative-domain
Returns:
the new ip-address identifier
	public static IpAddress createIp(IpAddressType typeString valueString admDom) {
		return new IpAddress(typevalueadmDom);
	}

Create a mac-address identifier with the given value.

Parameters:
value a java.lang.String that represents a valid MAC address
Returns:
the new mac-address identifier
	public static MacAddress createMac(String value) {
		return createMac(valuenull);
	}

Create a mac-address identifier with the given parameters.

Parameters:
value a java.lang.String that represents a valid MAC address
admDom the administrative-domain
Returns:
the new mac-address identifier
	public static MacAddress createMac(String valueString admDom) {
		return new MacAddress(valueadmDom);
	}

Create a mac-address identifier with the given value.

Parameters:
value a byte array that represents a valid MAC address
Returns:
the new mac-address identifier
	public static MacAddress createMacFromByte(byte[] value) {
		return createMacFromByte(valuenull);
	}

Create a mac-address identifier with the given parameters.

Parameters:
value a byte array that represents a valid MAC address
admDom the administrative-domain
Returns:
the new mac-address identifier
	public static MacAddress createMacFromByte(byte[] valueString admDom) {
		return new MacAddress(valueadmDom);
	}
	public static final class Helpers {
		private Helpers() { }
		public static String getAdministrativeDomain(Element el) {
			return node != null && node.getValue().length() > 0 ? node.getValue() : null;
		}

Check if the Identifier instance is one that can be handled by the given IdentifierHandler.

		public static void checkIdentifierType(Identifier i,
				IdentifierHandler<? extends Identifierihthrows MarshalException {
			Class<? extends Identifierclazz = ih.handles();
			if (!clazz.isInstance(i)) {
				throw new MarshalException("Handler for identifier of type "
ih.handles() + " got identifier of type " + i.getClass());
			}
		}

If the given Identifier has an administrative-domain set, add it to the given org.w3c.dom.Element instance.

Parameters:
to
i
		public static void addAdministrativeDomain(Element toIdentifierWithAd i) {
			if (adom != null && adom.length() > 0) {
				DomHelpers.addAttribute(to,
			}
		}
	}
// IDENTIFIER HANDLER IMPLEMENTATIONS
		Helpers.checkIdentifierType(ithis);
		String name = ar.getName();
		if (name == null) {
			throw new MarshalException("AccessRequest with null name not allowed");
		}
		if (name.length() == 0) {
			throw new MarshalException("AccessRequest with empty name not allowed");
		}
		Element ret = DomHelpers.createNonNsElement(doc,
		Helpers.addAdministrativeDomain(retar);
		return ret;
	}
		String name = null;
		// Are we responsible? return null if not.
			return null;
		}
		if (name == null || name.length() == 0) {
			throw new UnmarshalException("No or empty " + . + " found for "
		}
		return Identifiers.createAr(name, Helpers.getAdministrativeDomain(el));
	}
		return AccessRequest.class;
	}
class DeviceHandler implements IdentifierHandler<Device> {
		Helpers.checkIdentifierType(ithis);
		Device dev = (Devicei;
		String name = dev.getName();
		if (name == null) {
			throw new MarshalException("Device with null name not allowed");
		}
		if (name.length() == 0) {
			throw new MarshalException("Device with empty name not allowed");
		}
		Element ret = DomHelpers.createNonNsElement(doc,
		Element nameEl = DomHelpers.createNonNsElement(doc,
		Node text = doc.createTextNode(name);
		ret.appendChild(nameEl);
		nameEl.appendChild(text);
		return ret;
	}
	public Device fromElement(Element elthrows UnmarshalException {
		Element child = null;
		String name = null;
		// Are we responsible? return null if not.
		if (!DomHelpers.elementMatches(el.)) {
			return null;
		}
		List<Elementchildren = DomHelpers.getChildElements(el);
		if (children.size() != 1) {
" element? Has " + children.size() + " child elements.");
		}
		child = children.get(0);
		if (!DomHelpers.elementMatches(child.)) {
			throw new UnmarshalException("Unknown child element in " + . + " element: "
child.getLocalName());
		}
		name = child.getTextContent();
		if (name == null || name.length() == 0) {
			throw new UnmarshalException("No text content found");
		}
		return Identifiers.createDev(name);
	}
	public Class<Devicehandles() {
		return Device.class;
	}
		Helpers.checkIdentifierType(ithis);
		Identity id = (Identityi;
		String name = id.getName();
		IdentityType type = id.getType();
		String otherTypeDef = id.getOtherTypeDefinition();
		if (type == null) {
			throw new MarshalException("No Identity type set");
		}
		if (name == null) {
			throw new MarshalException("Identity with null name not allowed");
		}
		if (name.length() == 0) {
			throw new MarshalException("Identity with empty name not allowed");
		}
		Element ret = DomHelpers.createNonNsElement(doc,
		if (type == .) {
			if (otherTypeDef == null || otherTypeDef.length() == 0) {
				throw new MarshalException("Identity type=other requires other-type-definition");
			}
					otherTypeDef);
else {
			if (otherTypeDef != null && otherTypeDef.length() > 0) {
				throw new MarshalException("Identity other-type-definition is set, but type != other");
			}
		}
		Helpers.addAdministrativeDomain(retid);
		return ret;
	}
		IdentityType typeEnum = null;
		String name = null;
		String type = null;
		String otherTypeDef = null;
		// Are we responsible? return null if not.
		if (!DomHelpers.elementMatches(el.)) {
			return null;
		}
		if (name == null || name.length() == 0) {
" not set or empty for " + .);
		}
		if (type == null || type.length() == 0) {
" not set or empty for " + .);
		}
		for (IdentityType t : IdentityType.values()) {
			if (t.toString().equals(type)) {
				typeEnum = t;
				break;
			}
		}
		if (typeEnum == null) {
			throw new UnmarshalException("Illegal value for " +	. + ":" + type);
		}
		if (typeEnum == . && otherTypeDef.length() == 0) {
" with " + . + "=other, but"
		}
		// NOTE: THIS CHECK IS PRETTY HARSH AND MIGHT THORW AN EXCEPTION WHEN A
		// MAPS IS DOING SOMETHING WEIRD.
		// This is not the robustness principle...
		if (typeEnum != .
. + " set, but type not other");
		}
		return Identifiers.createIdentity(
				typeEnum,
				name,
				otherTypeDef);
	}
	public Class<Identityhandles() {
		return Identity.class;
	}
		Helpers.checkIdentifierType(ithis);
		IpAddress ip = (IpAddressi;
		String value = ip.getValue();
		if (value == null) {
			throw new MarshalException("IpAddress with null value not allowed");
		}
		if (value.length() == 0) {
			throw new MarshalException("IpAddress with empty value not allowed");
		}
		Element ret = DomHelpers.createNonNsElement(doc,
		type = ip.getType() != null ? ip.getType() : type;
		Helpers.addAdministrativeDomain(retip);
		return ret;
	}
		String value = null;
		String type = null;
		// Are we responsible? return null if not.
			return null;
		}
		}
		if (value == null || value.length() == 0) {
			throw new UnmarshalException("No or empty " + . + " for "
		}
		if (type != null) {	// if null then it's IPv4 anyway
			try {
				typeEnum = IpAddressType.valueOf(type);
catch (IllegalArgumentException e) {
				throw new UnmarshalException("Invalid type for " + . + " found:" + type);
			}
		}
		return Identifiers.createIp(
				typeEnum,
				value,
	}
	public Class<IpAddresshandles() {
		return IpAddress.class;
	}
		Helpers.checkIdentifierType(ithis);
		MacAddress mac = (MacAddressi;
		String value = mac.getValue();
		if (value == null) {
			throw new MarshalException("MacAddress with null value not allowed");
		}
		if (value.length() == 0) {
			throw new MarshalException("MacAddress with empty value not allowed");
		}
		if (!java.util.regex.Pattern.matches("^([0-9a-f]{2}[:]){5}([0-9a-f]{2})$"value)) {
			throw new MarshalException("MacAddress not valid");
		}
		Element ret = DomHelpers.createNonNsElement(doc,
		Helpers.addAdministrativeDomain(retmac);
		return ret;
	}
		String value = null;
		// Are we responsible? return null if not.
			return null;
		}
		if (value == null || value.length() == 0) {
			throw new UnmarshalException("No or empty value for " + . + " found");
		}
		return Identifiers.createMac(value, Helpers.getAdministrativeDomain(el));
	}
	public Class<MacAddresshandles() {
		return MacAddress.class;
	}
New to GrepCode? Check out our FAQ X