/*
 * @package jsDAV
 * @subpackage DAV
 * @copyright Copyright(c) 2011 Ajax.org B.V. <info AT ajax DOT org>
 * @author Mike de Boer <info AT mikedeboer DOT nl>
 * @license http://github.com/mikedeboer/jsDAV/blob/master/LICENSE MIT License
 */
"use strict";

var jsDAV          = require("./../../jsdav");
var jsDAV_Property = require("./../property").jsDAV_Property;

var Exc            = require("./../exceptions");

function jsDAV_Property_Response(href, responseProperties) {
    this.href               = href;
    this.responseProperties = responseProperties || {};
}

exports.jsDAV_Property_Response = jsDAV_Property_Response;

(function() {
    this.REGBASE = this.REGBASE | jsDAV.__PROP_RESPONSE__;

    /**
     * Returns the url
     *
     * @return {string}
     */
    this.getHref = function() {
        return this.href;
    };

    /**
     * Returns the property list
     *
     * @return {array}
     */
    this.getResponseProperties = function() {
        return this.responseProperties;
    };

    /**
     * serialize
     *
     * @param {jsDAV_Server} server
     * @param {String}       dom
     * @return void
     */
    this.serialize = function(handler, dom) {
        var properties = this.responseProperties;

        dom += "<d:response>"
            // Adding the baseurl to the beginning of the url
            +  "<d:href>" + encodeURI(handler.server.getBaseUri() + this.href) + "</d:href>";

        // The properties variable is an array containing properties, grouped by
        // HTTP status
        var resHttpStatus, resPropertyGroup, resPropertyName, resPropertyValue,
            resPropName, resNodeName, resCurrentProperty, resBlockProps;

        for (resHttpStatus in properties) {
            resPropertyGroup = properties[resHttpStatus];
            // The 'href' is also in this hashmap, and it's special cased.
            // We will ignore it
            if (!resPropertyGroup || resHttpStatus == "href") continue;

            resBlockProps = "";

            for (resPropertyName in resPropertyGroup) {
                if (typeof resPropertyName != "string")
                    continue;


                resPropertyValue = resPropertyGroup[resPropertyName];
                resCurrentProperty = "";
                resPropName        = resPropertyName.match(/^\{([^\}]*)\}(.*)/);

                // If the namespace was defined in the top-level xml namespaces, it means
                // there was already a namespace declaration, and we don't have to worry about it.
                if (resPropName && resPropName[1] && handler.xmlNamespaces[resPropName[1]]) {
                    resNodeName = handler.xmlNamespaces[resPropName[1]] + ":" + resPropName[2];
                    resCurrentProperty = "<" + resNodeName;
                }
                else {
                    // Define the namespace for this node directly...
                    resNodeName        = resPropName[2];
                    resCurrentProperty = "<" + resNodeName + " xmlns=\"" + resPropName[1] + "\"";
                }

                if(resPropertyValue === null) {
                    // There is no value, return a short tag
                    resCurrentProperty += "/>";
                }
                else if (typeof resPropertyValue != "object") {
                    resCurrentProperty += ">" + resPropertyValue + "</" + resNodeName + ">";
                }
                else if (resPropertyValue.hasFeature && resPropertyValue.hasFeature(jsDAV.__PROPERTY__)) {
                    resCurrentProperty += ">";
                    resCurrentProperty = resPropertyValue.serialize(handler, resCurrentProperty)
                        +  "</" + resNodeName + ">";
                }
                else if (resPropertyValue) {
                    throw new Exc.jsDAV_Exception("Unknown property value type: "
                        + resPropertyValue + " for property: " + resPropertyName);
                }

                resBlockProps += resCurrentProperty;
            }

            // If there are no properties in this group, we can also just carry on
            if (resBlockProps) {
                dom += "<d:propstat><d:prop>" + resBlockProps + "</d:prop>"
                    +  "<d:status>" + handler.getStatusMessage(resHttpStatus) + "</d:status>"
                    +  "</d:propstat>";
            }
        }
        
        return dom + "</d:response>";
    };
}).call(jsDAV_Property_Response.prototype = new jsDAV_Property());

@@ -410,16 +410,49 @@
 tions%22);
+%0Avar Util = require(%22./../util%22);
 %0A%0Afuncti
@@ -1656,21 +1656,8 @@
     
- resPropName,
  res
@@ -2289,28 +2289,29 @@
         
-resP
+var p
 ropName 
        =
@@ -2306,58 +2306,48 @@
 ame 
-       = resPropertyName.match(/%5E%5C%7B(%5B%5E%5C%7D%5D*)%5C%7D(.*)/
+= Util.fromClarkNotation(resPropertyName
 );%0A%0A
@@ -2559,35 +2559,17 @@
 if (
-resPropName && resP
+p
 ropName%5B
 1%5D &
@@ -2564,17 +2564,17 @@
 ropName%5B
-1
+0
 %5D && han
@@ -2592,28 +2592,25 @@
 espaces%5B
-resP
+p
 ropName%5B
 1%5D%5D) %7B%0A 
@@ -2601,17 +2601,17 @@
 ropName%5B
-1
+0
 %5D%5D) %7B%0A  
@@ -2664,28 +2664,25 @@
 espaces%5B
-resP
+p
 ropName%5B
 1%5D%5D + %22:
@@ -2673,17 +2673,17 @@
 ropName%5B
-1
+0
 %5D%5D + %22:%22
@@ -2685,28 +2685,25 @@
 + %22:%22 + 
-resP
+p
 ropName%5B
 2%5D;%0A    
@@ -2686,33 +2686,33 @@
  %22:%22 + propName%5B
-2
+1
 %5D;%0A             
@@ -2910,28 +2910,25 @@
       = 
-resP
+p
 ropName%5B
 2%5D;%0A    
@@ -2919,17 +2919,17 @@
 ropName%5B
-2
+1
 %5D;%0A     
@@ -3002,20 +3002,17 @@
 %22 + 
-resP
+p
 ropName%5B
 1%5D +
@@ -3007,17 +3007,17 @@
 ropName%5B
-1
+0
 %5D + %22%5C%22%22

Use Util.fromClarkNotation() to parse property namespaces