org.richfaces.cdk.model.Name Maven / Gradle / Ivy
The newest version!
/*
* $Id$
*
* License Agreement.
*
* Rich Faces - Natural Ajax for Java Server Faces (JSF)
*
* Copyright (C) 2007 Exadel, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.richfaces.cdk.model;
import java.util.regex.Pattern;
/**
*
* Represents parts of component type/family/classname according to CDK naming conventions.
*
*
* @author [email protected]
*/
public class Name {
private static final Pattern NAME_PATTERN = Pattern.compile("^(?:(.+)\\.)?(?:(" + Classifier.component + "|"
+ Classifier.renderkit + "|" + Classifier.event + "|" + Classifier.taglib + ")\\.(?:([^\\.]+)\\.)?)?([^\\.]+)$");
/**
*
* Element type classifier - "component","event","renderkit","taglib"
*
*/
private Classifier classifier;
/**
*
* Markup-specific part of name ( "html","xhtml","wml" ... )
*
*/
private String markup;
/**
*
* represents library part prefix of name.
*
*/
private String prefix;
/**
*
* Simple name ( last word after a period ).
*
*/
private String simpleName;
/**
*
* Standard package names for components, renderers, event listeners and taglib.
*
*
* @author [email protected]
*/
public enum Classifier {
/**
*
*
*/
component,
/**
*
*
*/
renderkit,
/**
*
*
*/
event,
/**
*
*
*/
taglib,
behavior;
}
/**
*
* Creates RichFaces name representation from string.
*
*
* @param name
* @return
* @throws InvalidNameException
*/
public static Name create(String name) throws InvalidNameException {
Name cdkName = new Name();
StringBuilder prefix = new StringBuilder(name.length());
String[] parts = name.split("\\.");
cdkName.setSimpleName(parts[parts.length - 1]);
if (parts.length > 1) {
try {
cdkName.setClassifier(Classifier.valueOf(parts[parts.length - 2]));
fillPrefix(prefix, parts, parts.length - 2);
} catch (IllegalArgumentException e) {
if (parts.length > 2) {
try {
cdkName.setClassifier(Classifier.valueOf(parts[parts.length - 3]));
fillPrefix(prefix, parts, parts.length - 3);
cdkName.setMarkup(parts[parts.length - 2]);
} catch (IllegalArgumentException e1) {
fillPrefix(prefix, parts, parts.length - 1);
}
} else {
prefix.append(parts[0]);
}
}
if (prefix.length() > 0) {
cdkName.setPrefix(prefix.toString());
}
}
return cdkName;
}
/**
*
* Utility method that composes library prefix from first elements of array
*
*
* @param prefix buffer that collects prefix.
* @param parts package name parts
* @param size size of prefix part of array.
*/
private static void fillPrefix(StringBuilder prefix, String[] parts, int size) {
for (int i = 0; i < size; i++) {
if (i != 0) {
prefix.append('.');
}
prefix.append(parts[i]);
}
}
public static Name create(String prefix, String name) throws InvalidNameException {
Name cdkName = create(name);
if (prefix.equals(cdkName.getPrefix())) {
return new Name();
} else {
throw new InvalidNameException("Nape " + name + " does not start with prefix " + prefix);
}
}
public static Name create(String prefix, Classifier classifier, String name) throws InvalidNameException {
return new Name();
}
/**
*
*
*
* @return the prefix
*/
public String getPrefix() {
return prefix;
}
/**
*
*
*
* @param prefix the prefix to set
*/
public void setPrefix(String prefix) {
this.prefix = prefix;
}
/**
*
*
*
* @return the classifier
*/
public Classifier getClassifier() {
return classifier;
}
/**
*
*
*
* @param classifier the classifier to set
*/
public void setClassifier(Classifier classifier) {
this.classifier = classifier;
}
/**
*
*
*
* @return the markup
*/
public String getMarkup() {
return markup;
}
/**
*
*
*
* @param markup the markup to set
*/
public void setMarkup(String markup) {
this.markup = markup;
}
/**
*
*
*
* @return the simpleName
*/
public String getSimpleName() {
return simpleName;
}
/**
*
*
*
* @param simpleName the simpleName to set
*/
public void setSimpleName(String simpleName) {
this.simpleName = simpleName;
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
if (null != prefix) {
result.append(prefix).append('.');
}
if (null != classifier) {
result.append(classifier).append('.');
}
if (null != markup) {
result.append(markup).append('.');
}
result.append(simpleName);
return result.toString();
}
}