<commit_before>/*
 * 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.wysiwyg.server.internal.wiki;

import org.apache.commons.lang.StringUtils;
import org.xwiki.bridge.DocumentAccessBridge;
import org.xwiki.component.annotation.Requirement;
import org.xwiki.gwt.wysiwyg.client.wiki.EntityConfig;
import org.xwiki.model.reference.AttachmentReference;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.EntityReferenceResolver;
import org.xwiki.model.reference.EntityReferenceSerializer;
import org.xwiki.rendering.listener.ResourceReference;
import org.xwiki.rendering.listener.ResourceType;
import org.xwiki.rendering.parser.LinkParser;
import org.xwiki.rendering.renderer.link.LinkReferenceSerializer;

import com.xpn.xwiki.wysiwyg.server.wiki.EntityReferenceConverter;
import com.xpn.xwiki.wysiwyg.server.wiki.LinkService;

/**
 * The service used to create links.
 * 
 * @version $Id$
 */
public class DefaultLinkService implements LinkService
{
    /**
     * The image URI protocol.
     */
    private static final String IMAGE_URI_PROTOCOL = "image:";

    /**
     * The component used to access documents. This is temporary till XWiki model is moved into components.
     */
    @Requirement
    private DocumentAccessBridge documentAccessBridge;

    /**
     * The component used to serialize XWiki document references.
     */
    @Requirement("compact")
    private EntityReferenceSerializer<String> entityReferenceSerializer;

    /**
     * The component used to resolve an entity reference relative to another entity reference.
     */
    @Requirement("explicit/reference")
    private EntityReferenceResolver<EntityReference> explicitReferenceEntityReferenceResolver;

    /**
     * The component used to resolve a string entity reference relative to another entity reference.
     */
    @Requirement("explicit")
    private EntityReferenceResolver<String> explicitStringEntityReferenceResolver;

    /**
     * The component used to serialize link references.
     * <p>
     * Note: The link reference syntax is independent of the syntax of the edited document. The current hint should be
     * replaced with a generic one to avoid confusion.
     */
    @Requirement("xwiki/2.1")
    private LinkReferenceSerializer linkReferenceSerializer;

    /**
     * The component used to parser link references.
     * <p>
     * Note: The link reference syntax is independent of the syntax of the edited document. The current hint should be
     * replaced with a generic one to avoid confusion.
     */
    @Requirement("xwiki/2.1")
    private LinkParser linkReferenceParser;

    /**
     * The object used to convert between client-side entity references and server-side entity references.
     */
    private final EntityReferenceConverter entityReferenceConverter = new EntityReferenceConverter();

    /**
     * {@inheritDoc}
     * 
     * @see LinkService#getEntityConfig(org.xwiki.gwt.wysiwyg.client.wiki.EntityReference,
     *      org.xwiki.gwt.wysiwyg.client.wiki.EntityReference)
     */
    public EntityConfig getEntityConfig(org.xwiki.gwt.wysiwyg.client.wiki.EntityReference origin,
        org.xwiki.gwt.wysiwyg.client.wiki.EntityReference destination)
    {
        EntityReference originReference = entityReferenceConverter.convert(origin);
        EntityReference destinationReference = entityReferenceConverter.convert(destination);
        destinationReference =
            explicitReferenceEntityReferenceResolver.resolve(destinationReference, destinationReference.getType(),
                originReference);
        String destRelativeStrRef = this.entityReferenceSerializer.serialize(destinationReference, originReference);

        EntityConfig entityConfig = new EntityConfig();
        entityConfig.setUrl(getEntityURL(destinationReference));
        entityConfig.setReference(getLinkReference(destination.getType(), destRelativeStrRef));
        return entityConfig;
    }

