Author: nbelaevski
Date: 2010-06-29 18:50:34 -0400 (Tue, 29 Jun 2010)
New Revision: 17681
Added:
root/build/resources/trunk/faces-shade-transformers/src/main/java/org/richfaces/build/shade/resource/NamespacesTracker.java
Modified:
root/build/resources/trunk/faces-shade-transformers/src/main/java/org/richfaces/build/shade/resource/BaseFacesResourceTransformer.java
root/build/resources/trunk/faces-shade-transformers/src/main/java/org/richfaces/build/shade/resource/FacesConfigXmlResourceTransformer.java
root/build/resources/trunk/faces-shade-transformers/src/main/java/org/richfaces/build/shade/resource/TaglibXmlResourceTransformer.java
root/build/resources/trunk/faces-shade-transformers/src/test/jars/functions-ui-4.0.0-SNAPSHOT.jar
Log:
https://jira.jboss.org/browse/RF-8769
Modified:
root/build/resources/trunk/faces-shade-transformers/src/main/java/org/richfaces/build/shade/resource/BaseFacesResourceTransformer.java
===================================================================
---
root/build/resources/trunk/faces-shade-transformers/src/main/java/org/richfaces/build/shade/resource/BaseFacesResourceTransformer.java 2010-06-29
10:16:16 UTC (rev 17680)
+++
root/build/resources/trunk/faces-shade-transformers/src/main/java/org/richfaces/build/shade/resource/BaseFacesResourceTransformer.java 2010-06-29
22:50:34 UTC (rev 17681)
@@ -25,6 +25,7 @@
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
@@ -51,25 +52,32 @@
protected static final String META_INF_PATH = "META-INF/";
- protected static final String JAVAEE_URI =
"http://java.sun.com/xml/ns/javaee";
-
protected static final String JAVAEE_PREFIX = "javaee";
- protected static final Namespace JAVAEE_NAMESPACE =
Namespace.getNamespace(JAVAEE_PREFIX, JAVAEE_URI);
+ protected static final String JAVAEE_URI =
"http://java.sun.com/xml/ns/javaee";
- protected static final Namespace XSI_NAMESPACE =
Namespace.getNamespace("xsi",
"http://www.w3.org/2001/XMLSchema-instance");
-
+ private static final String XSI_URI =
"http://www.w3.org/2001/XMLSchema-instance";
+
+ private static final String XSI_PREFIX = "xsi";
+
+ protected NamespacesTracker namespacesFactory = new NamespacesTracker();
+
protected static XPath createXPath(String path) throws JDOMException {
XPath xPath = XPath.newInstance(path);
- xPath.addNamespace(JAVAEE_NAMESPACE);
- xPath.addNamespace(XSI_NAMESPACE);
+ xPath.addNamespace(Namespace.getNamespace(JAVAEE_PREFIX, JAVAEE_URI));
+ xPath.addNamespace(Namespace.getNamespace(XSI_PREFIX, XSI_URI));
return xPath;
}
+ protected Namespace getJavaEENamespace() {
+ return namespacesFactory.getNamespace(JAVAEE_URI, null);
+ }
+
protected void addSchemaLocation(Element element, String schemaLocation) {
if (schemaLocation != null && schemaLocation.length() != 0) {
- element.setAttribute("schemaLocation", JAVAEE_URI + " " +
schemaLocation, XSI_NAMESPACE);
+ Namespace xsiNamespace = namespacesFactory.getNamespace(XSI_URI,
XSI_PREFIX);
+ element.setAttribute("schemaLocation", JAVAEE_URI + " " +
schemaLocation, xsiNamespace);
}
}
@@ -77,15 +85,15 @@
if (object instanceof Element) {
Element element = (Element) object;
- if (JAVAEE_URI.equals(element.getNamespaceURI())) {
- element.setNamespace(JAVAEE_NAMESPACE);
- }
+
element.setNamespace(namespacesFactory.getNamespace(element.getNamespace()));
for (Object attributeObject : element.getAttributes()) {
Attribute attribute = (Attribute) attributeObject;
- if (JAVAEE_URI.equals(attribute.getNamespaceURI())) {
- attribute.setNamespace(JAVAEE_NAMESPACE);
+ if (!Namespace.NO_NAMESPACE.equals(attribute.getNamespace())) {
+
attribute.setNamespace(namespacesFactory.getNamespace(attribute.getNamespace()));
+ } else {
+ attribute.setNamespace(element.getNamespace());
}
}
@@ -97,7 +105,7 @@
protected boolean isJavaEEOrDefaultNamespace(Element element) {
String namespaceURI = element.getNamespaceURI();
- if (namespaceURI == null || namespaceURI.length() == 0) {
+ if (namespaceURI == null || namespaceURI.trim().length() == 0) {
return true;
}
@@ -123,11 +131,26 @@
jos.putNextEntry(new JarEntry(resourceName));
Format prettyFormat = Format.getPrettyFormat();
prettyFormat.setIndent(" ");
+
+ Element rootElement = document.getRootElement();
+ Collection<Namespace> namespaces = namespacesFactory.getNamespaces();
+ for (Namespace namespace : namespaces) {
+ if (namespace.getPrefix().length() == 0) {
+ continue;
+ }
+
+ rootElement.addNamespaceDeclaration(namespace);
+ }
+
new XMLOutputter(prettyFormat).output(document, jos);
}
protected abstract void processDocument(String resource, Document document, List
relocators) throws JDOMException;
+ protected void resetTransformer() {
+ namespacesFactory = new NamespacesTracker();
+ }
+
protected String getMetaInfResourceName(String resource) {
if (!resource.startsWith(META_INF_PATH)) {
return null;
Modified:
root/build/resources/trunk/faces-shade-transformers/src/main/java/org/richfaces/build/shade/resource/FacesConfigXmlResourceTransformer.java
===================================================================
---
root/build/resources/trunk/faces-shade-transformers/src/main/java/org/richfaces/build/shade/resource/FacesConfigXmlResourceTransformer.java 2010-06-29
10:16:16 UTC (rev 17680)
+++
root/build/resources/trunk/faces-shade-transformers/src/main/java/org/richfaces/build/shade/resource/FacesConfigXmlResourceTransformer.java 2010-06-29
22:50:34 UTC (rev 17681)
@@ -37,6 +37,7 @@
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
+import org.jdom.Namespace;
import org.jdom.filter.ElementFilter;
import org.jdom.filter.Filter;
import org.jdom.xpath.XPath;
@@ -158,7 +159,7 @@
}
}
- Filter renderkitIdFilter = new ElementFilter().and(new
ElementFilter(RENDER_KIT_ID, JAVAEE_NAMESPACE).negate());
+ Filter renderkitIdFilter = new ElementFilter().and(new
ElementFilter(RENDER_KIT_ID, getJavaEENamespace()).negate());
XPath renderKitIdXPath = createXPath(RENDER_KIT_ID_EXPRESSION);
List<Element> children = checkedList(rootElement.getChildren(),
Element.class);
for (Element child : children) {
@@ -225,7 +226,8 @@
}
Document document = new Document();
- Element rootElement = new Element(FACES_CONFIG, JAVAEE_NAMESPACE);
+ Namespace javaEENamespace = getJavaEENamespace();
+ Element rootElement = new Element(FACES_CONFIG, javaEENamespace);
rootElement.setAttribute(VERSION, CURRENT_VERSION);
if (metadataComplete != ThreeState.UNDEFINED) {
@@ -241,7 +243,7 @@
String elementName = entry.getKey();
List<Element> aggregatorElementChildren = entry.getValue();
- Element aggregatorElement = new Element(elementName, JAVAEE_NAMESPACE);
+ Element aggregatorElement = new Element(elementName, javaEENamespace);
rootElementChildren.add(aggregatorElement);
Collections.sort(aggregatorElementChildren, comparator);
@@ -252,11 +254,11 @@
String renderkitId = entry.getKey();
List<Element> renderkitElementChildren = entry.getValue();
- Element renderkitElement = new Element(RENDER_KIT, JAVAEE_NAMESPACE);
+ Element renderkitElement = new Element(RENDER_KIT, javaEENamespace);
rootElementChildren.add(renderkitElement);
if (renderkitId.length() != 0) {
- Element renderkitIdElement = new Element(RENDER_KIT_ID,
JAVAEE_NAMESPACE);
+ Element renderkitIdElement = new Element(RENDER_KIT_ID,
javaEENamespace);
renderkitIdElement.setText(renderkitId);
renderkitElementChildren.add(renderkitIdElement);
}
@@ -271,13 +273,19 @@
appendToStream(FACES_CONFIG_FILE_PATH, document, os);
} finally {
- hasProcessedConfigFiles = false;
- metadataComplete = ThreeState.UNDEFINED;
-
- simpleElements.clear();
- aggregatorElements.clear();
- renderkitElements.clear();
+ resetTransformer();
}
}
+ @Override
+ protected void resetTransformer() {
+ super.resetTransformer();
+
+ hasProcessedConfigFiles = false;
+ metadataComplete = ThreeState.UNDEFINED;
+
+ simpleElements.clear();
+ aggregatorElements.clear();
+ renderkitElements.clear();
+ }
}
Added:
root/build/resources/trunk/faces-shade-transformers/src/main/java/org/richfaces/build/shade/resource/NamespacesTracker.java
===================================================================
---
root/build/resources/trunk/faces-shade-transformers/src/main/java/org/richfaces/build/shade/resource/NamespacesTracker.java
(rev 0)
+++
root/build/resources/trunk/faces-shade-transformers/src/main/java/org/richfaces/build/shade/resource/NamespacesTracker.java 2010-06-29
22:50:34 UTC (rev 17681)
@@ -0,0 +1,91 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.build.shade.resource;
+
+import java.text.MessageFormat;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.jdom.Namespace;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+final class NamespacesTracker {
+
+ //see MessageFormat class for format of this string
+ private static final String GENERATED_NS_PREFIX_FORMAT = "x{0}";
+
+ private Set<String> usedPrefixes = new HashSet<String>();
+
+ private Map<String, Namespace> namespaces = new HashMap<String,
Namespace>();
+
+ private int prefixGeneratorCounter = 0;
+
+ private String maskEmptyString(String s) {
+ if (s == null || s.trim().length() == 0) {
+ return "";
+ }
+
+ return s;
+ }
+
+ private Namespace createNamespace(String uri, String prefix) {
+ String maskedPrefix = maskEmptyString(prefix);
+
+ while (usedPrefixes.contains(maskedPrefix)) {
+ //generate next prefix using counter & format string
+ maskedPrefix = MessageFormat.format(GENERATED_NS_PREFIX_FORMAT,
prefixGeneratorCounter++);
+ }
+
+ return Namespace.getNamespace(maskedPrefix, uri);
+ }
+
+ public Namespace getNamespace(String nsUri, String nsPrefix) {
+ String maskedUri = maskEmptyString(nsUri);
+
+ Namespace namespace = namespaces.get(maskedUri);
+
+ if (namespace == null) {
+ namespace = createNamespace(maskedUri, nsPrefix);
+
+ usedPrefixes.add(namespace.getPrefix());
+ namespaces.put(maskedUri, namespace);
+ }
+
+ return namespace;
+ }
+
+ public Namespace getNamespace(Namespace namespace) {
+ return getNamespace(namespace.getURI(), namespace.getPrefix());
+ }
+
+ public Collection<Namespace> getNamespaces() {
+ return Collections.unmodifiableCollection(namespaces.values());
+ }
+
+}
Modified:
root/build/resources/trunk/faces-shade-transformers/src/main/java/org/richfaces/build/shade/resource/TaglibXmlResourceTransformer.java
===================================================================
---
root/build/resources/trunk/faces-shade-transformers/src/main/java/org/richfaces/build/shade/resource/TaglibXmlResourceTransformer.java 2010-06-29
10:16:16 UTC (rev 17680)
+++
root/build/resources/trunk/faces-shade-transformers/src/main/java/org/richfaces/build/shade/resource/TaglibXmlResourceTransformer.java 2010-06-29
22:50:34 UTC (rev 17681)
@@ -35,6 +35,7 @@
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
+import org.jdom.Namespace;
import org.jdom.filter.ElementFilter;
import org.jdom.filter.Filter;
import org.jdom.xpath.XPath;
@@ -65,7 +66,7 @@
private Map<String, Document> passThroughLibraries = new HashMap<String,
Document>();
private Taglib[] taglibs = new Taglib[0];
-
+
private Comparator<Element> createElementsComparator() throws JDOMException {
List<String> elements = Arrays.asList("description",
"display-name", "icon",
"library-class", NAMESPACE, "composite-library-name",
TAG, FUNCTION, "taglib-extension");
@@ -73,13 +74,13 @@
Map<String, XPath> elementNameExpressions = new HashMap<String,
XPath>();
String tagPathExpr = MessageFormat.format("./{0}:{1}|./{1}",
JAVAEE_PREFIX, TAG_NAME);
elementNameExpressions.put(TAG, createXPath(tagPathExpr));
-
+
String fnPathExpr = MessageFormat.format("./{0}:{1}|./{1}",
JAVAEE_PREFIX, FUNCTION_NAME);
elementNameExpressions.put(FUNCTION, createXPath(fnPathExpr));
-
+
return new ElementsComparator(JAVAEE_URI, elements, elementNameExpressions);
}
-
+
private String getShortName(String namespaceUri) {
int idx = namespaceUri.lastIndexOf('/');
if (idx < 0) {
@@ -89,12 +90,6 @@
}
}
- private Element createNamespaceElement(String namespaceUri) {
- Element nsElement = new Element(NAMESPACE, JAVAEE_NAMESPACE);
- nsElement.setText(namespaceUri);
- return nsElement;
- }
-
private String getFileName(String shortName) {
return META_INF_PATH + shortName + TAGLIB_XML_FILE_EXTENSION;
}
@@ -113,7 +108,7 @@
throw new IllegalArgumentException("Root element namespace: " +
element.getNamespaceURI());
}
}
-
+
public boolean hasTransformedResource() {
return !tagLibraries.isEmpty() || !passThroughLibraries.isEmpty();
}
@@ -126,21 +121,18 @@
appendToStream(resourceName, document, os);
}
- } finally {
- passThroughLibraries.clear();
- }
- if (!tagLibraries.isEmpty()) {
- Comparator<Element> elementsComparator;
- try {
- elementsComparator = createElementsComparator();
- } catch (JDOMException e) {
- throw new RuntimeException(e.getMessage(), e);
- }
+ if (!tagLibraries.isEmpty()) {
+ Comparator<Element> elementsComparator;
+ try {
+ elementsComparator = createElementsComparator();
+ } catch (JDOMException e) {
+ throw new RuntimeException(e.getMessage(), e);
+ }
- Filter filter = new ElementFilter().and(new ElementFilter(NAMESPACE,
JAVAEE_NAMESPACE).negate());
-
- try {
+ Namespace javaEENamespace = getJavaEENamespace();
+ Filter filter = new ElementFilter().and(new ElementFilter(NAMESPACE,
javaEENamespace).negate());
+
for (Map.Entry<String, List<Document>> entry :
tagLibraries.entrySet()) {
String namespaceUri = entry.getKey();
String shortName = getShortName(namespaceUri);
@@ -148,40 +140,50 @@
Document document = new org.jdom.Document();
- Element rootElement = new Element(FACELET_TAGLIB, JAVAEE_NAMESPACE);
+ Element rootElement = new Element(FACELET_TAGLIB, javaEENamespace);
rootElement.setAttribute(VERSION, CURRENT_VERSION);
addSchemaLocation(rootElement,
"http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd");
rootElement.setAttribute(ID, shortName);
-
+
document.addContent(rootElement);
List<Element> elements = new ArrayList<Element>();
- elements.add(createNamespaceElement(namespaceUri));
-
+
+ Element nsElement = new Element(NAMESPACE, javaEENamespace);
+ nsElement.setText(namespaceUri);
+ elements.add(nsElement);
+
for (Document sourceDocument : sourceDocuments) {
Element sourceRootElement = sourceDocument.getRootElement();
checkRootElement(sourceRootElement);
-
+
List<Element> tagsContent =
checkedList(sourceRootElement.getContent(filter), Element.class);
for (Element tagElement: tagsContent) {
Element clonedElement = cloneAndImportElement(tagElement);
elements.add(clonedElement);
}
}
-
+
Collections.sort(elements, elementsComparator);
rootElement.addContent(elements);
-
+
String fileName = getFileName(shortName);
appendToStream(fileName, document, os);
}
- } finally {
- tagLibraries.clear();
}
+ } finally {
+ resetTransformer();
}
}
@Override
+ protected void resetTransformer() {
+ super.resetTransformer();
+ passThroughLibraries.clear();
+ tagLibraries.clear();
+ }
+
+ @Override
protected void processDocument(String resource, Document document, List relocators)
throws JDOMException {
String namespaceUri = (String)
createXPath(NAMESPACE_EXPRESSION).valueOf(document);
if (namespaceUri == null || namespaceUri.length() == 0) {
@@ -193,7 +195,7 @@
break;
}
}
-
+
List<Document> documents = tagLibraries.get(namespaceUri);
if (documents == null) {
documents = new ArrayList<Document>();
Modified:
root/build/resources/trunk/faces-shade-transformers/src/test/jars/functions-ui-4.0.0-SNAPSHOT.jar
===================================================================
(Binary files differ)