<commit_before>/*
 * PublicationFactory.java
 *
 * Created on 15. Mai 2003, 17:49
 */

package org.apache.lenya.cms.publication;

import java.util.HashMap;
import java.util.Map;
import org.apache.cocoon.environment.Context;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;
import org.apache.log4j.Category;

/**
 *
 * @author  andreas
 */
public final class PublicationFactory {
	
	private PublicationFactory() {
	}
    
    private static Category log = Category.getInstance(PublicationFactory.class);
    
    private static Map idToPublication = new HashMap();
    
    /**
     * Creates a new publication.
     * The publication ID is resolved from the request URI.
     * The servlet context path is resolved from the context object.
     * @param objectModel The object model of the Cocoon component.
     */
    public static Publication getPublication(Map objectModel) {
        assert objectModel != null;
        
        Request request = ObjectModelHelper.getRequest(objectModel);
        String uri = request.getRequestURI();
        String contextPath = request.getContextPath();
        String webappUri = uri.substring(contextPath.length() + 1);
        
        String publicationId;
        int slashIndex = webappUri.indexOf("/");
        if (slashIndex > -1) {
            publicationId = webappUri.substring(0, slashIndex);
        }
        else {
            publicationId = webappUri;
        }
        
        Context context = ObjectModelHelper.getContext(objectModel);
        String servletContextPath = context.getRealPath("");
        
        return getPublication(publicationId, servletContextPath);
    }
    
    public static Publication getPublication(String id, String servletContextPath) {
        assert id != null;
        assert servletContextPath != null;
        Publication publication;
        if (idToPublication.containsKey(id)) {
            publication = (Publication) idToPublication.get(id);
        }
        else {
            publication = new Publication(id, servletContextPath);
            idToPublication.put(id, publication);
        }
        assert publication != null;
        return publication;
    }
    
}
<commit_msg>- simplified publication id resolving - null contextPath is handled<commit_after>/*
 * PublicationFactory.java
 *
 * Created on 15. Mai 2003, 17:49
 */

package org.apache.lenya.cms.publication;

import java.util.HashMap;
import java.util.Map;
import org.apache.cocoon.environment.Context;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;
import org.apache.log4j.Category;

/**
 *
 * @author  andreas
 */
public final class PublicationFactory {
	
	private PublicationFactory() {
	}
    
    private static Category log = Category.getInstance(PublicationFactory.class);
    
    private static Map idToPublication = new HashMap();
    
    /**
     * Creates a new publication.
     * The publication ID is resolved from the request URI.
     * The servlet context path is resolved from the context object.
     * @param objectModel The object model of the Cocoon component.
     */
    public static Publication getPublication(Map objectModel) {
        assert objectModel != null;
        
        Request request = ObjectModelHelper.getRequest(objectModel);
        String contextPath = request.getContextPath();
        if (contextPath == null) {
            contextPath = "";
        }
        String webappUri = request.getRequestURI().substring(contextPath.length());
        
        String publicationId = webappUri.split("/")[1];
        assert !"".equals(publicationId);
        
        Context context = ObjectModelHelper.getContext(objectModel);
        String servletContextPath = context.getRealPath("");
        
        return getPublication(publicationId, servletContextPath);
    }
    
    public static Publication getPublication(String id, String servletContextPath) {
        assert id != null;
        assert servletContextPath != null;
        Publication publication;
        if (idToPublication.containsKey(id)) {
            publication = (Publication) idToPublication.get(id);
        }
        else {
            publication = new Publication(id, servletContextPath);
            idToPublication.put(id, publication);
        }
        assert publication != null;
        return publication;
    }
    
}

{'fraction_non_alphanumeric': 0.0887734357352609, 'fraction_numerical': 0.005873051728032527, 'mean_word_length': 2.671641791044776, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1372681', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1140, 'n_words': 297}