All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ajax4jsf.framework.renderer.compiler.ClassElement Maven / Gradle / Ivy

Go to download

Ajax4jsf is an open source extension to the JavaServer Faces standard that adds AJAX capability to JSF applications without requiring the writing of any JavaScript.

The newest version!
/**
 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.sun.com/cddl/
 *   
 * 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.ajax4jsf.framework.renderer.compiler;

import java.io.IOException;
import java.util.Iterator;

import javax.faces.FacesException;

import org.ajax4jsf.framework.util.message.Messages;
import org.xml.sax.SAXException;

/**
 * @author [email protected] (latest modification by $Author: alexsmirnov $)
 * @version $Revision: 1.4 $ $Date: 2006/09/04 14:07:03 $
 * 
 */
public class ClassElement extends ElementBase {

	private String _name;

	private static final String[] url = { "url(", ")" };

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ajax4jsf.framework.renderer.compiler.ElementBase#encode(org.ajax4jsf.framework.renderer.compiler.TemplateContext,
	 *      java.lang.String)
	 */
	public void encode(TemplateContext context, String breakPoint)
			throws IOException {
		encode(context);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ajax4jsf.framework.renderer.compiler.ElementBase#encode(org.ajax4jsf.framework.renderer.compiler.TemplateContext)
	 */
	public void encode(TemplateContext context) throws IOException {
		context.getWriter().writeText(getString(context), null);
	}

	/**
	 * @return Returns the name.
	 */
	public String getName() {
		return _name;
	}

	/**
	 * @param name
	 *            The name to set.
	 */
	public void setName(String name) {
		_name = name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ajax4jsf.framework.renderer.compiler.ElementBase#getString(org.ajax4jsf.framework.renderer.compiler.TemplateContext)
	 */
	public String getString(TemplateContext context) throws FacesException {
		if (null == getName()) {
			throw new FacesException(Messages
					.getMessage(Messages.STYLE_ATTRIBUTE_ERROR));
		}
		String stringOrDefault = valueGetter.getStringOrDefault(context);
		StringBuffer string = new StringBuffer();
		string.append(stringOrDefault);
		for (Iterator iter = getChildren().iterator(); iter.hasNext();) {
			Object element = iter.next();
			if (element instanceof ElementBase) {
				ElementBase stringElement = (ElementBase) element;
				String str = stringElement.getString(context);
				if (str.length() > 0) {
					string.append(" ");
					boolean isUrl = element instanceof ResourceElement;
					if (isUrl) {
						string.append(url[0]);
					}
					string.append(str);
					if (isUrl) {
						string.append(url[1]);
					}
				}
			}
		}
		if (string.length() > 0) {
			string.insert(0, ":").insert(0, getName()).append(";");
			return string.toString();
		} else {
			return "";
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ajax4jsf.framework.renderer.compiler.PreparedTemplate#getTag()
	 */
	public String getTag() {
		// TODO Auto-generated method stub
		return HtmlCompiler.NS_PREFIX + HtmlCompiler.STYLE_TAG;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ajax4jsf.framework.renderer.compiler.ElementBase#setParent(org.ajax4jsf.framework.renderer.compiler.PreparedTemplate)
	 */
	public void setParent(PreparedTemplate parent) throws SAXException {
		super.setParent(parent);
		if (getName() == null) {
			throw new SAXException(Messages.getMessage(
					Messages.NO_NAME_ATTRIBUTE_ERROR, getTag()));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ajax4jsf.framework.renderer.compiler.ElementBase#getAllowedClasses()
	 */
	protected Class[] getAllowedClasses() {
		// TODO Auto-generated method stub
		return new Class[] { MethodCallElement.class, TextElement.class,
				ResourceElement.class };
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy