Shouldn't we have only one component which do the serialize/unserialize to make sure the same rule is applied in the two way ? It seems weird to me to be able to change the way to parse a document reference and let the document reference creation to another implementation. On Fri, Apr 3, 2009 at 22:48, vmassol <[email protected]> wrote:
Author: vmassol Date: 2009-04-03 22:48:34 +0200 (Fri, 03 Apr 2009) New Revision: 18236
Added: platform/core/branches/xwiki-core-1.8/xwiki-bridge/src/main/java/org/xwiki/bridge/DocumentNameFactory.java platform/core/branches/xwiki-core-1.8/xwiki-bridge/src/main/java/org/xwiki/bridge/DocumentNameSerializer.java platform/core/branches/xwiki-core-1.8/xwiki-core/src/main/java/com/xpn/xwiki/doc/DefaultDocumentNameFactory.java platform/core/branches/xwiki-core-1.8/xwiki-core/src/main/java/com/xpn/xwiki/doc/DefaultDocumentNameSerializer.java platform/core/branches/xwiki-core-1.8/xwiki-core/src/test/java/com/xpn/xwiki/doc/DefaultDocumentNameFactoryTest.java platform/core/branches/xwiki-core-1.8/xwiki-core/src/test/java/com/xpn/xwiki/doc/DefaultDocumentNameSerializerTest.java Modified: platform/core/branches/xwiki-core-1.8/xwiki-core/src/main/resources/META-INF/plexus/components.xml Log: XWIKI-3501: Add Document Name factory and serializer
Merged from trunk (rev 18235)
Added: platform/core/branches/xwiki-core-1.8/xwiki-bridge/src/main/java/org/xwiki/bridge/DocumentNameFactory.java =================================================================== --- platform/core/branches/xwiki-core-1.8/xwiki-bridge/src/main/java/org/xwiki/bridge/DocumentNameFactory.java (rev 0) +++ platform/core/branches/xwiki-core-1.8/xwiki-bridge/src/main/java/org/xwiki/bridge/DocumentNameFactory.java 2009-04-03 20:48:34 UTC (rev 18236) @@ -0,0 +1,41 @@ +/* + * 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.bridge; + +/** + * Generate a Document Name from a raw string reference. + * + * @version $Id: $ + * @since 1.8.1 + */ +public interface DocumentNameFactory +{ + /** + * Role for looking up implementing components. + */ + String ROLE = DocumentNameFactory.class.getName(); + + /** + * @param reference the document's name as a string using a textual format (eg {@code wiki:space.page}). + * The supported format is up to implementers of this method. + * @return the object representing a document reference + */ + DocumentName createDocumentName(String reference); +}
Added: platform/core/branches/xwiki-core-1.8/xwiki-bridge/src/main/java/org/xwiki/bridge/DocumentNameSerializer.java =================================================================== --- platform/core/branches/xwiki-core-1.8/xwiki-bridge/src/main/java/org/xwiki/bridge/DocumentNameSerializer.java (rev 0) +++ platform/core/branches/xwiki-core-1.8/xwiki-bridge/src/main/java/org/xwiki/bridge/DocumentNameSerializer.java 2009-04-03 20:48:34 UTC (rev 18236) @@ -0,0 +1,41 @@ +/* + * 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.bridge; + +/** + * Generate a fully qualified document reference string (ie of the form + * {@code wiki:space.page}) out of a {@link DocumentName}. + * + * @version $Id: $ + * @since 1.8.1 + */ +public interface DocumentNameSerializer +{ + /** + * Role for looking up implementing components. + */ + String ROLE = DocumentNameSerializer.class.getName(); + + /** + * @param documentName the document name to serialize + * @return the fully qualified document reference string (ie of the form {@code wiki:space.page}) + */ + String serialize(DocumentName documentName); +}
Added: platform/core/branches/xwiki-core-1.8/xwiki-core/src/main/java/com/xpn/xwiki/doc/DefaultDocumentNameFactory.java =================================================================== --- platform/core/branches/xwiki-core-1.8/xwiki-core/src/main/java/com/xpn/xwiki/doc/DefaultDocumentNameFactory.java (rev 0) +++ platform/core/branches/xwiki-core-1.8/xwiki-core/src/main/java/com/xpn/xwiki/doc/DefaultDocumentNameFactory.java 2009-04-03 20:48:34 UTC (rev 18236) @@ -0,0 +1,163 @@ +/* + * 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 com.xpn.xwiki.doc; + +import org.apache.commons.lang.StringUtils; +import org.xwiki.bridge.DocumentName; +import org.xwiki.bridge.DocumentNameFactory; +import org.xwiki.context.Execution; + +import com.xpn.xwiki.XWikiContext; + +/** + * Generate a Document Name from a raw string reference. + * + * @version $Id: $ + * @since 1.8.1 + */ +public class DefaultDocumentNameFactory implements DocumentNameFactory +{ + /** + * Default space to use when the user has not specified any space and there's no current space set in the context. + */ + private static final String DEFAULT_SPACE = "XWiki"; + + /** + * Default page name when the user has not specified the page name. + */ + private static final String DEFAULT_PAGE = "WebHome"; + + /** + * Default wiki to use when the user has not specified any wiki and there's no current wiki set in the context. + */ + private static final String DEFAULT_WIKI = "xwiki"; + + private static final String WIKI_SEPARATOR = ":"; + + private static final String SPACE_SEPARATOR = "."; + + /** + * Execution context handler, needed for accessing the XWikiContext. + */ + private Execution execution; + + public DocumentName createDocumentName(String reference) + { + String wiki; + String space; + String page; + + if (StringUtils.isBlank(reference)) { + wiki = getDefaultWikiName(); + space = getDefaultSpaceName(); + page = DEFAULT_PAGE; + } else { + + // Step 1: Extract the wiki name + + // We allow the wiki separator in wiki names and thus we look for the last wiki sep in the reference. + // TODO: Note that this was done to have the same behavior of XWikiDocument.setFullName() but it would + // seem better to me to allow the wiki sep in space names rather than in wiki names (since wiki + // names are constrained by database schema names). + int spaceSeparatorPosition; + int wikiSeparatorPosition = reference.lastIndexOf(WIKI_SEPARATOR); + if (wikiSeparatorPosition != -1) { + wiki = reference.substring(0, wikiSeparatorPosition); + if (wiki.length() == 0) { + wiki = getDefaultWikiName(); + } + + spaceSeparatorPosition = reference.indexOf(SPACE_SEPARATOR, wikiSeparatorPosition); + } else { + // No wiki separator, use default wiki. + wiki = getDefaultWikiName(); + + // We allow space sep in space names and thus we look for the last space sep in the reference. + // TODO: Note that this was done to have the same behavior of XWikiDocument.setFullName() but it would + // seem better to me to allow space sep in pages names rather than in space names (since users + // want more liberty in page names and usually create pages in existing spaces). + spaceSeparatorPosition = reference.lastIndexOf(SPACE_SEPARATOR); + } + + // Step 2: Extract the space and page names + + if (spaceSeparatorPosition != -1) { + space = reference.substring(wikiSeparatorPosition + WIKI_SEPARATOR.length(), + spaceSeparatorPosition); + if (space.length() == 0) { + space = getDefaultSpaceName(); + } + + // Make sure the space separator is not the last char of the reference + if (spaceSeparatorPosition + SPACE_SEPARATOR.length() < reference.length()) { + page = reference.substring(spaceSeparatorPosition + SPACE_SEPARATOR.length()); + } else { + page = DEFAULT_PAGE; + } + } else { + // No space separator the whole substring is thus the page. + space = getDefaultSpaceName(); + + // Make sure the wiki separator is not the last char of the reference + if (wikiSeparatorPosition == -1 + || wikiSeparatorPosition + WIKI_SEPARATOR.length() < reference.length()) + { + page = reference.substring(wikiSeparatorPosition + WIKI_SEPARATOR.length()); + } else { + page = DEFAULT_PAGE; + } + } + } + + return new DocumentName(wiki, space, page); + } + + private String getDefaultWikiName() + { + String wiki = getContext().getDatabase(); + if (wiki == null) { + wiki = DEFAULT_WIKI; + } + return wiki; + } + + private String getDefaultSpaceName() + { + String space; + XWikiDocument currentDocument = getContext().getDoc(); + if (currentDocument == null) { + space = DEFAULT_SPACE; + } else { + space = currentDocument.getSpace(); + if (space == null) { + space = DEFAULT_SPACE; + } + } + return space; + } + + /** + * @return the XWiki Context used to bridge with the old API + */ + private XWikiContext getContext() + { + return (XWikiContext) this.execution.getContext().getProperty("xwikicontext"); + } +}
Added: platform/core/branches/xwiki-core-1.8/xwiki-core/src/main/java/com/xpn/xwiki/doc/DefaultDocumentNameSerializer.java =================================================================== --- platform/core/branches/xwiki-core-1.8/xwiki-core/src/main/java/com/xpn/xwiki/doc/DefaultDocumentNameSerializer.java (rev 0) +++ platform/core/branches/xwiki-core-1.8/xwiki-core/src/main/java/com/xpn/xwiki/doc/DefaultDocumentNameSerializer.java 2009-04-03 20:48:34 UTC (rev 18236) @@ -0,0 +1,51 @@ +/* + * 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 com.xpn.xwiki.doc; + +import org.xwiki.bridge.DocumentName; +import org.xwiki.bridge.DocumentNameSerializer; + +/** + * Generate a fully qualified document reference string (ie of the form + * {@code <wiki>:<space>.<page>} out of a {@link DocumentName}. + * + * @version $Id: $ + * @since 1.8.1 + */ +public class DefaultDocumentNameSerializer implements DocumentNameSerializer +{ + /** + * {@inheritDoc} + * @see DocumentNameSerializer#serialize(DocumentName) + */ + public String serialize(DocumentName documentName) + { + // A valid DocumentName must not have any null value and thus we don't need to check for nulls here. + // It's the responsibility of creators of DocumentName factories to ensure it's valid. + StringBuffer result = new StringBuffer(); + result.append(documentName.getWiki()); + result.append(':'); + result.append(documentName.getSpace()); + result.append('.'); + result.append(documentName.getPage()); + + return result.toString(); + } +}
Modified: platform/core/branches/xwiki-core-1.8/xwiki-core/src/main/resources/META-INF/plexus/components.xml =================================================================== --- platform/core/branches/xwiki-core-1.8/xwiki-core/src/main/resources/META-INF/plexus/components.xml 2009-04-03 20:46:01 UTC (rev 18235) +++ platform/core/branches/xwiki-core-1.8/xwiki-core/src/main/resources/META-INF/plexus/components.xml 2009-04-03 20:48:34 UTC (rev 18236) @@ -51,7 +51,6 @@ <role-hint>default</role-hint> <implementation>com.xpn.xwiki.doc.DefaultDocumentAccessBridge</implementation> <instantiation-strategy>singleton</instantiation-strategy> - <lifecycle-handler>xwiki</lifecycle-handler> <requirements> <requirement> <role>org.xwiki.context.Execution</role> @@ -59,11 +58,27 @@ </requirements> </component> <component> + <role>org.xwiki.bridge.DocumentNameFactory</role> + <role-hint>default</role-hint> + <implementation>com.xpn.xwiki.doc.DefaultDocumentNameFactory</implementation> + <instantiation-strategy>singleton</instantiation-strategy> + <requirements> + <requirement> + <role>org.xwiki.context.Execution</role> + </requirement> + </requirements> + </component> + <component> + <role>org.xwiki.bridge.DocumentNameSerializer</role> + <role-hint>default</role-hint> + <implementation>com.xpn.xwiki.doc.DefaultDocumentNameSerializer</implementation> + <instantiation-strategy>singleton</instantiation-strategy> + </component> + <component> <role>org.xwiki.bridge.SkinAccessBridge</role> <role-hint>default</role-hint> <implementation>com.xpn.xwiki.DefaultSkinAccessBridge</implementation> <instantiation-strategy>singleton</instantiation-strategy> - <lifecycle-handler>xwiki</lifecycle-handler> <requirements> <requirement> <role>org.xwiki.context.Execution</role>
Added: platform/core/branches/xwiki-core-1.8/xwiki-core/src/test/java/com/xpn/xwiki/doc/DefaultDocumentNameFactoryTest.java =================================================================== --- platform/core/branches/xwiki-core-1.8/xwiki-core/src/test/java/com/xpn/xwiki/doc/DefaultDocumentNameFactoryTest.java (rev 0) +++ platform/core/branches/xwiki-core-1.8/xwiki-core/src/test/java/com/xpn/xwiki/doc/DefaultDocumentNameFactoryTest.java 2009-04-03 20:48:34 UTC (rev 18236) @@ -0,0 +1,119 @@ +/* + * 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 com.xpn.xwiki.doc; + +import org.xwiki.bridge.DocumentName; +import org.xwiki.bridge.DocumentNameFactory; + +import com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase; + +/** + * Unit tests for {@link DocumentNameFactory}. + * + * @version $Id: $ + * @since 1.8.1 + */ +public class DefaultDocumentNameFactoryTest extends AbstractBridgedXWikiComponentTestCase +{ + private DocumentNameFactory factory; + + protected void setUp() throws Exception + { + super.setUp(); + this.factory = (DocumentNameFactory) getComponentManager().lookup(DocumentNameFactory.ROLE); + } + + public void testCreateDocumentNameWhenCurrentDocSet() throws Exception + { + getContext().setDatabase("testwiki"); + XWikiDocument document = new XWikiDocument(); + document.setSpace("testspace"); + getContext().setDoc(document); + verify("testwiki", "testspace"); + } + + public void testCreateDocumentNameWhenNoCurrentDoc() throws Exception + { + verify("xwiki", "XWiki"); + } + + private void verify(String expectedDefaultWiki, String expectedDefaultSpace) + { + DocumentName name = factory.createDocumentName("wiki:space.page"); + assertEquals("wiki", name.getWiki()); + assertEquals("space", name.getSpace()); + assertEquals("page", name.getPage()); + + name = factory.createDocumentName("wiki1:wiki2:page"); + assertEquals("wiki1:wiki2", name.getWiki()); + assertEquals(expectedDefaultSpace, name.getSpace()); + assertEquals("page", name.getPage()); + + name = factory.createDocumentName("wiki:"); + assertEquals("wiki", name.getWiki()); + assertEquals(expectedDefaultSpace, name.getSpace()); + assertEquals("WebHome", name.getPage()); + + name = factory.createDocumentName("wiki1.wiki2:page"); + assertEquals("wiki1.wiki2", name.getWiki()); + assertEquals(expectedDefaultSpace, name.getSpace()); + assertEquals("page", name.getPage()); + + name = factory.createDocumentName("wiki:page"); + assertEquals("wiki", name.getWiki()); + assertEquals(expectedDefaultSpace, name.getSpace()); + assertEquals("page", name.getPage()); + + name = factory.createDocumentName("wiki:space."); + assertEquals("wiki", name.getWiki()); + assertEquals("space", name.getSpace()); + assertEquals("WebHome", name.getPage()); + + name = factory.createDocumentName("space."); + assertEquals(expectedDefaultWiki, name.getWiki()); + assertEquals("space", name.getSpace()); + assertEquals("WebHome", name.getPage()); + + name = factory.createDocumentName("page"); + assertEquals(expectedDefaultWiki, name.getWiki()); + assertEquals(expectedDefaultSpace, name.getSpace()); + assertEquals("page", name.getPage()); + + name = factory.createDocumentName("."); + assertEquals(expectedDefaultWiki, name.getWiki()); + assertEquals(expectedDefaultSpace, name.getSpace()); + assertEquals("WebHome", name.getPage()); + + name = factory.createDocumentName(":"); + assertEquals(expectedDefaultWiki, name.getWiki()); + assertEquals(expectedDefaultSpace, name.getSpace()); + assertEquals("WebHome", name.getPage()); + + name = factory.createDocumentName(null); + assertEquals(expectedDefaultWiki, name.getWiki()); + assertEquals(expectedDefaultSpace, name.getSpace()); + assertEquals("WebHome", name.getPage()); + + name = factory.createDocumentName(""); + assertEquals(expectedDefaultWiki, name.getWiki()); + assertEquals(expectedDefaultSpace, name.getSpace()); + assertEquals("WebHome", name.getPage()); + } +}
Added: platform/core/branches/xwiki-core-1.8/xwiki-core/src/test/java/com/xpn/xwiki/doc/DefaultDocumentNameSerializerTest.java =================================================================== --- platform/core/branches/xwiki-core-1.8/xwiki-core/src/test/java/com/xpn/xwiki/doc/DefaultDocumentNameSerializerTest.java (rev 0) +++ platform/core/branches/xwiki-core-1.8/xwiki-core/src/test/java/com/xpn/xwiki/doc/DefaultDocumentNameSerializerTest.java 2009-04-03 20:48:34 UTC (rev 18236) @@ -0,0 +1,41 @@ +/* + * 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 com.xpn.xwiki.doc; + +import org.xwiki.bridge.DocumentName; +import org.xwiki.bridge.DocumentNameSerializer; +import org.xwiki.test.AbstractXWikiComponentTestCase; + +/** + * Unit tests for {@link DocumentNameSerializer}. + * + * @version $Id: $ + * @since 1.8.1 + */ +public class DefaultDocumentNameSerializerTest extends AbstractXWikiComponentTestCase +{ + public void testSerialize() throws Exception + { + DocumentNameSerializer serializer = + (DocumentNameSerializer) getComponentManager().lookup(DocumentNameSerializer.ROLE); + DocumentName name = new DocumentName("wiki", "space", "page"); + assertEquals("wiki:space.page", serializer.serialize(name)); + } +}
_______________________________________________ notifications mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/notifications
-- Thomas Mortagne