### Eclipse Workspace Patch 1.0 #P xwiki-core-url-default Index: src/main/java/org/xwiki/url/internal/URLXWikiURLSerializer.java =================================================================== --- src/main/java/org/xwiki/url/internal/URLXWikiURLSerializer.java (revision 0) +++ src/main/java/org/xwiki/url/internal/URLXWikiURLSerializer.java (revision 0) @@ -0,0 +1,48 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * 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.xwiki.url.internal; + +import java.net.URL; + +import org.xwiki.url.XWikiURL; +import org.xwiki.url.XWikiURLSerializer; + +/** + * Transforms a XWikiURL instance into a URL object. Note that the serialization performs URL-encoding + * wherever necessary to generate a valid URL (see http://www.ietf.org/rfc/rfc2396.txt). + * + * @version $Id$ + * @since 2.0M1 + */ +public class URLXWikiURLSerializer implements XWikiURLSerializer +{ + /** + * Transforms a XWikiURL instance into a URL object. Note that the serialization performs URL-encoding + * wherever necessary to generate a valid URL (see http://www.ietf.org/rfc/rfc2396.txt). + * + * @param xwikiURL the XWiki URL to transform + * @return the standard URL instance + */ + public T serialize(XWikiURL xwikiURL) + { + throw new RuntimeException("Not implemented yet"); + } +} Property changes on: src/main/java/org/xwiki/url/internal/URLXWikiURLSerializer.java ___________________________________________________________________ Added: svn:eol-style + native Index: src/main/java/org/xwiki/url/internal/RegexXWikiURLFactory.java =================================================================== --- src/main/java/org/xwiki/url/internal/RegexXWikiURLFactory.java (revision 20914) +++ src/main/java/org/xwiki/url/internal/RegexXWikiURLFactory.java (working copy) @@ -25,6 +25,7 @@ import org.xwiki.component.phase.Initializable; import org.xwiki.component.phase.InitializationException; import org.xwiki.url.InvalidURLException; +import org.xwiki.url.XWikiDocumentURL; import org.xwiki.url.XWikiURL; import org.xwiki.url.XWikiURLFactory; @@ -35,11 +36,13 @@ import java.util.regex.Pattern; /** + * Transform a String representation of a URL into a XWiki URL. + * * @todo how do we support removing the view/ part of the url for example? * @todo how do we support hierarchical spaces? */ @Component -public class RegexXWikiURLFactory implements XWikiURLFactory, Initializable +public class RegexXWikiURLFactory implements XWikiURLFactory, Initializable { // TODO: Move to a configuration parameter private String pattern = "(?:http[s]?://([a-zA-Z-]*)[a-zA-Z-.]*[:]?\\d*)?/\\w*/\\w*/(\\w*)/(\\w*)/?(\\w*)\\??(.*)"; @@ -64,9 +67,15 @@ this.regexMappings.put("queryString", "5"); } + /** + * Transform a String representation of a URL into a XWiki URL. + * + * {@inheritDoc} + * @see XWikiURLFactory#createURL(Object) + */ public XWikiURL createURL(String urlAsString) throws InvalidURLException { - XWikiURL url = new XWikiURL(); + XWikiDocumentURL url; // Use a regex to parse the URL into its discrete parts: // ://://// @@ -76,18 +85,18 @@ // Find the wiki part in the URL String wiki = matcher.group(Integer.parseInt((String) this.regexMappings.get("wiki"))); - // Find the action part in the URL - String action = matcher.group(Integer.parseInt((String) this.regexMappings.get("action"))); - url.setAction(action); - // Find the space part in the URL String space = matcher.group(Integer.parseInt((String) this.regexMappings.get("space"))); // Find the document part in the URL String page = matcher.group(Integer.parseInt((String) this.regexMappings.get("page"))); - url.setDocumentName(new DocumentName(wiki, space, page)); + url = new XWikiDocumentURL(new DocumentName(wiki, space, page)); + // Find the action part in the URL + String action = matcher.group(Integer.parseInt((String) this.regexMappings.get("action"))); + url.setAction(action); + // Find the query string if any and transform it into a parameter Map for easy access String queryString = matcher.group(Integer.parseInt((String) this.regexMappings.get("queryString"))); if (queryString != null) { Index: src/test/java/org/xwiki/url/internal/RegexURLFactoryTest.java =================================================================== --- src/test/java/org/xwiki/url/internal/RegexURLFactoryTest.java (revision 20914) +++ src/test/java/org/xwiki/url/internal/RegexURLFactoryTest.java (working copy) @@ -21,6 +21,7 @@ package org.xwiki.url.internal; import org.xwiki.test.AbstractXWikiComponentTestCase; +import org.xwiki.url.XWikiDocumentURL; import org.xwiki.url.XWikiURL; import org.xwiki.url.XWikiURLFactory; @@ -32,17 +33,18 @@ */ public class RegexURLFactoryTest extends AbstractXWikiComponentTestCase { - private XWikiURLFactory factory; + private XWikiURLFactory factory; @Override protected void setUp() throws Exception { - this.factory = (XWikiURLFactory) getComponentManager().lookup(XWikiURLFactory.class); + this.factory = (XWikiURLFactory) getComponentManager().lookup(XWikiURLFactory.class); } public void testCreateURL() throws Exception { - XWikiURL url = this.factory.createURL("http://wiki.domain.com:8080/xwiki/bin/view/Main/WebHome?language=fr"); + XWikiDocumentURL url = (XWikiDocumentURL) this.factory.createURL( + "http://wiki.domain.com:8080/xwiki/bin/view/Main/WebHome?language=fr"); assertEquals("view", url.getAction()); assertEquals("WebHome", url.getDocumentName().getPage()); assertEquals("Main", url.getDocumentName().getSpace()); @@ -52,7 +54,7 @@ public void testCreateURLFromPath() throws Exception { - XWikiURL url = this.factory.createURL("/xwiki/bin/view/Main/WebHome"); + XWikiDocumentURL url = (XWikiDocumentURL) this.factory.createURL("/xwiki/bin/view/Main/WebHome"); assertEquals("view", url.getAction()); assertEquals("WebHome", url.getDocumentName().getPage()); assertEquals("Main", url.getDocumentName().getSpace()); #P xwiki-core-url-api Index: src/main/java/org/xwiki/url/XWikiURLSerializer.java =================================================================== --- src/main/java/org/xwiki/url/XWikiURLSerializer.java (revision 0) +++ src/main/java/org/xwiki/url/XWikiURLSerializer.java (revision 0) @@ -0,0 +1,40 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * 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.xwiki.url; + +/** + * Transforms a XWiki URL instance into some other representation. + * + * @version $Id$ + * @since 2.0M1 + * @param the output type + */ +public interface XWikiURLSerializer +{ + /** + * Transforms a XWiki URL instance into some other representation. + * + * @param the output type + * @param xwikiURL the XWiki URL to transform + * @return the new representation + */ + T serialize(XWikiURL xwikiURL); +} Property changes on: src/main/java/org/xwiki/url/XWikiURLSerializer.java ___________________________________________________________________ Added: svn:eol-style + native Index: src/main/java/org/xwiki/url/XWikiAttachmentURL.java =================================================================== --- src/main/java/org/xwiki/url/XWikiAttachmentURL.java (revision 0) +++ src/main/java/org/xwiki/url/XWikiAttachmentURL.java (revision 0) @@ -0,0 +1,25 @@ +package org.xwiki.url; + +import org.xwiki.bridge.AttachmentName; + +public class XWikiAttachmentURL extends XWikiDocumentURL +{ + private AttachmentName attachmentName; + + public XWikiAttachmentURL(AttachmentName attachmentName) + { + super(attachmentName); + setType(XWikiURLType.ATTACHMENT); + setAttachmentName(attachmentName); + } + + public void setAttachmentName(AttachmentName attachmentName) + { + this.attachmentName = attachmentName; + } + + public AttachmentName getAttachmentName() + { + return this.attachmentName; + } +} Property changes on: src/main/java/org/xwiki/url/XWikiAttachmentURL.java ___________________________________________________________________ Added: svn:eol-style + native Index: src/main/java/org/xwiki/url/XWikiDocumentURL.java =================================================================== --- src/main/java/org/xwiki/url/XWikiDocumentURL.java (revision 0) +++ src/main/java/org/xwiki/url/XWikiDocumentURL.java (revision 0) @@ -0,0 +1,82 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * 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.xwiki.url; + +import java.util.Locale; + +import org.xwiki.bridge.DocumentName; + +public class XWikiDocumentURL extends AbstractXWikiURL +{ + private String action; + + private DocumentName documentName; + + private Locale locale; + + private String revision; + + public XWikiDocumentURL(DocumentName documentName) + { + super(XWikiURLType.DOCUMENT); + setDocumentName(documentName); + } + + public String getAction() + { + return this.action; + } + + public void setAction(String action) + { + this.action = action; + } + + public DocumentName getDocumentName() + { + return this.documentName; + } + + public void setDocumentName(DocumentName documentName) + { + this.documentName = documentName; + } + + public void setLocale(Locale locale) + { + this.locale = locale; + } + + public Locale getLocale() + { + return this.locale; + } + + public void setRevision(String revision) + { + this.revision = revision; + } + + public String getRevision() + { + return this.revision; + } +} Property changes on: src/main/java/org/xwiki/url/XWikiDocumentURL.java ___________________________________________________________________ Added: svn:eol-style + native Index: src/main/java/org/xwiki/url/XWikiURLType.java =================================================================== --- src/main/java/org/xwiki/url/XWikiURLType.java (revision 0) +++ src/main/java/org/xwiki/url/XWikiURLType.java (revision 0) @@ -0,0 +1,6 @@ +package org.xwiki.url; + +public enum XWikiURLType +{ + DOCUMENT, ATTACHMENT, SKIN, TEMPLATE, RESOURCE; +} Property changes on: src/main/java/org/xwiki/url/XWikiURLType.java ___________________________________________________________________ Added: svn:eol-style + native Index: src/main/java/org/xwiki/url/XWikiURL.java =================================================================== --- src/main/java/org/xwiki/url/XWikiURL.java (revision 20914) +++ src/main/java/org/xwiki/url/XWikiURL.java (working copy) @@ -1,76 +1,26 @@ -/* - * See the NOTICE file distributed with this work for additional - * information regarding copyright ownership. - * - * 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.xwiki.url; +import java.util.List; import java.util.Map; -import java.util.HashMap; -import java.util.Collections; -import org.xwiki.bridge.DocumentName; - /** * Represents a XWiki URL. * * @version $Id$ */ -public class XWikiURL +public interface XWikiURL { - private String action; + XWikiURLType getType(); + + void addParameter(String name, String value); - private DocumentName documentName; + Map> getParameters(); - private Map parameters = new HashMap(); - - public String getAction() - { - return this.action; - } - - public void setAction(String action) - { - this.action = action; - } - - public DocumentName getDocumentName() - { - return this.documentName; - } - - public void setDocumentName(DocumentName documentName) - { - this.documentName = documentName; - } - - public void addParameter(String name, String value) - { - this.parameters.put(name, value); - } - - public Map getParameters() - { - return Collections.unmodifiableMap(this.parameters); - } - - public String getParameterValue(String name) - { - return this.parameters.get(name); - } + List getParameterValues(String name); + + /** + * @param name the parameter name for which to return the value + * @return the first parameter value matching the passed parameter name + */ + String getParameterValue(String name); } Index: src/main/java/org/xwiki/url/XWikiURLFactory.java =================================================================== --- src/main/java/org/xwiki/url/XWikiURLFactory.java (revision 20914) +++ src/main/java/org/xwiki/url/XWikiURLFactory.java (working copy) @@ -22,8 +22,21 @@ import org.xwiki.component.annotation.ComponentRole; +/** + * Transforms some representation of a XWiki URL into a {@link XWikiURL} instance. + * + * @version $Id$ + * @param the object to transform into a XWiki URL + */ @ComponentRole -public interface XWikiURLFactory +public interface XWikiURLFactory { - XWikiURL createURL(String urlAsString) throws InvalidURLException; + /** + * Transforms some representation of a XWiki URL into a {@link XWikiURL} instance. + * + * @param urlRepresentation the object to transform into a {@link XWikiURL} instance + * @return the {@link XWikiURL} instance + * @throws InvalidURLException if the input representation doesn't represent a valid XWiki URL + */ + XWikiURL createURL(T urlRepresentation) throws InvalidURLException; } Index: src/main/java/org/xwiki/url/AbstractXWikiURL.java =================================================================== --- src/main/java/org/xwiki/url/AbstractXWikiURL.java (revision 20914) +++ src/main/java/org/xwiki/url/AbstractXWikiURL.java (working copy) @@ -20,57 +20,72 @@ */ package org.xwiki.url; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.HashMap; import java.util.Collections; -import org.xwiki.bridge.DocumentName; - /** - * Represents a XWiki URL. - * * @version $Id$ + * @since 2.0M1 */ -public class XWikiURL +public class AbstractXWikiURL implements XWikiURL { - private String action; + private XWikiURLType type; + + /** +Ž * There can be several values for the same name (since this is allowed in URLs and we want to map a URL to a + * XWiki URL). + */ + private Map> parameters = new HashMap>(); - private DocumentName documentName; - - private Map parameters = new HashMap(); - - public String getAction() + public AbstractXWikiURL(XWikiURLType type) { - return this.action; + setType(type); } - - public void setAction(String action) + + public XWikiURLType getType() { - this.action = action; + return this.type; } - - public DocumentName getDocumentName() + + public void setType(XWikiURLType type) { - return this.documentName; + this.type = type; } - - public void setDocumentName(DocumentName documentName) - { - this.documentName = documentName; - } - + public void addParameter(String name, String value) { - this.parameters.put(name, value); + List list = this.parameters.get(name); + if (list == null) { + list = new ArrayList(); + } + list.add(value); + this.parameters.put(name, list); } - public Map getParameters() + public Map> getParameters() { return Collections.unmodifiableMap(this.parameters); } - public String getParameterValue(String name) + public List getParameterValues(String name) { return this.parameters.get(name); } + + /** + * @param name the parameter name for which to return the value + * @return the first parameter value matching the passed parameter name + */ + public String getParameterValue(String name) + { + String result = null; + List list = this.parameters.get(name); + if (list != null) { + result = list.get(0); + } + return result; + } }