Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.hshannover.f4.trust.ifmapj.metadata;
  
  /*
   * #%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 1.0.0, implemented by the Trust@HsH
  * research group at the Hochschule Hannover.
  * 
  * IfmapJ is a lightweight, platform-independent, easy-to-use IF-MAP client
  * library for Java. IF-MAP is an XML based protocol for sharing data across
  * arbitrary components, specified by the Trusted Computing Group. IfmapJ is
  * maintained by the Trust@HsH group at the Hochschule Hannover. IfmapJ
  * was developed within the ESUKOM research project.
  * %%
  * Copyright (C) 2010 - 2013 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%
  */
 
 
 import java.util.List;
 import java.util.Map;
 
 
Simple implementation of the StandardIfmapMetadataFactory interface.

Author(s):
aw
ib
 
 
 	
 		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 		dbf.setNamespaceAware(true);
 		try {
 		} catch (ParserConfigurationException e) {
 			IfmapJLog.error("Could not get DocumentBuilder instance [" +
 					e.getMessage() + "]");
 			throw new RuntimeException(e);
 		}
 	}
 
 	public Document createIpMac(String startTimeString endTime,
 			String dhcpServer) {
 		
 		Element root = (Element)doc.getFirstChild();
 		
 		appendTextElementIfNotNull(docroot"start-time"startTime);
 		appendTextElementIfNotNull(docroot"end-time"endTime);
 		appendTextElementIfNotNull(docroot"dhcp-server"dhcpServer);
 		
 		return doc;
 	}
	public Document createIpMac() {
		return createIpMac(nullnullnull);
	}
	public Document createArMac() {
		return createStdSingleElementDocument("access-request-mac",
	}
	public Document createArDev() {
		return createStdSingleElementDocument("access-request-device",
	}
	public Document createArIp() {
		return createStdSingleElementDocument("access-request-ip",
	}
	public Document createAuthAs() {
		return createStdSingleElementDocument("authenticated-as",
	}
	public Document createAuthBy() {
		return createStdSingleElementDocument("authenticated-by",
	}
	public Document createDevIp() {
		return createStdSingleElementDocument("device-ip",
	}
		return createStdSingleElementDocument("discovered-by",
	}
	public Document createRole(String nameString administrativeDomain) {
		Element root = (Element)doc.getFirstChild();
		appendTextElementIfNotNull(docroot"administrative-domain",
				administrativeDomain);
		createAndAppendTextElementCheckNull(docroot"name"name);
		return doc;
	}
	public Document createRole(String name) {
		return createRole(namenull);
	}
	public Document createDevAttr(String name) {
		Document doc = createStdSingleElementDocument("device-attribute",
		Element root = (Element)doc.getFirstChild();
		createAndAppendTextElementCheckNull(docroot"name"name);
		return doc;
	}
	public Document createCapability(String nameString administrativeDomain) {
		Element root = (Element)doc.getFirstChild();
		createAndAppendTextElementCheckNull(docroot"name"name);
		appendTextElementIfNotNull(docroot"administrative-domain",
				administrativeDomain);
		return doc;
	}
	public Document createCapability(String name) {
		return createCapability(namenull);
	}
	public Document createDevChar(String manufacturerString modelString os,
			String osVersionString deviceTypeString discoveredTime,
			String discovererIdString discoveryMethod) {
		Document doc = createStdSingleElementDocument("device-characteristic",
		Element root = (Element)doc.getFirstChild();
		appendTextElementIfNotNull(docroot"manufacturer"manufacturer);
		appendTextElementIfNotNull(docroot"model"model);
		appendTextElementIfNotNull(docroot"os"os);
		appendTextElementIfNotNull(docroot"os-version"osVersion);
		appendTextElementIfNotNull(docroot"device-type"deviceType);
		createAndAppendTextElementCheckNull(docroot"discovered-time"discoveredTime);
		createAndAppendTextElementCheckNull(docroot"discoverer-id"discovererId);
		createAndAppendTextElementCheckNull(docroot"discovery-method"discoveryMethod);
		return doc;
	}
			EnforcementAction enforcementActionString otherTypeDefinition,
			String enforcementReason) {
		Document doc = createStdSingleElementDocument("enforcement-report",
		Element root = (Element)doc.getFirstChild();
		createAndAppendTextElementCheckNull(docroot"enforcement-action"enforcementAction);
		// TODO Do we need to check consistency with the action?
		appendTextElementIfNotNull(docroot"other-type-definition"otherTypeDefinition);
		appendTextElementIfNotNull(docroot"enforcement-reason"enforcementReason);
		return doc;
	}
	public Document createEvent(String nameString discoveredTime,
			String discovererIdInteger magnitudeInteger confidence,
			Significance significanceEventType type,
			String otherTypeDefinitionString information,
			String vulnerabilityUri) {
		Element root = (Element)doc.getFirstChild();
		createAndAppendTextElementCheckNull(docroot"name"name);
		createAndAppendTextElementCheckNull(docroot"discovered-time"discoveredTime);
		createAndAppendTextElementCheckNull(docroot"discoverer-id"discovererId);
		// TODO add checks for magnitude and confidence for correct range?
		createAndAppendTextElementCheckNull(docroot"magnitude"magnitude);
		createAndAppendTextElementCheckNull(docroot"confidence"confidence);
		createAndAppendTextElementCheckNull(docroot"significance"significance);
		appendTextElementIfNotNull(docroot"type"type);
		// TODO Do we need to check consistency with the type?
		appendTextElementIfNotNull(docroot"other-type-definition"otherTypeDefinition);
		appendTextElementIfNotNull(docroot"information"information);
		// TODO Do we need to check consistency with the type?
		appendTextElementIfNotNull(docroot"vulnerability-uri"vulnerabilityUri);
		return doc;
	}
	public Document create(String elementNameString qualifiedNameString uri,
			Cardinality cardinality) {
		return create(elementNamequalifiedNameuricardinality,
			new HashMap<StringString>());
	}
	public Document create(String elementNameString qualifiedNameString uri,
			Cardinality cardinalityString attrNameString attrValue) {
		HashMap<StringStringattributes = new HashMap<StringString>();
		attributes.put(attrNameattrValue);
		return create(elementNamequalifiedNameuricardinalityattributes);
	}
	public Document create(String elementNameString qualifiedNameString uri,
			Cardinality cardinalityHashMap<StringStringattributes) {
		Element e = doc.createElementNS(uriqualifiedName+":"+elementName);
		e.setAttributeNS(null"ifmap-cardinality"cardinality.toString());
		for (Map.Entry<StringStringattr : attributes.entrySet())
		e.setAttributeNS(nullattr.getKey(), attr.getValue());
		doc.createAttributeNS(uriqualifiedName);
		return doc;
	}
	public Document createLayer2Information(Integer vlanNumString vlanName,
			Integer portString administrativeDomain) {
		Document doc = createStdSingleElementDocument("layer2-information",
		Element root = (Element)doc.getFirstChild();
		/* non of them is mandatory */
		appendTextElementIfNotNull(docroot"vlan"vlanNum);
		appendTextElementIfNotNull(docroot"vlan-name"vlanName);
		appendTextElementIfNotNull(docroot"port"port);
		appendTextElementIfNotNull(docroot"administrative-domain",
				administrativeDomain);
		return doc;
	}
	public Document createLocation(List<LocationInformationlocationInformation
			String discoveredTimeString discovererId) {
		Element root = (Element)doc.getFirstChild();
		if (locationInformation == null || locationInformation.size() == 0)
			throw new NullPointerException("location-information needs to be set for location");
		for (LocationInformation tmpLoc : locationInformation)
			appendElementWithAttributes(docroot"location-information",
					"type"tmpLoc.mType"value"tmpLoc.mValue);
		createAndAppendTextElementCheckNull(docroot"discovered-time"discoveredTime);
		createAndAppendTextElementCheckNull(docroot"discoverer-id"discovererId);
		return doc;
	}
		Document doc = createStdSingleElementDocument("request-for-investigation",
		Element root = (Element)doc.getFirstChild();
		if (qualifier != null)
			root.setAttributeNS(null"qualifier"qualifier);
		return doc;
	}
			List<WlanSecurityTypessidUnicastSecurity,
			WlanSecurityType ssidGroupSecurity,
			List<WlanSecurityTypessidManagementSecurity) {
		Document doc = createStdSingleElementDocument("wlan-information",
		Element root = (Element)doc.getFirstChild();
		Element ssidUniSecEl = null;
		Element ssidMgmtSecEl = null;
		if (ssidUnicastSecurity == null || ssidUnicastSecurity.size() == 0)
			throw new NullPointerException("ssid-unicast-security to be set"
" for wlan-information");
		if (ssidGroupSecurity == null)
			throw new NullPointerException("ssid-group-security to be set for"
" wlan-information");
		if (ssidManagementSecurity == null || ssidManagementSecurity.size() == 0)
			throw new NullPointerException("ssid-management-security to be set"
" for wlan-information");
		appendTextElementIfNotNull(docroot"ssid"ssid);
		for (WlanSecurityType wlanSec : ssidUnicastSecurity) {
			ssidUniSecEl = createAndAppendTextElementCheckNull(docroot,
					"ssid-unicast-security"wlanSec.mWlanSecurityType.toString());
			if (wlanSec.mOtherTypeDefinition != null)
				ssidUniSecEl.setAttributeNS(null"other-type-definition",
						wlanSec.mOtherTypeDefinition);				
		}
				"ssid-group-security"ssidGroupSecurity.mWlanSecurityType.toString());
		if (ssidGroupSecurity.mOtherTypeDefinition != null)
			grpSecEl.setAttributeNS(null"other-type-definition",
					ssidGroupSecurity.mOtherTypeDefinition);				
		for (WlanSecurityType wlanSec : ssidManagementSecurity) {
			ssidMgmtSecEl = createAndAppendTextElementCheckNull(docroot,
					"ssid-management-security"wlanSec.mWlanSecurityType.toString());
			if (wlanSec.mOtherTypeDefinition != null)
				ssidMgmtSecEl.setAttributeNS(null"other-type-definition",
						wlanSec.mOtherTypeDefinition);				
		}
		return doc;
	}
	public Document createUnexpectedBehavior(String discoveredTimeString discovererId,
			Integer magnitudeInteger confidenceSignificance significance,
			String type) {
		Document doc = createStdSingleElementDocument("unexpected-behavior",
		Element root = (Element)doc.getFirstChild();
		createAndAppendTextElementCheckNull(docroot"discovered-time"discoveredTime);
		createAndAppendTextElementCheckNull(docroot"discoverer-id"discovererId);
		// TODO add checks for magnitude and confidence for correct range?
		createAndAppendTextElementCheckNull(docroot"magnitude"magnitude);
		createAndAppendTextElementCheckNull(docroot"confidence"confidence);
		createAndAppendTextElementCheckNull(docroot"significance"significance);
		appendTextElementIfNotNull(docroot"type"type);
		return doc;
	}
	public Document createClientTime(String time) {
		// Generate XML within OPERATIONAL-METADATA namespace (ifmap 2.1)
		Element root = (Element)doc.getFirstChild();
		root.setAttribute("current-timestamp"time);
		return doc;
	}
				card);
	}
				card);		
	}
			String uriCardinality cardinality) {
		Element e = doc.createElementNS(uriqualifiedName);
		e.setAttributeNS(null"ifmap-cardinality"cardinality.toString());
		return doc;
	}

Helper to create a new element with name elName and append it to the org.w3c.dom.Element given by parent if the given value is non-null. The new org.w3c.dom.Element will have org.w3c.dom.Text node containing value.

Parameters:
doc org.w3c.dom.Document where parent is located in
parent where to append the new element
elName the name of the new element.
value the value of the org.w3c.dom.Text node appended to the new element, using toString() on the object.
	private void appendTextElementIfNotNull(Document docElement parent,
			String elNameObject value) {
			if (value == null)
				return;
			createAndAppendTextElementCheckNull(docparentelNamevalue);
	}

Helper to create a new element with name elName and append it to the org.w3c.dom.Element given by parent. The new org.w3c.dom.Element will have org.w3c.dom.Text node containing value.

Parameters:
doc org.w3c.dom.Document where parent is located in
parent where to append the new element
elName the name of the new element.
value the value of the org.w3c.dom.Text node appended to the new element, using toString() on the object.
Returns:
the new org.w3c.dom.Element
Throws:
java.lang.NullPointerException if value is null, or any other parameter is null
			String elNameObject value) {
		if (doc == null || parent == null || elName == null)
				throw new NullPointerException("bad parameters given");
		if (value == null)
			throw new NullPointerException("null is not allowed for " + elName 
" in " + doc.getFirstChild().getLocalName());
		String valueStr = value.toString();
		if (valueStr == null)
			throw new NullPointerException("null-string not allowed for " + elName 
" in " + doc.getFirstChild().getLocalName());
		Element child = createAndAppendElement(docparentelName);
		Text txtCElement = doc.createTextNode(valueStr);
		child.appendChild(txtCElement);
		return child;
	}

Helper to create an org.w3c.dom.Element without a namespace in org.w3c.dom.Document doc and append it to the org.w3c.dom.Element given by parent.

Parameters:
doc the target org.w3c.dom.Document
parent the parent org.w3c.dom.Element
elName the name of the new org.w3c.dom.Element
Returns:
the new org.w3c.dom.Element
	private Element createAndAppendElement(Document docElement parentString elName) {
		Element el = doc.createElementNS(nullelName);
		parent.appendChild(el);
		return el;
	}

Helper to create an org.w3c.dom.Element without a namespace in org.w3c.dom.Document doc and append it to the org.w3c.dom.Element given by parent. Further, add all attributes to the newly created org.w3c.dom.Element specified by attrList.

Parameters:
doc the target org.w3c.dom.Document
parent the parent org.w3c.dom.Element
elName the name of the new org.w3c.dom.Element
attrList name value pairs of attributes
Returns:
the new org.w3c.dom.Element
			String elNameString... attrList) {
		if ((attrList.length % 2) != 0)
			throw new RuntimeException("Bad attrList length");
		Element el = createAndAppendElement(docparentelName);
		for (int i = 0; i < attrList.lengthi += 2)
			el.setAttributeNS(nullattrList[i], attrList[i + 1]);
		return el;
	}
New to GrepCode? Check out our FAQ X