From seam-commits at lists.jboss.org Wed Apr 23 15:27:50 2008
Content-Type: multipart/mixed; boundary="===============9222701042269356196=="
MIME-Version: 1.0
From: seam-commits at lists.jboss.org
To: seam-commits at lists.jboss.org
Subject: [seam-commits] Seam SVN: r8013 - trunk/src/main/org/jboss/seam/init.
Date: Wed, 23 Apr 2008 15:27:50 -0400
Message-ID: Converts an XML namespac=
e to a Java package name. The conversion algorithm is as follows:
- *
- *
- * java.net.URI
. Only absolute URIs are supported (i.e., a =
scheme must be specified).//
) with one exception:
- * seam:
, the URI is considered =
opaque, and is converted to a Java
- * package using alternate rules.
- * URI.getPath()
is mapped to further Java packages such th=
at each path element becomes another Java package
- * appended in left-to-right order.www
subdomain, if specified, is ignored.=
li>
- *
- * URI.getUserInfo()
,
- * URI.getPort()
,
- * URI.getQuery()
, and
- * URI.getFragment()
are ignored.
seam:
S=
chemeIf the scheme is seam:
, the URI is considered opaque=
, and is converted to a Java package using
- * these rules:
- *
period
(".")Characters specified in the URI which are not valid characters for J=
ava packages result in {@link #resolve(String)}
- * throwing IllegalArgumentException
, with one exception: the=
hyphen ("-") character is converted to
- * the valid Java package characer, the underscore ("_").
import
is a java keyword)Converts an XML namespace, ns
, to a Stringified packag=
e name according to the rules
- * detailed in this class's javadoc.
Characters specified in ns
which are not valid charact=
ers
- * for Java packages result IllegalArgumentException
being t=
hrown, with one exception. The
- * hyphen ("-") character is converted to the valid Java package characer=
, the underscore ("_").
Converts an XML namespace, ns
, to a Stringified pac=
kage name
+ *
+ * @param ns the xml namespace to convert
+ * =
+ * @returns a namespace descriptor
+ */
+ public String resolve(final String ns) {
+ try {
+ return parseURI(new URI(ns));
+ } catch (Exception e) {
+ // the exact exception doesn't matter here. The caller
+ // can log if needed
+ return null;
+ }
+ }
+ =
+ private String parseURI(URI uri) {
+ if (!uri.isAbsolute()) {
+ throw new IllegalArgumentException(uri + " is not an absolute =
URI");
+ }
=
- return uri.isOpaque() ? parseOpaqueURI(uri) : parseHierarchicalURI(uri);
- }
-
+ return uri.isOpaque() ? parseOpaqueURI(uri) : parseHierarchicalURI=
(uri);
+ }
+ =
+ /**
+ * java:package =
+ * seam:component
+ * seam:package:prefix
+ */
+ private String parseOpaqueURI(URI uri) {
+ if (uri.getScheme().equalsIgnoreCase(JAVA_SCHEME)) {
+ return uri.getSchemeSpecificPart();
+ }
+ throw new IllegalArgumentException("Unrecognized scheme in " + uri=
);
+ }
+ =
+ private String parseHierarchicalURI(URI uri) {
+ String scheme =3D uri.getScheme().toLowerCase();
+ if (!scheme.equals("http") && !scheme.equals("https")) {
+ throw new IllegalArgumentException("Hierarchical URLs must use=
http or https scheme " + uri);
+ }
=
- /**
- * java:package =
- * seam:component
- * seam:package:prefix
- */
- private String parseOpaqueURI(URI uri) {
- if (uri.getScheme().equalsIgnoreCase(JAVA_SCHEME)) {
- return uri.getSchemeSpecificPart();
- }
- throw new IllegalArgumentException("Unrecognized scheme in " + uri);
- }
-
- private String parseHierarchicalURI(URI uri) {
- String scheme =3D uri.getScheme().toLowerCase();
- if (!scheme.equals("http") && !scheme.equals("https")) {
- throw new IllegalArgumentException("Hierarchical URLs must use http or =
https scheme " + uri);
- }
- =
- StringBuffer buf =3D new StringBuffer();
- =
- appendToPackageName(buf, hostnameToPackage(uri.getHost()));
- appendToPackageName(buf, pathToPackage(uri.getPath()));
- =
- return buf.toString();
- }
-
- /**
- * Convert path elements to package names in forward order
- */
- String pathToPackage(String path) {
- StringBuffer buf =3D new StringBuffer();
- =
- if (path !=3D null) {
- String[] pathElements =3D path.split("/");
- for (int i =3D 1, len =3D pathElements.length; i < len; i++) {
- appendToPackageName(buf, pathElements[i]);
- }
- }
- =
- return buf.toString();
- }
+ StringBuffer buf =3D new StringBuffer();
=
- String hostnameToPackage(String hostname) {
- StringBuffer result =3D new StringBuffer(); =
- =
- String[] subdomains =3D hostname.split("\\.");
-
- //Iterate through the subdomains in reverse converting each to a package=
name. =
- for (int i =3D subdomains.length - 1; i >=3D 0; i--) {
- String subdomain =3D subdomains[i];
- if (i > 0 || !subdomain.equalsIgnoreCase("www")) {
- appendToPackageName(result, subdomain);
- }
- }
- =
- return result.toString();
- } =
-
- private void appendToPackageName(StringBuffer buf, String subdomain) {
- if (subdomain.length()>0) {
- subdomain =3D makeSafeForJava(subdomain);
+ appendToPackageName(buf, hostnameToPackage(uri.getHost()));
+ appendToPackageName(buf, pathToPackage(uri.getPath()));
=
- if (buf.length() > 0) {
- buf.append('.');
- }
- =
- buf.append(subdomain);
- }
- }
-
- /**
- * Converts characters in subdomain
which aren't java-friend=
ly
- * into java-friendly equivalents. Right now, we only support the convers=
ion
- * of hyphens ("-") to underscores ("_"). We could do other things like t=
oLowerCase(),
- * but there are instances of upper-case package names in widespread use =
even by the
- * likes of IBM (e.g.,
- * COM.ibm.db2 classnames).
- * =
- * @param subdomain
- * @return
- */
- private String makeSafeForJava(String subdomain) {
- return subdomain.replace("-", "_");
- }
-
+ return buf.toString();
+ }
+ =
+ /**
+ * Convert path elements to package names in forward order
+ */
+ private String pathToPackage(String path) {
+ StringBuffer buf =3D new StringBuffer();
+ =
+ if (path !=3D null) {
+ String[] pathElements =3D path.split("/");
+ for (int i =3D 1, len =3D pathElements.length; i < len; i++) {
+ appendToPackageName(buf, pathElements[i]);
+ }
+ }
+ =
+ return buf.toString();
+ }
+ =
+ private String hostnameToPackage(String hostname) {
+ StringBuffer result =3D new StringBuffer(); =
+ =
+ String[] subdomains =3D hostname.split("\\.");
+ =
+ //Iterate through the subdomains in reverse converting each to a p=
ackage name. =
+ for (int i =3D subdomains.length - 1; i >=3D 0; i--) {
+ String subdomain =3D subdomains[i];
+ if (i > 0 || !subdomain.equalsIgnoreCase("www")) {
+ appendToPackageName(result, subdomain);
+ }
+ }
+ =
+ return result.toString();
+ } =
+ =
+ private void appendToPackageName(StringBuffer buf, String subdomain) {
+ if (subdomain.length()>0) {
+ subdomain =3D makeSafeForJava(subdomain);
+ =
+ if (buf.length() > 0) {
+ buf.append('.');
+ }
+ =
+ buf.append(subdomain);
+ }
+ }
+ =
+ /**
+ * Converts characters in subdomain
which aren't java-fri=
endly
+ * into java-friendly equivalents. Right now, we only support the conv=
ersion
+ * of hyphens ("-") to underscores ("_"). We could do other things lik=
e toLowerCase(),
+ * but there are instances of upper-case package names in widespread u=
se even by the
+ * likes of IBM (e.g.,
+ * COM.ibm.db2 classnames).
+ * =
+ * @param subdomain
+ * @return
+ */
+ private String makeSafeForJava(String subdomain) {
+ return subdomain.replace("-", "_");
+ }
}
--===============9222701042269356196==--