Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2013, Emanuel Rabina (http://www.ultraq.net.nz/)
   * 
   * 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 nz.net.ultraq.thymeleaf;
 
 import static nz.net.ultraq.thymeleaf.FragmentProcessor.PROCESSOR_NAME_FRAGMENT;
 import static nz.net.ultraq.thymeleaf.LayoutDialect.DIALECT_PREFIX_LAYOUT;
 
Common decorator-specific utility methods.

Author(s):
Emanuel Rabina
 
 public final class LayoutUtilities {
 
 	public static final String LINE_SEPARATOR     = System.getProperty("line.separator");
 	public static final String HTML_ELEMENT_HTML  = "html";
 	public static final String HTML_ELEMENT_HEAD  = "head";
 	public static final String HTML_ELEMENT_TITLE = "title";
 	public static final String HTML_ELEMENT_BODY  = "body";

Default hidden constructor as this class is only ever meant to be used statically.
 
 	private LayoutUtilities() {
 	}

Returns whether the attribute contains the given prefix and name parts. Checks both the prefixed and data- versions of a Thymeleaf attribute processor.

Parameters:
attribute
prefix
name
Returns:
true if the attribute name consists of the prefix and name parts.
 
 	public static boolean equalsAttributeName(Attribute attributeString prefixString name) {
 
 		String attributename = attribute.getOriginalName();
 		return attributename.equals(prefix + ":" + name) ||
 		       attributename.equals("data-" + prefix + "-" + name);
 	}

Returns the value of the attribute. Checks both the prefixed and data- versions of a Thymeleaf attribute processor.

Parameters:
element
prefix
name
Returns:
The value of the attribute, in either form, or null if the value doesn't exist in either form.
 
 	public static String getAttributeValue(Element elementString prefixString name) {
 
 		String value = element.getAttributeValue(prefix + ":" + name);
 		if (value == null) {
 			value = element.getAttributeValue("data-" + prefix + "-" + name);
 		}
 		return value;
 	}

Returns whether or not the attribute on the element exists. Checks both the prefixed and data- versions of a Thymeleaf attribute processor.

Parameters:
element
prefix
name
Returns:
true if the attribute exists on the element, in either form.
 
 	public static boolean hasAttribute(Element elementString prefixString name) {
 
 		return element.hasAttribute(prefix + ":" + name) ||
 		       element.hasAttribute("data-" + prefix + "-" + name);
 	}

Replace the attributes of the target element with those of the source element. A merge is done on the th:with attribute, while all other attributes are simply overidden.

Parameters:
targetelement
sourceelement
	public static void pullAttributes(Element targetelementElement sourceelement) {
		pullAttributes(targetelementsourceelementfalse);
	}

The same as pullAttributes(org.thymeleaf.dom.Element,org.thymeleaf.dom.Element), but with the option to specify whether to copy everything over, or only attributes that already exist in sourceelement.

Parameters:
targetelement
sourceelement
mergeonly true to pull only attributes that exist in targetelement. th:with values will continue to be brought in regardless.
	public static void pullAttributes(Element targetelementElement sourceelement,
		boolean mergeonly) {
		if (sourceelement == null || targetelement == null) {
			return;
		}
		for (Attribute sourceattributesourceelement.getAttributeMap().values()) {
			// Exclude the copying of fragment attributes
				continue;
			}
			// Merge th:with attributes to retain local variable declarations
				String mergedwithvalue = sourceattribute.getValue();
				String targetwithvalue = getAttributeValue(targetelement,
				if (targetwithvalue != null) {
					mergedwithvalue += "," + targetwithvalue;
				}
						mergedwithvalue);
				continue;
			}
			// Copy only attributes that already exist in the target element, or
			// copy any attributes
			if ((mergeonly && targetelement.hasAttribute(sourceattribute.getNormalizedName())) ||
				!mergeonly) {
				targetelement.setAttribute(sourceattribute.getOriginalName(), sourceattribute.getValue());
			}
		}
	}

Replace the content of the target element, with the content of the source element.

Parameters:
targetelement
sourceelement
	public static void pullContent(Element targetelementElement sourceelement) {
		// Clone target element without processing information to make Thymeleaf reprocesses it
		targetelement.clearChildren();
		targetelement.addChild(sourceelement.cloneNode(nullfalse));
		targetelement.getParent().extractChild(targetelement);
	}

Removes an attribute from an element. Removes both the prefixed and data- variations of the attribute.

Parameters:
element
prefix
name
	public static void removeAttribute(Element elementString prefixString name) {
		element.removeAttribute(prefix + ":" + name);
		element.removeAttribute("data-" + prefix + "-" + name);
	}
New to GrepCode? Check out our FAQ X