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

com.sun.faces.config.processor.BehaviorConfigProcessor Maven / Gradle / Ivy

There is a newer version: 4.1.1
Show newest version
/*
 * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

package com.sun.faces.config.processor;

import com.sun.faces.config.Verifier;
import com.sun.faces.config.manager.documents.DocumentInfo;
import com.sun.faces.util.FacesLogger;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Document;

import javax.faces.application.Application;
import javax.faces.component.behavior.Behavior;
import javax.faces.component.behavior.FacesBehavior;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.xml.xpath.XPathExpressionException;

import static java.text.MessageFormat.format;
import static java.util.logging.Level.FINE;

import java.text.MessageFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 

* This ConfigProcessor handles all elements defined under * /faces-config/behavior. *

*/ public class BehaviorConfigProcessor extends AbstractConfigProcessor { private static final Logger LOGGER = FacesLogger.CONFIG.getLogger(); /** *

* /faces-config/behavior *

*/ private static final String BEHAVIOR = "behavior"; /** *

* /faces-config/behavior/behavior-id *

*/ private static final String BEHAVIOR_ID = "behavior-id"; /** *

* /faces-config/behavior/behavior-class *

*/ private static final String BEHAVIOR_CLASS = "behavior-class"; // -------------------------------------------- Methods from ConfigProcessor /** * @see ConfigProcessor#process(javax.servlet.ServletContext,com.sun.faces.config.manager.documents.DocumentInfo[]) */ @Override public void process(ServletContext sc, FacesContext facesContext, DocumentInfo[] documentInfos) throws Exception { // process annotated Behaviors first as Behaviors configured // via config files take precedence processAnnotations(facesContext, FacesBehavior.class); for (int i = 0; i < documentInfos.length; i++) { if (LOGGER.isLoggable(FINE)) { LOGGER.log(FINE, format("Processing behavior elements for document: ''{0}''", documentInfos[i].getSourceURI())); } Document document = documentInfos[i].getDocument(); String namespace = document.getDocumentElement().getNamespaceURI(); NodeList behaviors = document.getDocumentElement().getElementsByTagNameNS(namespace, BEHAVIOR); if (behaviors != null && behaviors.getLength() > 0) { addBehaviors(behaviors, namespace); } } } // --------------------------------------------------------- Private Methods private void addBehaviors(NodeList behaviors, String namespace) throws XPathExpressionException { Application app = getApplication(); Verifier verifier = Verifier.getCurrentInstance(); for (int i = 0, size = behaviors.getLength(); i < size; i++) { Node behavior = behaviors.item(i); NodeList children = ((Element) behavior).getElementsByTagNameNS(namespace, "*"); String behaviorId = null; String behaviorClass = null; for (int c = 0, csize = children.getLength(); c < csize; c++) { Node n = children.item(c); if (n.getNodeType() == Node.ELEMENT_NODE) { switch (n.getLocalName()) { case BEHAVIOR_ID: behaviorId = getNodeText(n); break; case BEHAVIOR_CLASS: behaviorClass = getNodeText(n); break; } } } if (behaviorId != null && behaviorClass != null) { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, MessageFormat.format("Calling Application.addBehavior({0},{1})", behaviorId, behaviorClass)); } if (verifier != null) { verifier.validateObject(Verifier.ObjectType.BEHAVIOR, behaviorClass, Behavior.class); } app.addBehavior(behaviorId, behaviorClass); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy