1 package com.bradmcevoy.http.webdav;
2
3 import com.bradmcevoy.io.ReadingException;
4 import com.bradmcevoy.io.StreamUtils;
5 import com.bradmcevoy.io.WritingException;
6 import java.io.ByteArrayInputStream;
7 import org.apache.commons.io.output.ByteArrayOutputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.util.HashMap;
11 import java.util.HashSet;
12 import javax.xml.namespace.QName;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import org.xml.sax.InputSource;
16 import org.xml.sax.SAXException;
17 import org.xml.sax.XMLReader;
18 import org.xml.sax.helpers.XMLReaderFactory;
19
20
21
22
23
24 public class DefaultPropPatchParser implements PropPatchRequestParser {
25
26 private final static Logger log = LoggerFactory.getLogger( DefaultPropPatchParser.class );
27
28 public ParseResult getRequestedFields( InputStream in ) {
29 log.debug( "getRequestedFields" );
30 try {
31 ByteArrayOutputStream bout = new ByteArrayOutputStream();
32 StreamUtils.readTo( in, bout, false, true );
33 byte[] arr = bout.toByteArray();
34 return parseContent( arr );
35 } catch( SAXException ex ) {
36 throw new RuntimeException( ex );
37 } catch( ReadingException ex ) {
38 throw new RuntimeException( ex );
39 } catch( WritingException ex ) {
40 throw new RuntimeException( ex );
41 } catch( IOException ex ) {
42 throw new RuntimeException( ex );
43 }
44 }
45
46 private ParseResult parseContent( byte[] arr ) throws IOException, SAXException {
47 if( arr.length > 0 ) {
48 log.debug( "processing content" );
49 ByteArrayInputStream bin = new ByteArrayInputStream( arr );
50 XMLReader reader = XMLReaderFactory.createXMLReader();
51 PropPatchSaxHandler handler = new PropPatchSaxHandler();
52 reader.setContentHandler( handler );
53 reader.parse( new InputSource( bin ) );
54 log.debug( "toset: " + handler.getAttributesToSet().size());
55 return new ParseResult( handler.getAttributesToSet(), handler.getAttributesToRemove().keySet() );
56 } else {
57 log.debug( "empty content" );
58 return new ParseResult( new HashMap<QName, String>(), new HashSet<QName>() );
59 }
60
61 }
62 }