You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by fl...@apache.org on 2010/11/09 20:10:26 UTC
svn commit: r1033162 - in /lenya/trunk/org.apache.lenya.core.utils: ./
src/main/java/org/apache/lenya/utils/
src/main/java/org/apache/lenya/utils/test/
src/main/java/org/apache/lenya/utils/xml/
src/test/java/org/apache/lenya/utils/
Author: florent
Date: Tue Nov 9 19:10:26 2010
New Revision: 1033162
URL: http://svn.apache.org/viewvc?rev=1033162&view=rev
Log:
- move some utilities classes in this module
Added:
lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/ServletHelper.java
lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/SourceUtil.java
lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/test/SpringEnv.java
lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/xml/
lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/xml/DocumentHelper.java
Modified:
lenya/trunk/org.apache.lenya.core.utils/pom.xml
lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/URLInformation.java
lenya/trunk/org.apache.lenya.core.utils/src/test/java/org/apache/lenya/utils/URLInformationTest.java
Modified: lenya/trunk/org.apache.lenya.core.utils/pom.xml
URL: http://svn.apache.org/viewvc/lenya/trunk/org.apache.lenya.core.utils/pom.xml?rev=1033162&r1=1033161&r2=1033162&view=diff
==============================================================================
--- lenya/trunk/org.apache.lenya.core.utils/pom.xml (original)
+++ lenya/trunk/org.apache.lenya.core.utils/pom.xml Tue Nov 9 19:10:26 2010
@@ -111,15 +111,16 @@
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
- <groupId>org.apache.lenya</groupId>
- <artifactId>lenya-core-api</artifactId>
- </dependency>
- <!-- Tests -->
- <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
- <scope>test</scope>
</dependency>
+ <!-- lenya -->
+
+ <dependency>
+ <groupId>org.apache.lenya</groupId>
+ <artifactId>lenya-core-api</artifactId>
+ </dependency>
+ <!-- Tests -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Added: lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/ServletHelper.java
URL: http://svn.apache.org/viewvc/lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/ServletHelper.java?rev=1033162&view=auto
==============================================================================
--- lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/ServletHelper.java (added)
+++ lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/ServletHelper.java Tue Nov 9 19:10:26 2010
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+/* $Id: ServletHelper.java 741654 2009-02-06 17:55:28Z andreas $ */
+
+package org.apache.lenya.utils;
+
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerException;
+
+import org.apache.cocoon.environment.Request;
+import org.apache.cocoon.environment.http.HttpRequest;
+import org.apache.cocoon.spring.configurator.WebAppContextUtils;
+import org.apache.excalibur.source.SourceNotFoundException;
+import org.apache.excalibur.source.SourceResolver;
+import org.apache.lenya.cms.cocoon.source.SourceUtil;
+import org.apache.xpath.XPathAPI;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
+
+/**
+ * Servlet utility class
+ *
+ * TODO : remove the lenya-core-api/o.a.l.util.ServletHelper
+ */
+public final class ServletHelper {
+
+ /**
+ * Ctor.
+ */
+ private ServletHelper() {
+ // do nothing
+ }
+
+ public static HttpServletRequest getRequest(){
+ HttpServletRequest request = null;
+
+ RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
+ if (requestAttributes instanceof ServletRequestAttributes) {
+ request = ((ServletRequestAttributes)requestAttributes).getRequest();
+ }
+ return request;
+ }
+ /**
+ * Return the webapp url (with a / at the beginning)
+ */
+ public static String getCurrentURI(){
+ String currentURI = "";
+ HttpServletRequest request = getRequest();
+ currentURI = request.getRequestURI(); // ==> /default/authoring/
+ return currentURI;
+ }
+
+ /**
+ * Converts the request parameters to a map. If a key is mapped to multiple parameters, a string
+ * array is used as the value.
+ * @param request The request.
+ * @return A map.
+ *
+ * @TODO : write a test and see if very useful (code duplication ?)
+ */
+ public static Map getParameterMap() {
+ HttpServletRequest request = getRequest();
+ Map requestParameters = new HashMap();
+ for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
+ String key = (String) e.nextElement();
+ String[] values = request.getParameterValues(key);
+ Object value;
+ if (values.length == 1) {
+ value = values[0];
+ } else {
+ value = values;
+ }
+ requestParameters.put(key, value);
+ }
+ return requestParameters;
+ }
+
+ private static Boolean uploadEnabled = null;
+
+ /**
+ * Returns the value of enable-uploads in web.xml
+ * @return true if enable upload is true or not set in web.xml, else false
+ */
+ public static synchronized boolean isUploadEnabled() throws SourceNotFoundException,
+ ParserConfigurationException, SAXException, IOException, TransformerException {
+
+ if (ServletHelper.uploadEnabled == null) {
+
+ Node node;
+ String webappUrl = "context://WEB-INF/web.xml";
+ SourceResolver resolver = (SourceResolver) WebAppContextUtils
+ .getCurrentWebApplicationContext().getBean(SourceResolver.ROLE);
+ Document document = SourceUtil.readDOM(webappUrl, resolver);
+ Element root = document.getDocumentElement();
+ node = XPathAPI.selectSingleNode(root,
+ "/web-app/servlet/init-param[param-name='enable-uploads']/param-value/text()");
+
+ if (node == null) {
+ ServletHelper.uploadEnabled = Boolean.FALSE;
+ } else {
+ boolean enabled = node.getNodeValue().equals("true");
+ ServletHelper.uploadEnabled = Boolean.valueOf(enabled);
+ }
+ }
+ return ServletHelper.uploadEnabled.booleanValue();
+ }
+}
Added: lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/SourceUtil.java
URL: http://svn.apache.org/viewvc/lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/SourceUtil.java?rev=1033162&view=auto
==============================================================================
--- lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/SourceUtil.java (added)
+++ lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/SourceUtil.java Tue Nov 9 19:10:26 2010
@@ -0,0 +1,650 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.lenya.utils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.MalformedURLException;
+import java.util.Iterator;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.output.ByteArrayOutputStream;
+import org.apache.excalibur.source.ModifiableSource;
+import org.apache.excalibur.source.ModifiableTraversableSource;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceException;
+import org.apache.excalibur.source.SourceNotFoundException;
+import org.apache.excalibur.source.SourceResolver;
+import org.apache.lenya.utils.xml.DocumentHelper;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+/**
+ *
+ */
+public final class SourceUtil {
+
+ /**
+ * <p>
+ * Copies one Source to another using a source buffer i.e. the source Source is buffered before
+ * it is copied to its final destination.
+ * </p>
+ * <p>
+ * The optional buffering is sometimes useful, if the source Source somehow depends on the
+ * destination Source. This situation may occur e.g. if source Source is a Cocoon pipeline.
+ * </p>
+ * <p>
+ * <em>NOTE:</em> o.a.e..s.SourceUtil.copy does not close streams on an exception!!
+ * </p>
+ * @param source
+ * @param destination
+ * @param useBuffer If true, the source data will be read into a buffer before it is written to
+ * the final destination.
+ * @throws IOException If an error occures.
+ */
+ public static void copy(Source source, ModifiableSource destination, boolean useBuffer)
+ throws IOException {
+ InputStream sourceInputStream = null;
+ OutputStream destOutputStream = null;
+ try {
+ sourceInputStream = source.getInputStream();
+ destOutputStream = destination.getOutputStream();
+
+ if (useBuffer) {
+ final ByteArrayOutputStream sourceBos = new ByteArrayOutputStream();
+ IOUtils.copy(sourceInputStream, sourceBos);
+ IOUtils.write(sourceBos.toByteArray(), destOutputStream);
+ } else {
+ IOUtils.copy(sourceInputStream, destOutputStream);
+ }
+ } finally {
+ if (destOutputStream != null) {
+ destOutputStream.flush();
+ destOutputStream.close();
+ }
+ if (sourceInputStream != null) {
+ sourceInputStream.close();
+ }
+ }
+ }
+
+ /**
+ * Copies one Source to another. The source Source is optionally buffered.
+ * @param resolver The SourceResolver to use for lookin up Sources.
+ * @param sourceUri The source to be copied.
+ * @param destUri The URI to copy to.
+ * @param useBuffer If true, the source Source is buffered before copied to the final
+ * destination.
+ * @throws IOException If an error occures.
+ * @throws SourceException If the destination is not modifiable.
+ * @see #copy(Source, ModifiableSource, boolean)
+ */
+ public static void copy(SourceResolver resolver, String sourceUri, String destUri,
+ boolean useBuffer) throws IOException, SourceException {
+ Source source = null;
+ Source dest = null;
+ try {
+ source = resolver.resolveURI(sourceUri);
+ dest = resolver.resolveURI(destUri);
+
+ if (!(dest instanceof ModifiableSource))
+ throw new SourceException("Destination '" + dest.getURI() + "' is not modifiable.");
+
+ copy(source, (ModifiableSource) dest, useBuffer);
+ } finally {
+ if (source != null)
+ resolver.release(source);
+ if (dest != null)
+ resolver.release(dest);
+ }
+ }
+
+ /**
+ * Copies a Source without buffering.
+ * @param resolver A SourceResolver instance.
+ * @param sourceUri The source URI to copy from.
+ * @param destUri The destination URI to copy to.
+ * @throws IOException If an error occures.
+ * @see #copy(SourceResolver, String, String, boolean)
+ */
+ public static void copy(SourceResolver resolver, String sourceUri, String destUri)
+ throws IOException {
+ copy(resolver, sourceUri, destUri, false);
+ }
+
+ /**
+ * Copies a Source without buffering.
+ * @param manager A service manager.
+ * @param sourceUri The source URI to copy from.
+ * @param destUri The destination URI to copy to.
+ * @throws IOException If an error occures.
+ * @throws ServiceException
+ * @see #copy(SourceResolver, String, String, boolean)
+ */
+ public static void copy(ServiceManager manager, String sourceUri, String destUri)
+ throws IOException, ServiceException {
+ SourceResolver resolver = null;
+ try {
+ resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
+ copy(resolver, sourceUri, destUri, false);
+ } finally {
+ if (resolver != null) {
+ manager.release(resolver);
+ }
+ }
+ }
+
+ /**
+ * Reads a DOM from a source.
+ * @param sourceUri The source URI.
+ * @param manager The service manager.
+ * @return A document or <code>null</code> if the source does not exist.
+ * @throws ServiceException if an error occurs.
+ * @throws SourceNotFoundException if an error occurs.
+ * @throws ParserConfigurationException if an error occurs.
+ * @throws SAXException if an error occurs.
+ * @throws IOException if an error occurs.
+ * @deprecated
+ */
+ public static Document readDOM(String sourceUri, ServiceManager manager)
+ throws ServiceException, SourceNotFoundException, ParserConfigurationException,
+ SAXException, IOException {
+ SourceResolver resolver = null;
+ Source source = null;
+ Document document = null;
+ try {
+
+ resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
+ source = resolver.resolveURI(sourceUri);
+
+ if (source.exists()) {
+ document = DocumentHelper.readDocument(source.getInputStream());
+ }
+ } finally {
+ if (resolver != null) {
+ if (source != null) {
+ resolver.release(source);
+ }
+ manager.release(resolver);
+ }
+ }
+ return document;
+ }
+
+ /**
+ * Reads a DOM from a source.
+ * @param sourceUri The source URI.
+ * @param manager The service manager.
+ * @return A document or <code>null</code> if the source does not exist.
+ * @throws SourceNotFoundException if an error occurs.
+ * @throws ParserConfigurationException if an error occurs.
+ * @throws SAXException if an error occurs.
+ * @throws IOException if an error occurs.
+ */
+ public static Document readDOM(String sourceUri, SourceResolver resolver)
+ throws SourceNotFoundException, ParserConfigurationException, SAXException, IOException {
+ Source source = null;
+ Document document = null;
+ try {
+ source = resolver.resolveURI(sourceUri);
+ if (source.exists()) {
+ document = DocumentHelper.readDocument(source.getInputStream());
+ }
+ } finally {
+ if (resolver != null) {
+ if (source != null) {
+ resolver.release(source);
+ }
+ }
+ }
+ return document;
+ }
+
+ /**
+ * Writes a DOM to a source.
+ * @param document The document.
+ * @param sourceUri The source URI.
+ * @param manager The service manager.
+ * @throws TransformerConfigurationException if an error occurs.
+ * @throws TransformerException if an error occurs.
+ * @throws ServiceException if the source resolver could not be obtained.
+ * @throws MalformedURLException if the source URI is not valid.
+ * @throws IOException if an error occurs.
+ * @deprecated
+ */
+ public static void writeDOM(Document document, String sourceUri, ServiceManager manager)
+ throws TransformerConfigurationException, TransformerException, ServiceException,
+ MalformedURLException, IOException {
+ SourceResolver resolver = null;
+ ModifiableSource source = null;
+ try {
+
+ resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
+ source = (ModifiableSource) resolver.resolveURI(sourceUri);
+
+ OutputStream oStream = source.getOutputStream();
+ writeDOM(document, oStream);
+ } finally {
+ if (resolver != null) {
+ if (source != null) {
+ resolver.release(source);
+ }
+ manager.release(resolver);
+ }
+ }
+ }
+
+ /**
+ * Writes a DOM to a source.
+ * @param document The document.
+ * @param sourceUri The source URI.
+ * @throws TransformerConfigurationException if an error occurs.
+ * @throws TransformerException if an error occurs.
+ * @throws ServiceException if the source resolver could not be obtained.
+ * @throws MalformedURLException if the source URI is not valid.
+ * @throws IOException if an error occurs.
+ */
+ public static void writeDOM(Document document, String sourceUri, SourceResolver resolver)
+ throws TransformerConfigurationException, TransformerException, ServiceException,
+ MalformedURLException, IOException {
+ Source source = null;
+ try {
+ source = resolver.resolveURI(sourceUri);
+ if (source instanceof ModifiableSource) {
+ ModifiableSource modifiableSource = (ModifiableSource) source;
+ OutputStream oStream = modifiableSource.getOutputStream();
+ writeDOM(document, oStream);
+ } else {
+ throw new IOException("The source " + sourceUri + " is not modifiable.");
+ }
+ } finally {
+ if (source != null) {
+ resolver.release(source);
+ }
+ }
+ }
+
+ /**
+ * @param document The XML document.
+ * @param oStream The output stream.
+ * @throws TransformerConfigurationException if an error occurs.
+ * @throws TransformerException if an error occurs.
+ * @throws IOException if an error occurs.
+ */
+ public static void writeDOM(Document document, OutputStream oStream)
+ throws TransformerConfigurationException, TransformerException, IOException {
+ DocumentHelper.writeDocument(document, oStream);
+ if (oStream != null) {
+ oStream.flush();
+ try {
+ oStream.close();
+ } catch (Throwable t) {
+ throw new RuntimeException("Could not write document: ", t);
+ }
+ }
+ }
+
+ /**
+ * Deletes a source if it exists.
+ * @param sourceUri The source URI.
+ * @param manager The service manager.
+ * @throws ServiceException if an error occurs.
+ * @throws MalformedURLException if an error occurs.
+ * @throws IOException if an error occurs.
+ * @deprecated
+ */
+ public static void delete(String sourceUri, ServiceManager manager) throws ServiceException,
+ MalformedURLException, IOException {
+ SourceResolver resolver = null;
+ ModifiableTraversableSource source = null;
+ try {
+
+ resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
+ source = (ModifiableTraversableSource) resolver.resolveURI(sourceUri);
+ if (source.exists()) {
+ source.delete();
+ }
+
+ } finally {
+ if (resolver != null) {
+ if (source != null) {
+ resolver.release(source);
+ }
+ manager.release(resolver);
+ }
+ }
+ }
+
+ /**
+ * Deletes a source if it exists.
+ * @param sourceUri The source URI.
+ * @throws ServiceException if an error occurs.
+ * @throws MalformedURLException if an error occurs.
+ * @throws IOException if an error occurs.
+ */
+ public static void delete(String sourceUri, SourceResolver resolver) throws ServiceException,
+ MalformedURLException, IOException {
+ ModifiableTraversableSource source = null;
+ try {
+ source = (ModifiableTraversableSource) resolver.resolveURI(sourceUri);
+ if (source.exists()) {
+ source.delete();
+ }
+
+ } finally {
+ if (resolver != null) {
+ if (source != null) {
+ resolver.release(source);
+ }
+ }
+ }
+ }
+
+ /**
+ * Deletes all empty collections in a subtree.
+ * @param sourceUri The root source URI.
+ * @param manager The service manager.
+ * @throws ServiceException
+ * @throws MalformedURLException
+ * @throws IOException
+ */
+ public static void deleteEmptyCollections(String sourceUri, ServiceManager manager)
+ throws ServiceException, MalformedURLException, IOException {
+ SourceResolver resolver = null;
+ try {
+ resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
+ deleteEmptyCollections(sourceUri, resolver);
+ } finally {
+ if (resolver != null) {
+ manager.release(resolver);
+ }
+ }
+ }
+
+ /**
+ * Deletes all empty collections in a subtree.
+ * @param sourceUri The root source URI.
+ * @throws ServiceException
+ * @throws MalformedURLException
+ * @throws IOException
+ */
+ public static void deleteEmptyCollections(String sourceUri, SourceResolver resolver)
+ throws ServiceException, MalformedURLException, IOException {
+ ModifiableTraversableSource source = null;
+ try {
+ source = (ModifiableTraversableSource) resolver.resolveURI(sourceUri);
+ if (source.isCollection()) {
+ for (Iterator i = source.getChildren().iterator(); i.hasNext();) {
+ ModifiableTraversableSource child = (ModifiableTraversableSource) i.next();
+ deleteEmptyCollections(child.getURI(), resolver);
+ }
+ if (source.getChildren().size() == 0) {
+ source.delete();
+ }
+ }
+ } finally {
+ if (source != null) {
+ resolver.release(source);
+ }
+ }
+ }
+
+ /**
+ * Checks if a source exists.
+ * @param sourceUri The source URI.
+ * @param manager The service manager.
+ * @return A boolean value.
+ * @throws ServiceException if an error occurs.
+ * @throws MalformedURLException if an error occurs.
+ * @throws IOException if an error occurs.
+ * @deprecated
+ */
+ public static boolean exists(String sourceUri, ServiceManager manager) throws ServiceException,
+ MalformedURLException, IOException {
+ SourceResolver resolver = null;
+ Source source = null;
+ try {
+
+ resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
+ source = resolver.resolveURI(sourceUri);
+
+ return source.exists();
+ } finally {
+ if (resolver != null) {
+ if (source != null) {
+ resolver.release(source);
+ }
+ manager.release(resolver);
+ }
+ }
+ }
+
+ /**
+ * Checks if a source exists.
+ * @param sourceUri The source URI.
+ * @return A boolean value.
+ * @throws ServiceException if an error occurs.
+ * @throws MalformedURLException if an error occurs.
+ * @throws IOException if an error occurs.
+ */
+ public static boolean exists(String sourceUri, SourceResolver resolver)
+ throws ServiceException, MalformedURLException, IOException {
+ Source source = null;
+ try {
+ source = resolver.resolveURI(sourceUri);
+ return source.exists();
+ } finally {
+ if (source != null) {
+ resolver.release(source);
+ }
+ }
+ }
+
+ /**
+ * Returns the last modification date of a source.
+ * @param sourceUri The source URI.
+ * @param manager The service manager.
+ * @return A long value.
+ * @throws ServiceException if an error occurs.
+ * @throws MalformedURLException if an error occurs.
+ * @throws IOException if an error occurs.
+ * @deprecated
+ */
+ public static long getLastModified(String sourceUri, ServiceManager manager)
+ throws ServiceException, MalformedURLException, IOException {
+ SourceResolver resolver = null;
+ Source source = null;
+ try {
+
+ resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
+ source = resolver.resolveURI(sourceUri);
+
+ return source.getLastModified();
+ } finally {
+ if (resolver != null) {
+ if (source != null) {
+ resolver.release(source);
+ }
+ manager.release(resolver);
+ }
+ }
+ }
+
+ /**
+ * Returns the last modification date of a source.
+ * @param sourceUri The source URI.
+ * @param manager The service manager.
+ * @return A long value.
+ * @throws ServiceException if an error occurs.
+ * @throws MalformedURLException if an error occurs.
+ * @throws IOException if an error occurs.
+ */
+ public static long getLastModified(String sourceUri, SourceResolver resolver)
+ throws ServiceException, MalformedURLException, IOException {
+ Source source = null;
+ try {
+ source = resolver.resolveURI(sourceUri);
+ return source.getLastModified();
+ } finally {
+ if (source != null) {
+ resolver.release(source);
+ }
+ }
+ }
+
+ /**
+ * @param sourceUri The source URI.
+ * @param manager The service manager.
+ * @return A content length.
+ * @throws ServiceException
+ * @throws MalformedURLException
+ * @throws IOException
+ * @deprecated
+ */
+ public static long getContentLength(String sourceUri, ServiceManager manager)
+ throws ServiceException, MalformedURLException, IOException {
+ SourceResolver resolver = null;
+ Source source = null;
+ try {
+ resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
+ source = resolver.resolveURI(sourceUri);
+ return source.getContentLength();
+ } finally {
+ if (resolver != null) {
+ if (source != null) {
+ resolver.release(source);
+ }
+ manager.release(resolver);
+ }
+ }
+ }
+
+ /**
+ * @param sourceUri The source URI.
+ * @return A content length.
+ * @throws ServiceException
+ * @throws MalformedURLException
+ * @throws IOException
+ */
+ public static long getContentLength(String sourceUri, SourceResolver resolver)
+ throws ServiceException, MalformedURLException, IOException {
+ Source source = null;
+ try {
+ source = resolver.resolveURI(sourceUri);
+ return source.getContentLength();
+ } finally {
+ if (source != null) {
+ resolver.release(source);
+ }
+ }
+ }
+
+ /**
+ * @param sourceUri The source URI.
+ * @return A mime type.
+ * @throws ServiceException
+ * @throws IOException
+ * @throws MalformedURLException
+ */
+ public static String getMimeType(String sourceUri, SourceResolver resolver)
+ throws ServiceException, MalformedURLException, IOException {
+ Source source = null;
+ try {
+ source = resolver.resolveURI(sourceUri);
+ return source.getMimeType();
+ } finally {
+ if (source != null) {
+ resolver.release(source);
+ }
+ }
+ }
+
+ /**
+ * @param sourceUri The source URI.
+ * @param manager The service manager.
+ * @return A mime type.
+ * @throws ServiceException
+ * @throws IOException
+ * @throws MalformedURLException
+ * @deprecated
+ */
+ public static String getMimeType(String sourceUri, ServiceManager manager)
+ throws ServiceException, MalformedURLException, IOException {
+ SourceResolver resolver = null;
+ Source source = null;
+ try {
+
+ resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
+ source = resolver.resolveURI(sourceUri);
+
+ return source.getMimeType();
+ } finally {
+ if (resolver != null) {
+ if (source != null) {
+ resolver.release(source);
+ }
+ manager.release(resolver);
+ }
+ }
+ }
+
+ /**
+ * @param resolver
+ * @param sourceUri
+ * @param destOutputStream
+ * @throws MalformedURLException
+ * @throws IOException
+ */
+ public static void copy(SourceResolver resolver, String sourceUri, OutputStream destOutputStream)
+ throws MalformedURLException, IOException {
+ boolean useBuffer = true;
+ InputStream sourceInputStream = null;
+ Source source = null;
+ try {
+ source = resolver.resolveURI(sourceUri);
+ sourceInputStream = source.getInputStream();
+
+ if (useBuffer) {
+ final ByteArrayOutputStream sourceBos = new ByteArrayOutputStream();
+ IOUtils.copy(sourceInputStream, sourceBos);
+ IOUtils.write(sourceBos.toByteArray(), destOutputStream);
+ } else {
+ IOUtils.copy(sourceInputStream, destOutputStream);
+ }
+ } finally {
+ if (destOutputStream != null) {
+ destOutputStream.flush();
+ destOutputStream.close();
+ }
+ if (sourceInputStream != null) {
+ sourceInputStream.close();
+ }
+ if (source != null) {
+ resolver.release(source);
+ }
+ }
+ }
+}
\ No newline at end of file
Modified: lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/URLInformation.java
URL: http://svn.apache.org/viewvc/lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/URLInformation.java?rev=1033162&r1=1033161&r2=1033162&view=diff
==============================================================================
--- lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/URLInformation.java (original)
+++ lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/URLInformation.java Tue Nov 9 19:10:26 2010
@@ -18,15 +18,11 @@
package org.apache.lenya.utils;
-import javax.servlet.http.HttpServletRequest;
-
import org.apache.lenya.cms.publication.Publication;
-import org.springframework.web.context.request.RequestAttributes;
-import org.springframework.web.context.request.RequestContextHolder;
-import org.springframework.web.context.request.ServletRequestAttributes;
+
/**
- * This class resolves all Lenya-specific information from a webapp URL.
+ * This class provides all Lenya-specific information from a webapp URL.
*/
public class URLInformation {
@@ -40,19 +36,11 @@ public class URLInformation {
private String webappUrl = null;
public URLInformation(){
- webappUrl = getCurrentURI();
+ webappUrl = ServletHelper.getCurrentURI();
setSourceURL(webappUrl);
}
- private String getCurrentURI(){
- String currentURI = "";
- RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
- if (requestAttributes instanceof ServletRequestAttributes) {
- HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
- currentURI = request.getRequestURI(); // ==> /default/authoring/
- }
- return currentURI;
- }
+
private void setSourceURL(String webappUrl){
if (!webappUrl.startsWith("/")) {
@@ -63,10 +51,10 @@ public class URLInformation {
/**
- * Return the webapp url (with a / at the beginning
+ * Return the webapp url (with a / at the beginning)
*/
public String getWebappUrl(){
- return this.webappUrl;
+ return webappUrl;
}
/**
* Returns the publication ID.
Added: lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/test/SpringEnv.java
URL: http://svn.apache.org/viewvc/lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/test/SpringEnv.java?rev=1033162&view=auto
==============================================================================
--- lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/test/SpringEnv.java (added)
+++ lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/test/SpringEnv.java Tue Nov 9 19:10:26 2010
@@ -0,0 +1,20 @@
+package org.apache.lenya.utils.test;
+
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpSession;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+public class SpringEnv {
+
+ public static void setMockRequestContextHolder(String mockURL){
+
+ MockHttpServletRequest request = new MockHttpServletRequest("GET", mockURL);
+ MockHttpSession session = new MockHttpSession();
+ request.setSession(session);
+ RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
+
+
+ }
+
+}
Added: lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/xml/DocumentHelper.java
URL: http://svn.apache.org/viewvc/lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/xml/DocumentHelper.java?rev=1033162&view=auto
==============================================================================
--- lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/xml/DocumentHelper.java (added)
+++ lenya/trunk/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/xml/DocumentHelper.java Tue Nov 9 19:10:26 2010
@@ -0,0 +1,497 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.lenya.utils.xml;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Writer;
+import java.net.URI;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.xml.resolver.tools.CatalogResolver;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.SAXException;
+
+/**
+ * Various utility methods to work with JAXP.
+ * @version $Id: DocumentHelper.java 633538 2008-03-04 16:43:28Z andreas $
+ *
+ * TODO : move in a more suitable class all xpath related functions
+ * TODO : remove all deprecated funcs.
+ */
+public class DocumentHelper {
+
+ private static EntityResolver entityResolver = null;
+
+ /**
+ * @param resolver The entity resolver to use.
+ */
+ public static void setEntityResolver(EntityResolver resolver) {
+ entityResolver = resolver;
+ }
+
+ /**
+ * Creates a non-validating and namespace-aware DocumentBuilder.
+ * @return A new DocumentBuilder object.
+ * @throws ParserConfigurationException if an error occurs
+ */
+ public static DocumentBuilder createBuilder() throws ParserConfigurationException {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setNamespaceAware(true);
+ DocumentBuilder builder = factory.newDocumentBuilder();
+
+ EntityResolver resolver = entityResolver != null ? entityResolver : new CatalogResolver();
+ builder.setEntityResolver(resolver);
+ return builder;
+ }
+
+ /**
+ * Creates a document. A xmlns:prefix="namespaceUri" attribute is added to
+ * the document element.
+ * @param namespaceUri The namespace URL of the root element.
+ * @param qualifiedName The qualified name of the root element.
+ * @param documentType The type of document to be created or null. When
+ * doctype is not null, its Node.ownerDocument attribute is set
+ * to the document being created.
+ * @return A new Document object.
+ * @throws DOMException if an error occurs
+ * @throws ParserConfigurationException if an error occurs
+ * @see org.w3c.dom.DOMImplementation#createDocument(String, String,
+ * DocumentType)
+ */
+ public static Document createDocument(String namespaceUri, String qualifiedName,
+ DocumentType documentType) throws DOMException, ParserConfigurationException {
+ DocumentBuilder builder = createBuilder();
+ Document document = builder.getDOMImplementation().createDocument(namespaceUri,
+ qualifiedName,
+ documentType);
+
+ // add xmlns:prefix attribute
+ String name = "xmlns";
+ int index = qualifiedName.indexOf(":");
+
+ if (index > -1) {
+ name += (":" + qualifiedName.substring(0, index));
+ }
+
+ document.getDocumentElement().setAttributeNS("http://www.w3.org/2000/xmlns/",
+ name,
+ namespaceUri);
+
+ return document;
+ }
+
+ /**
+ * Reads a document from a file.
+ * @return A document.
+ * @param file The file to load the document from.
+ * @throws ParserConfigurationException if an error occurs
+ * @throws SAXException if an error occurs
+ * @throws IOException if an error occurs
+ */
+ public static Document readDocument(File file) throws ParserConfigurationException,
+ SAXException, IOException {
+ DocumentBuilder builder = createBuilder();
+ return builder.parse(file);
+ }
+
+ /**
+ * Reads a document from a URL.
+ * @return A document.
+ * @param url The URL to load the document from.
+ * @throws ParserConfigurationException if an error occurs
+ * @throws SAXException if an error occurs
+ * @throws IOException if an error occurs
+ */
+ public static Document readDocument(URL url) throws ParserConfigurationException, SAXException,
+ IOException {
+ DocumentBuilder builder = createBuilder();
+ return builder.parse(url.toString());
+ }
+
+ /**
+ * Reads a document from a URI.
+ * @return A document.
+ * @param uri The URI to load the document from.
+ * @throws ParserConfigurationException if an error occurs
+ * @throws SAXException if an error occurs
+ * @throws IOException if an error occurs
+ */
+ public static Document readDocument(URI uri) throws ParserConfigurationException, SAXException,
+ IOException {
+ DocumentBuilder builder = createBuilder();
+ return builder.parse(uri.toString());
+ }
+
+ /**
+ * Reads a document from a string.
+ * @return A document.
+ * @param string The string to load the document from.
+ * @param encoding The encoding which is used by the string.
+ * @throws ParserConfigurationException if an error occurs
+ * @throws SAXException if an error occurs
+ * @throws IOException if an error occurs
+ */
+ public static Document readDocument(String string, String encoding) throws ParserConfigurationException,
+ SAXException, IOException {
+ DocumentBuilder builder = createBuilder();
+ byte bytes[] = string.getBytes(encoding);
+ ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
+ return builder.parse(stream);
+ }
+
+ /**
+ * Reads a document from an input stream.
+ * @return A document.
+ * @param stream The input stream to load the document from.
+ * @throws ParserConfigurationException if an error occurs
+ * @throws SAXException if an error occurs
+ * @throws IOException if an error occurs
+ */
+ public static Document readDocument(InputStream stream) throws ParserConfigurationException,
+ SAXException, IOException {
+ DocumentBuilder builder = createBuilder();
+ return builder.parse(stream);
+ }
+
+ /**
+ * Writes a document to a file. A new file is created if it does not exist.
+ * @param document The document to save.
+ * @param file The file to save the document to.
+ * @throws IOException if an error occurs
+ * @throws TransformerConfigurationException if an error occurs
+ * @throws TransformerException if an error occurs
+ */
+ public static void writeDocument(Document document, File file)
+ throws TransformerConfigurationException, TransformerException, IOException {
+ // sanity checks
+ if (document == null)
+ throw new IllegalArgumentException("illegal usage, parameter document may not be null");
+ if (file == null)
+ throw new IllegalArgumentException("illegal usage, parameter file may not be null");
+
+ file.getParentFile().mkdirs();
+ file.createNewFile();
+
+ DOMSource source = new DOMSource(document);
+ StreamResult result = new StreamResult(file);
+ getTransformer(document.getDoctype()).transform(source, result);
+ }
+
+ /**
+ * Writes a document to a writer.
+ * @param document The document to write.
+ * @param writer The writer to write the document to.
+ * @throws TransformerConfigurationException if an error occurs
+ * @throws TransformerException if an error occurs
+ */
+ public static void writeDocument(Document document, Writer writer)
+ throws TransformerConfigurationException, TransformerException {
+
+ // sanity checks
+ if (document == null)
+ throw new IllegalArgumentException("illegal usage of DocumentHelper::writeDocument(), parameter document may not be null");
+
+ DOMSource source = new DOMSource(document);
+ StreamResult result = new StreamResult(writer);
+ getTransformer(document.getDoctype()).transform(source, result);
+ }
+
+ /**
+ * Writes a document to an output stream.
+ * @param document The document to write.
+ * @param outputStream The stream to write the document to.
+ * @throws TransformerConfigurationException if an error occurs
+ * @throws TransformerException if an error occurs
+ */
+ public static void writeDocument(Document document, OutputStream outputStream)
+ throws TransformerConfigurationException, TransformerException {
+
+ // sanity checks
+ if (document == null)
+ throw new IllegalArgumentException("illegal usage of DocumentHelper::writeDocument(), parameter document may not be null");
+
+ DOMSource source = new DOMSource(document);
+ StreamResult result = new StreamResult(outputStream);
+ try {
+ getTransformer(document.getDoctype()).transform(source, result);
+ }
+ finally {
+ try {
+ if (outputStream != null) {
+ outputStream.close();
+ }
+ }
+ catch (Exception ignore) {
+
+ }
+ }
+ }
+
+ /**
+ * Get the transformer.
+ * @param documentType the document type
+ * @return a transformer
+ * @throws TransformerConfigurationException if an error occurs
+ */
+ protected static Transformer getTransformer(DocumentType documentType)
+ throws TransformerConfigurationException {
+ TransformerFactory factory = TransformerFactory.newInstance();
+ Transformer transformer = factory.newTransformer();
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+ transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+
+ if (documentType != null) {
+ if (documentType.getPublicId() != null)
+ transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, documentType.getPublicId());
+ if (documentType.getSystemId() != null)
+ transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, documentType.getSystemId());
+ }
+
+ return transformer;
+ }
+
+ /**
+ * Creates a document type.
+ * @param qualifiedName The qualified name of the document type.
+ * @param publicId The public identifier.
+ * @param systemId The system identifier.
+ * @return the document type
+ * @throws ParserConfigurationException if an error occurs
+ * @see org.w3c.dom.DOMImplementation#createDocumentType(java.lang.String,
+ * java.lang.String, java.lang.String)
+ */
+ public DocumentType createDocumentType(String qualifiedName, String publicId, String systemId)
+ throws ParserConfigurationException {
+ DocumentBuilder builder = createBuilder();
+
+ return builder.getDOMImplementation().createDocumentType(qualifiedName, publicId, systemId);
+ }
+
+ /**
+ * Returns the first child element of an element that belong to a certain
+ * namespace or <code>null</code> if none exists.
+ * @param element The parent element.
+ * @param namespaceUri The namespace that the childen must belong to.
+ * @return The first child element or <code>null</code> if none exists.
+ */
+ public static Element getFirstChild(Element element, String namespaceUri) {
+ return getFirstChild(element, namespaceUri, "*");
+ }
+
+ /**
+ * Returns the first child element of an element that belongs to a certain
+ * namespace and has a certain local name or <code>null</code> if none
+ * exists.
+ * @param element The parent element.
+ * @param namespaceUri The namespace that the childen must belong to.
+ * @param localName The local name of the children.
+ * @return The child element or <code>null</code> if none exists.
+ */
+ public static Element getFirstChild(Element element, String namespaceUri, String localName) {
+ Element[] children = getChildren(element, namespaceUri, localName);
+
+ if (children.length > 0) {
+ return children[0];
+ }
+ return null;
+ }
+
+ /**
+ * Returns all child elements of an element, regardless of the namespace.
+ * @param element The parent element.
+ * @return The child elements.
+ */
+ public static Element[] getChildren(Element element) {
+ List childElements = new ArrayList();
+ NodeList children = element.getElementsByTagName("*");
+
+ for (int i = 0; i < children.getLength(); i++) {
+ if (children.item(i).getParentNode() == element) {
+ childElements.add(children.item(i));
+ }
+ }
+
+ return (Element[]) childElements.toArray(new Element[childElements.size()]);
+ }
+
+ /**
+ * Returns all child elements of an element that belong to a certain
+ * namespace.
+ * @param element The parent element.
+ * @param namespaceUri The namespace that the childen must belong to.
+ * @return The child elements.
+ */
+ public static Element[] getChildren(Element element, String namespaceUri) {
+ return getChildren(element, namespaceUri, "*");
+ }
+
+ /**
+ * Returns all child elements of an element that belong to a certain
+ * namespace and have a certain local name.
+ * @param element The parent element.
+ * @param namespaceUri The namespace that the childen must belong to.
+ * @param localName The local name of the children.
+ * @return The child elements.
+ */
+ public static Element[] getChildren(Element element, String namespaceUri, String localName) {
+ List childElements = new ArrayList();
+ NodeList children = element.getElementsByTagNameNS(namespaceUri, localName);
+
+ for (int i = 0; i < children.getLength(); i++) {
+ if (children.item(i).getParentNode() == element) {
+ childElements.add(children.item(i));
+ }
+ }
+
+ return (Element[]) childElements.toArray(new Element[childElements.size()]);
+ }
+
+ /**
+ * Returns the text inside an element. Only the child text nodes of this
+ * element are collected.
+ * @param element The element.
+ * @return The text inside the element.
+ */
+ public static String getSimpleElementText(Element element) {
+ StringBuffer buffer = new StringBuffer();
+ NodeList children = element.getChildNodes();
+
+ for (int i = 0; i < children.getLength(); i++) {
+ Node child = children.item(i);
+
+ if (child instanceof Text) {
+ buffer.append(child.getNodeValue());
+ }
+ }
+
+ return buffer.toString();
+ }
+
+ /**
+ * Replaces all child nodes of an element by a single text node.
+ * @param element The element.
+ * @param text The text to insert.
+ */
+ public static void setSimpleElementText(Element element, String text) {
+ NodeList children = element.getChildNodes();
+
+ for (int i = 0; i < children.getLength(); i++) {
+ Node child = children.item(i);
+ element.removeChild(child);
+ }
+
+ Node textNode = element.getOwnerDocument().createTextNode(text);
+ element.appendChild(textNode);
+ }
+
+ /**
+ * Returns all following sibling elements of an element that belong to a
+ * certain namespace.
+ * @param element The parent element.
+ * @param namespaceUri The namespace that the childen must belong to.
+ * @return The following sibling elements.
+ */
+ public static Element[] getNextSiblings(Element element, String namespaceUri) {
+ return getNextSiblings(element, namespaceUri, "*");
+ }
+
+ /**
+ * Returns all following sibling elements of an element that belong to a
+ * certain namespace. and have a certain local name.
+ * @param element The parent element.
+ * @param namespaceUri The namespace that the childen must belong to.
+ * @param localName The local name of the children.
+ * @return The following sibling elements.
+ */
+ public static Element[] getNextSiblings(Element element, String namespaceUri, String localName) {
+ List childElements = new ArrayList();
+ Element parent = (Element) element.getParentNode();
+ Element[] children = getChildren(parent, namespaceUri, localName);
+
+ int l = children.length;
+ for (int i = 0; i < children.length; i++) {
+ if (children[i] == element) {
+ l = i;
+ }
+ if (i > l) {
+ childElements.add(children[i]);
+ }
+ }
+
+ return (Element[]) childElements.toArray(new Element[childElements.size()]);
+ }
+
+ /**
+ * Returns all preceding sibling elements of an element that belong to a
+ * certain namespace.
+ * @param element The parent element.
+ * @param namespaceUri The namespace that the childen must belong to.
+ * @return The preceding sibling elements.
+ */
+ public static Element[] getPrecedingSiblings(Element element, String namespaceUri) {
+ return getPrecedingSiblings(element, namespaceUri, "*");
+ }
+
+ /**
+ * Returns all preceding sibling elements of an element that belong to a
+ * certain namespace. and have a certain local name.
+ * @param element The parent element.
+ * @param namespaceUri The namespace that the childen must belong to.
+ * @param localName The local name of the children.
+ * @return The preceding sibling elements.
+ */
+ public static Element[] getPrecedingSiblings(Element element, String namespaceUri,
+ String localName) {
+ List childElements = new ArrayList();
+ Element parent = (Element) element.getParentNode();
+ Element[] children = getChildren(parent, namespaceUri, localName);
+
+ int i = 0;
+ while (children[i] != element && i < children.length) {
+ childElements.add(children[i]);
+ i++;
+ }
+
+ return (Element[]) childElements.toArray(new Element[childElements.size()]);
+ }
+}
Modified: lenya/trunk/org.apache.lenya.core.utils/src/test/java/org/apache/lenya/utils/URLInformationTest.java
URL: http://svn.apache.org/viewvc/lenya/trunk/org.apache.lenya.core.utils/src/test/java/org/apache/lenya/utils/URLInformationTest.java?rev=1033162&r1=1033161&r2=1033162&view=diff
==============================================================================
--- lenya/trunk/org.apache.lenya.core.utils/src/test/java/org/apache/lenya/utils/URLInformationTest.java (original)
+++ lenya/trunk/org.apache.lenya.core.utils/src/test/java/org/apache/lenya/utils/URLInformationTest.java Tue Nov 9 19:10:26 2010
@@ -2,6 +2,7 @@ package org.apache.lenya.utils;
import static org.junit.Assert.assertEquals;
+import org.apache.lenya.utils.test.SpringEnv;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
@@ -11,22 +12,18 @@ import org.springframework.web.context.r
public class URLInformationTest {
- String testUrl = "/test/authoring/DocumentTest.html";
+ String mockURL = "/test/authoring/DocumentTest.html";
@Before
public void setMockURL(){
- String mockURL = testUrl;
- MockHttpServletRequest request = new MockHttpServletRequest("GET", mockURL);
- MockHttpSession session = new MockHttpSession();
- request.setSession(session);
- RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
+ SpringEnv.setMockRequestContextHolder(mockURL);
}
@Test
public void getWebappUrl(){
URLInformation urli = new URLInformation();
String pubId = urli.getWebappUrl();
- assertEquals(pubId,testUrl);
+ assertEquals(pubId,mockURL);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@lenya.apache.org
For additional commands, e-mail: commits-help@lenya.apache.org