    /**
     * @param entityReference an entity reference<commit_msg>XWIKI-5554: Generalize Image support and add relative paths support XWiki Syntax 2.1 * Rename LinkParser to ResourceReferenceParser.<commit_after>/*
 * 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.wysiwyg.server.internal.wiki;

import org.apache.commons.lang.StringUtils;
import org.xwiki.bridge.DocumentAccessBridge;
import org.xwiki.component.annotation.Requirement;
import org.xwiki.gwt.wysiwyg.client.wiki.EntityConfig;
import org.xwiki.model.reference.AttachmentReference;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.EntityReferenceResolver;
import org.xwiki.model.reference.EntityReferenceSerializer;
import org.xwiki.rendering.listener.ResourceReference;
import org.xwiki.rendering.listener.ResourceType;
import org.xwiki.rendering.parser.ResourceReferenceParser;
import org.xwiki.rendering.renderer.link.LinkReferenceSerializer;

import com.xpn.xwiki.wysiwyg.server.wiki.EntityReferenceConverter;
import com.xpn.xwiki.wysiwyg.server.wiki.LinkService;

/**
 * The service used to create links.
 * 
 * @version $Id$
 */
public class DefaultLinkService implements LinkService
{
    /**
     * The image URI protocol.
     */
    private static final String IMAGE_URI_PROTOCOL = "image:";

    /**
     * The component used to access documents. This is temporary till XWiki model is moved into components.
     */
    @Requirement
    private DocumentAccessBridge documentAccessBridge;

    /**
     * The component used to serialize XWiki document references.
     */
    @Requirement("compact")
    private EntityReferenceSerializer<String> entityReferenceSerializer;

    /**
     * The component used to resolve an entity reference relative to another entity reference.
     */
    @Requirement("explicit/reference")
    private EntityReferenceResolver<EntityReference> explicitReferenceEntityReferenceResolver;

    /**
     * The component used to resolve a string entity reference relative to another entity reference.
     */
    @Requirement("explicit")
    private EntityReferenceResolver<String> explicitStringEntityReferenceResolver;

    /**
     * The component used to serialize link references.
     * <p>
     * Note: The link reference syntax is independent of the syntax of the edited document. The current hint should be
     * replaced with a generic one to avoid confusion.
     */
    @Requirement("xwiki/2.1")
    private LinkReferenceSerializer linkReferenceSerializer;

    /**
     * The component used to parser link references.
     * <p>
     * Note: The link reference syntax is independent of the syntax of the edited document. The current hint should be
     * replaced with a generic one to avoid confusion.
     */
    @Requirement("xwiki/2.1")
    private ResourceReferenceParser linkReferenceParser;

    /**
     * The object used to convert between client-side entity references and server-side entity references.
     */
    private final EntityReferenceConverter entityReferenceConverter = new EntityReferenceConverter();

    /**
     * {@inheritDoc}
     * 
     * @see LinkService#getEntityConfig(org.xwiki.gwt.wysiwyg.client.wiki.EntityReference,
     *      org.xwiki.gwt.wysiwyg.client.wiki.EntityReference)
     */
    public EntityConfig getEntityConfig(org.xwiki.gwt.wysiwyg.client.wiki.EntityReference origin,
        org.xwiki.gwt.wysiwyg.client.wiki.EntityReference destination)
    {
        EntityReference originReference = entityReferenceConverter.convert(origin);
        EntityReference destinationReference = entityReferenceConverter.convert(destination);
        destinationReference =
            explicitReferenceEntityReferenceResolver.resolve(destinationReference, destinationReference.getType(),
                originReference);
        String destRelativeStrRef = this.entityReferenceSerializer.serialize(destinationReference, originReference);

        EntityConfig entityConfig = new EntityConfig();
        entityConfig.setUrl(getEntityURL(destinationReference));
        entityConfig.setReference(getLinkReference(destination.getType(), destRelativeStrRef));
        return entityConfig;
    }

    /**
     * @param entityReference an entity reference
{'fraction_non_alphanumeric': 0.07652399481193256, 'fraction_numerical': 0.003990821111443679, 'mean_word_length': 3.8897560975609755, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2685137', 'n_tokens_mistral': 2523, 'n_tokens_neox': 2477, 'n_words': 800}