<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.server.io;

import org.apache.jackrabbit.JcrConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.ImportUUIDBehavior;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * <code>XmlHandler</code> imports xml files and exports nodes that have
 * the proper {@link #XML_MIMETYPE} defined with their content. The export is
 * performed by running a {@link Session#exportDocumentView(String, OutputStream, boolean, boolean)
 * document view export} for the content of the export root defined with the
 * specified {@link ExportContext}.<br>
 * Please note that this handler is not suited for a generic system or document
 * view import/export of {@link Node}s because an extra root node is always
 * created during import and expected during export, respectively.
 */
public class XmlHandler extends DefaultHandler {

    private static Logger log = LoggerFactory.getLogger(XmlHandler.class);

    /**
     * the xml mimetype
     */
    public static final String XML_MIMETYPE = "text/xml";

    /**
     * Creates a new <code>XmlHandler</code> with default nodetype definitions
     * and without setting the IOManager.
     *
     * @see IOHandler#setIOManager(IOManager)
     */
    public XmlHandler() {
    }

    /**
     * Creates a new <code>XmlHandler</code> with default nodetype definitions:<br>
     * <ul>
     * <li>Nodetype for Collection: {@link JcrConstants#NT_UNSTRUCTURED nt:unstructured}</li>
     * <li>Nodetype for Non-Collection: {@link JcrConstants#NT_FILE nt:file}</li>
     * <li>Nodetype for Non-Collection content: {@link JcrConstants#NT_UNSTRUCTURED nt:unstructured}</li>
     * </ul>
     *
     * @param ioManager
     */
    public XmlHandler(IOManager ioManager) {
        super(ioManager, JcrConstants.NT_UNSTRUCTURED, JcrConstants.NT_FILE, JcrConstants.NT_UNSTRUCTURED);
    }

    /**
     * Creates a new <code>XmlHandler</code>
     *
     * @param ioManager
     * @param collectionNodetype
     * @param defaultNodetype
     * @param contentNodetype
     */
    public XmlHandler(IOManager ioManager, String collectionNodetype, String defaultNodetype, String contentNodetype) {
        super(ioManager, collectionNodetype, defaultNodetype, contentNodetype);
    }

    /**
     * @see IOHandler#canImport(ImportContext, boolean)
     */
    @Override
    public boolean canImport(ImportContext context, boolean isCollection) {
        if (context == null || context.isCompleted()) {
            return false;
        }
        boolean isXmlMimeType = XML_MIMETYPE.equals(context.getMimeType());
        return isXmlMimeType
                && context.hasStream()
                && context.getContentLength() > 0
                && super.canImport(context, isCollection);
    }

    /**
     * @see DefaultHandler#importData(ImportContext, boolean, Node)
     */
    @Override
    protected boolean importData(ImportContext context, boolean isCollection, Node contentNode) throws IOException, RepositoryException {
        InputStream in = context.getInputStream();
        int uuidBehavior = (isCollection)
            ? ImportUUIDBehavior.IMPORT_UUID_COLLISION_REMOVE_EXISTING
            : ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW;
        try {
            contentNode.getSession().importXML(contentNode.getPath(), in, uuidBehavior);
        } finally {
            in.close();<commit_msg>JCR-2522 unable to workspace import XML.<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.server.io;

import org.apache.jackrabbit.JcrConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.ImportUUIDBehavior;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Set;

/**
 * <code>XmlHandler</code> imports xml files and exports nodes that have
 * the proper {@link #XML_MIMETYPE} defined with their content. The export is
 * performed by running a {@link Session#exportDocumentView(String, OutputStream, boolean, boolean)
 * document view export} for the content of the export root defined with the
 * specified {@link ExportContext}.<br>
 * Please note that this handler is not suited for a generic system or document
 * view import/export of {@link Node}s because an extra root node is always
 * created during import and expected during export, respectively.
 */
public class XmlHandler extends DefaultHandler {

    private static Logger log = LoggerFactory.getLogger(XmlHandler.class);

    /**
     * the xml mimetype
     */
    public static final String XML_MIMETYPE = "text/xml";

    /**
     * the alternative xml mimetype. tika detects xml as this.
     */
    public static final String XML_MIMETYPE_ALT = "application/xml";

    private static final Set<String> supportedTypes;
    static {
        supportedTypes = new HashSet<String>();
        supportedTypes.add(XML_MIMETYPE);
        supportedTypes.add(XML_MIMETYPE_ALT);
    }


    /**
     * Creates a new <code>XmlHandler</code> with default nodetype definitions
     * and without setting the IOManager.
     *
     * @see IOHandler#setIOManager(IOManager)
     */
    public XmlHandler() {
    }

    /**
     * Creates a new <code>XmlHandler</code> with default nodetype definitions:<br>
     * <ul>
     * <li>Nodetype for Collection: {@link JcrConstants#NT_UNSTRUCTURED nt:unstructured}</li>
     * <li>Nodetype for Non-Collection: {@link JcrConstants#NT_FILE nt:file}</li>
     * <li>Nodetype for Non-Collection content: {@link JcrConstants#NT_UNSTRUCTURED nt:unstructured}</li>
     * </ul>
     *
     * @param ioManager
     */
    public XmlHandler(IOManager ioManager) {
        super(ioManager, JcrConstants.NT_UNSTRUCTURED, JcrConstants.NT_FILE, JcrConstants.NT_UNSTRUCTURED);
    }

    /**
     * Creates a new <code>XmlHandler</code>
     *
     * @param ioManager
     * @param collectionNodetype
     * @param defaultNodetype
     * @param contentNodetype
     */
    public XmlHandler(IOManager ioManager, String collectionNodetype, String defaultNodetype, String contentNodetype) {
        super(ioManager, collectionNodetype, defaultNodetype, contentNodetype);
    }

    /**
     * @see IOHandler#canImport(ImportContext, boolean)
     */
    @Override
    public boolean canImport(ImportContext context, boolean isCollection) {
        return !(context == null || context.isCompleted())
                && supportedTypes.contains(context.getMimeType())
                && context.hasStream()
                && context.getContentLength() > 0
                && super.canImport(context, isCollection);
    }

    /**
     * @see DefaultHandler#importData(ImportContext, boolean, Node)
     */
    @Override
    protected boolean importData(ImportContext context, boolean isCollection, Node contentNode) throws IOException, RepositoryException {
        InputStream in = context.getInputStream();
        int uuidBehavior = (isCollection)
            ? ImportUUIDBehavior.IMPORT_UUID_COLLISION_REMOVE_EXISTING
            : ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW;
        try {
            contentNode.getSession().importXML(contentNode.getPath(), in, uuidBehavior);
        } finally {
            in.close();
{'fraction_non_alphanumeric': 0.08919876051350155, 'fraction_numerical': 0.00199203187250996, 'mean_word_length': 3.4061433447098977, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 9}, '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': '2408608', 'n_tokens_mistral': 2492, 'n_tokens_neox': 2454, 'n_words': 766}