<commit_before>package org.wyona.yanel.impl.resources;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Category;
import org.wyona.yanel.core.Path;
import org.wyona.yanel.core.api.attributes.ViewableV2;
import org.wyona.yarep.core.Repository;
import org.wyona.yarep.core.RepositoryFactory;
import org.wyona.yarep.util.RepoPath;
import org.xml.sax.SAXException;

public class DavCollection extends XmlViewResource implements ViewableV2 {

    private static Category log = Category.getInstance(DavCollection.class);
    
    // WebDAV namespace definition
    
    // NOTE: We use the DavCollection in the context of Neutron10 so
    // the namespaces are different.
    private static final String WEBDAV_NS_PREFIX = ""; // "D"
    private static final String WEBDAV_PREFIX = ""; //WEBDAV_NS_PREFIX + ":";
    private static final String WEBDAV_NS_URI = "http://www.wyona.org/neutron/1.0"; //"DAV:";
    
    // WebDav element definitions
    private static final String WEBDAV_EL_MSTATUS   = "multistatus";
    private static final String WEBDAV_EL_STATUS    = "status";
    private static final String WEBDAV_EL_HREF      = "href";
    private static final String WEBDAV_EL_RESPONSE  = "response";
    private static final String WEBDAV_EL_PROPSTAT  = "propstat";
    private static final String WEBDAV_EL_PROP      = "prop";
    private static final String WEBDAV_EL_DISPNAME  = "displayname";
    private static final String WEBDAV_EL_LASTMOD   = "lastmodified";
    private static final String WEBDAV_EL_CREATDATE = "creationdate";
    private static final String WEBDAV_EL_RESTYPE   = "resourcetype";
    private static final String WEBDAV_EL_CONTTYPE  = "contenttype";
    private static final String WEBDAV_EL_CONTLEN   = "contentlength";
    private static final String WEBDAV_EL_LOCKDISC  = "lockdiscovery";
    private static final String WEBDAV_EL_COLL      = "collection";
    private static final String WEBDAV_EL_RES       = "resource";
    
   
    
    public DavCollection() throws ParserConfigurationException, IOException {
        super();
        // TODO Auto-generated constructor stub
    }

    public void buildXmlView(Path path, String viewId, HttpServletRequest request, HttpServletResponse response) throws SAXException {

        response.setStatus(207, "Multi Status");
        
        startDocument();          
        startPrefixMapping(WEBDAV_NS_PREFIX, WEBDAV_NS_URI);      
        startElement(WEBDAV_NS_PREFIX, WEBDAV_EL_MSTATUS, WEBDAV_PREFIX + WEBDAV_EL_MSTATUS, null);
        
        // copyied from DirectoryResource.java, modified to use a ContentHandler
        // instead of a StringBuffer for the XML view 
        
        Repository contentRepo = null;
        try {
            RepoPath rp = new org.wyona.yarep.util.YarepUtil().getRepositoryPath(new org.wyona.yarep.core.Path(path.toString()), new RepositoryFactory());
            contentRepo = rp.getRepo();
            org.wyona.yarep.core.Path p = rp.getPath();

            // TODO: This doesn't seem to work ... (check on Yarep ...)
            if (contentRepo.isResource(p)) {
                log.warn("Path is a resource instead of a collection: " + p);
                // p = p.getParent();
            }

            // TODO: Implement org.wyona.yarep.core.Path.getParent()
            if (!contentRepo.isCollection(p)) {
                log.warn("Path is not a collection: " + p);
                p = new org.wyona.yarep.core.Path(new org.wyona.commons.io.Path(p.toString()).getParent().toString());
                log.warn("Use parent of path: " + p);
            }
         
            
            this.propfindAddResource(request.getContextPath() + path.toString(), 
                    path.getName(), 
                    WEBDAV_EL_COLL,
                    "httpd/unix-directory",
                    new Date(contentRepo.getLastModified(p)).toGMTString());
            
            // TODO: Do not show the children with suffix .yanel-rti resp. make
            // this configurable!
            // NOTE: Do not hardcode the .yanel-rti, but rather use
            // Path.getRTIPath ...
            
            org.wyona.yarep.core.Path[] children = contentRepo.getChildren(p);
            for (int i = 0; i < children.length; i++) {
                if (contentRepo.isResource(children[i])) {
                    this.propfindAddResource(request.getContextPath() + children[i].toString(), 
                            children[i].getName(), 
                            WEBDAV_EL_RES,
                            "httpd/unix-directory",
                            new Date(contentRepo.getLastModified(children[i])).toGMTString());
                    
                } else if (contentRepo.isCollection(children[i])) {
                    this.propfindAddResource(request.getContextPath() + children[i].toString(), 
                            children[i].getName(), 
                            WEBDAV_EL_COLL,
                            "httpd/unix-directory",
                            new Date(contentRepo.getLastModified(children[i])).toGMTString());
                } else {
                    log.debug("####### neither collection nor resource");
                }
            }
            
        } catch (Exception e) {
            log.error(e);
        }
        
        endElement(WEBDAV_NS_URI, WEBDAV_EL_MSTATUS, WEBDAV_PREFIX + WEBDAV_EL_MSTATUS);
        endPrefixMapping(WEBDAV_NS_PREFIX);
        endDocument();
    }
    
