Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011 Tomas Schlosser
   *
   * 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.
  */
 package org.anadix.html;
 
 
Class representing set of element's attributes. Contains both the attributes contained in tag and contained in style.

Author(s):
tomason
Version:
$Id: $
 
 public class Attributes {
 	private final Properties attributes;

Constructor

Parameters:
attributes list of key - value pairs
 
 	public Attributes(Properties attributes) {
 		if (attributes == null) {
 			throw new NullPointerException("attributes can't be null");
 		}
 		this. = new Properties();
 
 		for (String key : attributes.stringPropertyNames()) {
 			this..setProperty(keyattributes.getProperty(key));
 		}
 
 		if (hasAttribute("style")) {
 			Properties style = parseStyle(getAttribute("style"));
 			for (String key : style.stringPropertyNames()) {
 				this..setProperty(keystyle.getProperty(key));
 			}
 		}
 	}

Gets the attribute by name

Parameters:
key name of the attribute to get
Returns:
value of the attribute or null if attribute is not defined
 
 	public String getAttribute(String key) {
 		return .getProperty(key);
 	}

Gets the attribute by name

Parameters:
key name of the attribute to get
defaultValue default value to use if attribute is not defined
Returns:
value of the attribute or defaultValue if attribute is not defined
 
 	public String getAttribute(String keyString defaultValue) {
 		return .getProperty(keydefaultValue);
 	}

Determines whether attribute with given name is defined

Parameters:
key name of the attribute
Returns:
true if attribute is defined false otherwise
 
 	public boolean hasAttribute(String key) {
 		return .containsKey(key);
 	}

Gets names of all attributes

Returns:
Collection of attribute names
 
 	}

Parses the style into list of key - value pairs

Parameters:
style String representation of style
Returns:
list of key - value pairs
	public static Properties parseStyle(String style) {
		Properties result = new Properties();
		style = style.replace("\r""").replace("\n""").replace("\t""");
		Pattern p = Pattern.compile("[^:;]+:([^;\"]|\"[^\"]+\")*;?");
		Matcher m = p.matcher(style);
		while (m.find()) {
			String prop = m.group().trim();
			if (prop.endsWith(";")) {
				prop = prop.substring(0, prop.length() - 1);
			}
			String[] split = prop.split(":");
			if (split.length < 2) {
				Logger.getLogger(Attributes.class).warnf("CSS property without value: '%s'"prop);
				continue;
			}
			result.setProperty(split[0].trim(), split[1].trim());
		}
		return result;
	}

	public String toString() {
	}
New to GrepCode? Check out our FAQ X