/**
* Copyright (C) 2001-2005 France Telecom R&D
*/
package org.objectweb.speedo.lib;
import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.NameController;
import org.objectweb.fractal.util.Fractal;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;
import java.util.ArrayList;
import java.util.StringTokenizer;
/**
* This class provides static method for helping the use of Fractal.
*
* @author S.Chassande-Barrioz
*/
public class FractalHelper {
/**
* Searches a sub component from a parent and a path in the architecture
* @param parent is the root composite component
* @param path is a dotted string representing a path in the architecture
* @param logger for the debug of the search
* @return the component if it has been found, otherwise null.
* @throws NoSuchInterfaceException when a component has no name (no
* NameController in fact)
*/
public static Component getSubComponent(Component parent, String path, Logger logger)
throws NoSuchInterfaceException {
final boolean debug = logger != null && logger.isLoggable(BasicLevel.DEBUG);
if (debug) {
logger.log(BasicLevel.DEBUG, "initial component: "
+ Fractal.getNameController(parent).getFcName()
+ ", path: " + path);
}
//run over the path
final StringTokenizer st = new StringTokenizer(path, ".", false);
Component res = parent;
while (st.hasMoreTokens()) {
String commponenentname = st.nextToken();
Component[] children = Fractal.getContentController(res)
.getFcSubComponents();
int i = 0;
boolean found = false;
//the list of sub component names (used for debug only)
final ArrayList subNames = (debug ? new ArrayList() : null);
while (!found && i < children.length) {
String subName = Fractal.getNameController(children[i]).getFcName();
found = subName.equals(commponenentname);
if (!found) {
if (debug) {
subNames.add(subName);
}
i++;
} else {
res = children[i];
}
}
if (found) {
if (debug) {
logger.log(BasicLevel.DEBUG, "sub component '"
+ commponenentname + "' found: " + res);
}
} else {
if (debug) {
logger.log(BasicLevel.DEBUG, "sub component '"
+ commponenentname + "' not found (among: " + subNames);
}
return null;
}
}
if (debug) {
logger.log(BasicLevel.DEBUG, "Path entirely found: " + res);
}
return res;
}
}