/*
 * 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.model.internal.reference.converter;

import java.lang.reflect.Type;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.commons.lang3.EnumUtils;
import org.xwiki.component.annotation.Component;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.EntityReferenceResolver;
import org.xwiki.model.reference.EntityReferenceSerializer;
import org.xwiki.properties.converter.AbstractConverter;

/**
 * Converter that converts a value into an {@link EntityReference} object. Reference are not resolved to created
 * complete reference but kept exactly as indicated in the value.
 * 
 * @version $Id$
 * @since 5.2RC1
 */
@Component
@Singleton
public class EntityReferenceConverter extends AbstractConverter<EntityReference>
{
    @Inject
    @Named("relative")
    private EntityReferenceResolver<String> stringResolver;

    @Inject
    private EntityReferenceSerializer<String> serialier;

    @Override
    protected EntityReference convertToType(Type type, Object value)
    {
        if (value == null) {
            return null;
        }

        return convertToType(type, value.toString());
    }

    private EntityReference convertToType(Type type, String value)
    {
        EntityType entityType = EntityType.DOCUMENT;
        String entityReference = value;

        int index = value.indexOf(':');
        if (index > 0) {
            String entityTypeString = value.substring(0, index);
            EntityType foundEntityType = EnumUtils.getEnum(EntityType.class, entityTypeString.toUpperCase());
            if (foundEntityType != null) {
                entityType = foundEntityType;
                ++index;
                entityReference = index < value.length() ? value.substring(index) : "";
            }
        }

        return this.stringResolver.resolve(entityReference, entityType);
    }

    @Override
    protected String convertToString(EntityReference value)
    {
        if (value == null) {
            return null;
        }

        return value.getType().getLowerCase() + ':' + this.serialier.serialize(value);
    }
}

@@ -1153,16 +1153,117 @@
 ponent;%0A
+import org.xwiki.component.namespace.Namespace;%0Aimport org.xwiki.component.namespace.NamespaceUtils;%0A
 import o
@@ -2311,228 +2311,66 @@
     
-EntityType entityType = EntityType.DOCUMENT;%0A        String entityReference = value;%0A%0A        int index = value.indexOf(':');%0A        if (index %3E 0) %7B%0A            String entityTypeString = value.substring(0, index);%0A    
+Namespace namespace = NamespaceUtils.toNamespace(value);%0A%0A
     
@@ -2384,22 +2384,17 @@
 ityType 
-foundE
+e
 ntityTyp
@@ -2437,24 +2437,27 @@
 ss, 
-entityTypeString
+namespace.getType()
 .toU
@@ -2469,24 +2469,75 @@
 ase());%0A    
+    String entityReference = namespace.getValue();%0A
         if (
@@ -2536,22 +2536,17 @@
     if (
-foundE
+e
 ntityTyp
@@ -2551,9 +2551,9 @@
 ype 
-!
+=
 = nu
@@ -2550,36 +2550,32 @@
 Type == null) %7B%0A
-    
             enti
@@ -2587,13 +2587,8 @@
 e = 
-found
 Enti
@@ -2597,136 +2597,55 @@
 Type
-;%0A                ++index;%0A                entityReference = index %3C value.length() ? value.substring(index) : %22%22;%0A            %7D
+.DOCUMENT;%0A            entityReference = value;
 %0A   
@@ -2892,16 +2892,30 @@
  return 
+new Namespace(
 value.ge
@@ -2940,16 +2940,9 @@
 se()
- + ':' +
+,
  thi
@@ -2969,16 +2969,28 @@
 e(value)
+).toString()
 ;%0A    %7D%0A

Introduce Namespace class