/*
 * 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.oak.jcr.xml;

import java.util.ArrayList;
import java.util.List;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XmlImportHandler extends DefaultHandler {

    private Node node;

    private String name;

    private final List<String> values = new ArrayList<String>();

    private final StringBuilder builder = new StringBuilder();

    public XmlImportHandler(Node parent, int uuidBehavior) {
        this.node = parent;
    }

    @Override
    public void startElement(
            String uri, String localName, String qName,
            Attributes atts) throws SAXException {
        if ("http://www.jcp.org/jcr/sv/1.0".equals(uri)) {
            String value = atts.getValue("sv:name");
            if ("node".equals(localName)) {
                try {
                    node = node.addNode(value);
                } catch (RepositoryException e) {
                    throw new SAXException(e);
                }
            } else if (value != null) {
                name = value;
            }
            builder.setLength(0);
        } else {
            for (int i = 0; i < atts.getLength(); i++) {
                try {
                    node.setProperty(atts.getQName(i), atts.getValue(i));
                } catch (RepositoryException e) {
                    throw new SAXException(e);
                }
            }
        }
    }

    @Override
    public void endElement(
            String uri, String localName, String qName)
            throws SAXException {
        if (!"http://www.jcp.org/jcr/sv/1.0".equals(uri)) {
            return;
        } else if ("value".equals(localName)) {
            values.add(builder.toString());
        } else if ("property".equals(localName)) {
            try {
                if (values.size() == 1) {
                    if (name.equals("jcr:primaryType")) {
                        node.setPrimaryType(values.get(0));
                    } else if (name.equals("jcr:mixinTypes")) {
                        node.addMixin(values.get(0));
                    } else {
                        node.setProperty(name, values.get(0));
                    }
                } else if (name.equals("jcr:mixinTypes")) {
                    for (String value : values) {
                        node.addMixin(value);
                    }
                } else {
                    node.setProperty(name, values.toArray(new String[values.size()]));
                }
            } catch (RepositoryException e) {
                throw new SAXException(e);
            }
            values.clear();
        } else if ("node".equals(localName)) {
            try {
                node = node.getParent();
            } catch (RepositoryException e) {
                throw new SAXException(e);
            }
        }
    }

    @Override
    public void characters(char[] ch, int offset, int length) {
        builder.append(ch, offset, length);
    }

}
@@ -1138,24 +1138,54 @@
 Node node;%0A%0A
+    private String nodeName;%0A%0A
     private 
@@ -1751,176 +1751,87 @@
     
-try %7B%0A                    node = node.addNode(value);%0A                %7D catch (RepositoryException e) %7B%0A                    throw new SAXException(e);%0A                %7D
+// create node on jcr:primaryType sv:property%0A                nodeName = value;
 %0A   
@@ -2762,43 +2762,226 @@
     
-node.setPrimaryType(values.get(0));
+try %7B%0A                            node = node.addNode(nodeName, values.get(0));%0A                        %7D catch (RepositoryException e) %7B%0A                            throw new SAXException(e);%0A                        %7D
 %0A   

Support for XML imports - create node when primary node type is known. otherwise addNode() without node type name may throw a ConstraintViolationException because there is no child node definition with a default node type