    /**
     * Adds a resource to a propfind response
     * @param href<commit_msg>added repository prefix for href elements<commit_after>package org.wyona.yanel.impl.resources;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Category;
import org.wyona.yanel.core.Path;
import org.wyona.yanel.core.api.attributes.ViewableV2;
import org.wyona.yarep.core.Repository;
import org.wyona.yarep.core.RepositoryFactory;
import org.wyona.yarep.util.RepoPath;
import org.xml.sax.SAXException;

public class DavCollection extends XmlViewResource implements ViewableV2 {

    private static Category log = Category.getInstance(DavCollection.class);
    
    // WebDAV namespace definition
    
    // NOTE: We use the DavCollection in the context of Neutron10 so
    // the namespaces are different.
    private static final String WEBDAV_NS_PREFIX = ""; // "D"
    private static final String WEBDAV_PREFIX = ""; //WEBDAV_NS_PREFIX + ":";
    private static final String WEBDAV_NS_URI = "http://www.wyona.org/neutron/1.0"; //"DAV:";
    
    // WebDav element definitions
    private static final String WEBDAV_EL_MSTATUS   = "multistatus";
    private static final String WEBDAV_EL_STATUS    = "status";
    private static final String WEBDAV_EL_HREF      = "href";
    private static final String WEBDAV_EL_RESPONSE  = "response";
    private static final String WEBDAV_EL_PROPSTAT  = "propstat";
    private static final String WEBDAV_EL_PROP      = "prop";
    private static final String WEBDAV_EL_DISPNAME  = "displayname";
    private static final String WEBDAV_EL_LASTMOD   = "lastmodified";
    private static final String WEBDAV_EL_CREATDATE = "creationdate";
    private static final String WEBDAV_EL_RESTYPE   = "resourcetype";
    private static final String WEBDAV_EL_CONTTYPE  = "contenttype";
    private static final String WEBDAV_EL_CONTLEN   = "contentlength";
    private static final String WEBDAV_EL_LOCKDISC  = "lockdiscovery";
    private static final String WEBDAV_EL_COLL      = "collection";
    private static final String WEBDAV_EL_RES       = "resource";
    
   
    
    public DavCollection() throws ParserConfigurationException, IOException {
        super();
        // TODO Auto-generated constructor stub
    }

    public void buildXmlView(Path path, String viewId, HttpServletRequest request, HttpServletResponse response) throws SAXException {

        response.setStatus(207, "Multi Status");
        
        startDocument();          
        startPrefixMapping(WEBDAV_NS_PREFIX, WEBDAV_NS_URI);      
        startElement(WEBDAV_NS_PREFIX, WEBDAV_EL_MSTATUS, WEBDAV_PREFIX + WEBDAV_EL_MSTATUS, null);
        
        // copyied from DirectoryResource.java, modified to use a ContentHandler
        // instead of a StringBuffer for the XML view 
        
        Repository contentRepo = null;
        try {
            RepoPath rp = new org.wyona.yarep.util.YarepUtil().getRepositoryPath(new org.wyona.yarep.core.Path(path.toString()), new RepositoryFactory());
            contentRepo = rp.getRepo();
            org.wyona.yarep.core.Path p = rp.getPath();

            // TODO: This doesn't seem to work ... (check on Yarep ...)
            if (contentRepo.isResource(p)) {
                log.warn("Path is a resource instead of a collection: " + p);
                // p = p.getParent();
            }

            // TODO: Implement org.wyona.yarep.core.Path.getParent()
            if (!contentRepo.isCollection(p)) {
                log.warn("Path is not a collection: " + p);
                p = new org.wyona.yarep.core.Path(new org.wyona.commons.io.Path(p.toString()).getParent().toString());
                log.warn("Use parent of path: " + p);
            }
         
            // TODO: Do not show the children with suffix .yanel-rti resp. make
            // this configurable!
            // NOTE: Do not hardcode the .yanel-rti, but rather use
            // Path.getRTIPath ...
            
            org.wyona.yarep.core.Path[] children = contentRepo.getChildren(p);
            for (int i = 0; i < children.length; i++) {
                if (contentRepo.isResource(children[i])) {
                    this.propfindAddResource(request.getContextPath() + "/" + contentRepo.getID() + children[i].toString(), 
                            children[i].getName(), 
                            WEBDAV_EL_RES,
                            "httpd/unix-directory",
                            new Date(contentRepo.getLastModified(children[i])).toGMTString());
                    
                } else if (contentRepo.isCollection(children[i])) {
                    this.propfindAddResource(request.getContextPath() + "/" + contentRepo.getID() + children[i].toString(),
                            children[i].getName(), 
                            WEBDAV_EL_COLL,
                            "httpd/unix-directory",
                            new Date(contentRepo.getLastModified(children[i])).toGMTString());
                } else {
                    log.debug("####### neither collection nor resource");
                }
            }
            
        } catch (Exception e) {
            log.error(e);
        }
        
        endElement(WEBDAV_NS_URI, WEBDAV_EL_MSTATUS, WEBDAV_PREFIX + WEBDAV_EL_MSTATUS);
        endPrefixMapping(WEBDAV_NS_PREFIX);
        endDocument();
    }
    
    /**
     * Adds a resource to a propfind response
     * @param href
{'fraction_non_alphanumeric': 0.09757398027192749, 'fraction_numerical': 0.0019550342130987292, 'mean_word_length': 2.212674850128461, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '4675293', 'n_tokens_mistral': 3270, 'n_tokens_neox': 3226, 'n_words': 717}