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.decorator;
 
Common decorator-specific utility methods.

Author(s):
Emanuel Rabina
 
 public final class DecoratorUtilities {
 
 	private static final String TH_WITH = . + ":" + .;
 
 	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 DecoratorUtilities() {
 	}

Recursive search for an element within the given node in the DOM tree.

Parameters:
element Node to initiate the search from.
name Name of the element to look for.
Returns:
Element with the given name, or null if the element could not be found.
 
 	public static Element findElement(Element elementString name) {
 
 		if (element.getOriginalName().equals(name)) {
 			return element;
 		}
 		for (Element childelement.getElementChildren()) {
 			Element result = findElement(childname);
 			if (result != null) {
 				return result;
 			}
 		}
 		return null;
 	}

Replace the attributes of the source element with those of the target 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) {
 
 		if (targetelement == null || sourceelement == null) {
 			return;
 		}
 
 		for (Attribute contentattributesourceelement.getAttributeMap().values()) {
 			String attributename = contentattribute.getOriginalName();
 
 			// Merge th:with attributes to retain local variable declarations
 			if (attributename.equals() && targetelement.hasAttribute()) {
 				targetelement.setAttribute(attributenamecontentattribute.getValue() + "," +
 						targetelement.getAttributeValue());
 			}
 			else {
 				targetelement.setAttribute(contentattribute.getOriginalName(), contentattribute.getValue());
 			}
 		}
 	}

Replace the target element with the source element.

Parameters:
targettelement
sourceelement
	public static void pullContent(Element targettelementElement sourceelement) {
		// Clone target element without processing information to make Thymeleaf reprocesses it
		targettelement.clearChildren();
		targettelement.addChild(sourceelement.cloneNode(nullfalse));
		targettelement.getParent().extractChild(targettelement);
	}
New to GrepCode? Check out our FAQ X