All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.milton.http.webdav.PropFindSaxHandler Maven / Gradle / Ivy

Go to download

Milton Community Edition: Supports DAV level 1 and is available on Apache2 license

There is a newer version: 4.0.3.2215
Show newest version
/*
 * 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 io.milton.http.webdav;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import javax.xml.namespace.QName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class PropFindSaxHandler extends DefaultHandler {

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

    private final Stack elementPath = new Stack<>();
    private final Map attributes = new HashMap<>();
    private final StringBuilder sb = new StringBuilder();
    private boolean inProp;
    private boolean allProp;

    @Override
    public void startElement( String uri, String localName, String name, Attributes attributes ) throws SAXException {
        if( elementPath.size() > 0 ) {
            String elname = elementPath.peek().getLocalPart();
            if( elname.equals( "prop" ) ) {
                inProp = true;
            }
        }
        if( localName.equals( "allprop" ) ) {
            allProp = true;
        }

        QName qname = new QName( uri, localName );
        elementPath.push( qname );
        super.startElement( uri, localName, name, attributes );
    }

    @Override
    public void characters( char[] ch, int start, int length ) throws SAXException {
        if( inProp ) {
            sb.append( ch, start, length );
        }
    }

    @Override
    public void endElement( String uri, String localName, String name ) throws SAXException {
        elementPath.pop();
        if( elementPath.size() > 0 && elementPath.peek().getLocalPart().endsWith( "prop" ) ) {
            if( sb != null ) {
//                uri = uri.substring( 0, uri.length()-1); // need to strip trailing :
                QName qname = new QName( uri, localName );
                getAttributes().put( qname, sb.toString().trim() );
            }
            sb.delete( 0, sb.length() );
        }

        super.endElement( uri, localName, name );
    }

    public Map getAttributes() {
        return attributes;
    }

    public boolean isAllProp() {
        return allProp;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy