Author: dulvac
Date: Wed Aug 10 12:39:52 2016
New Revision: 1755732

URL: http://svn.apache.org/viewvc?rev=1755732&view=rev
Log:
SLING-5880 made forms package private

Added:
    
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/impl/
    
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/impl/FormValues.java
    
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/impl/Vals.java
    
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/impl/package-info.java
Removed:
    
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/FormValues.java
    
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/Vals.java
    
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/package-info.java
Modified:
    sling/trunk/contrib/extensions/hapi/client/pom.xml
    
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/impl/microdata/MicrodataDocument.java

Modified: sling/trunk/contrib/extensions/hapi/client/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/hapi/client/pom.xml?rev=1755732&r1=1755731&r2=1755732&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/hapi/client/pom.xml (original)
+++ sling/trunk/contrib/extensions/hapi/client/pom.xml Wed Aug 10 12:39:52 2016
@@ -61,7 +61,6 @@
                 <configuration>
                     <instructions>
                         <Bundle-Category>sling</Bundle-Category>
-                        
<Private-Package>org.apache.sling.hapi.client.forms</Private-Package>
                         
<Sling-Initial-Content>SLING-INF;overwrite=true</Sling-Initial-Content>
                         <Embed-Dependency>groupId=org.jsoup</Embed-Dependency>
                     </instructions>

Added: 
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/impl/FormValues.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/impl/FormValues.java?rev=1755732&view=auto
==============================================================================
--- 
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/impl/FormValues.java
 (added)
+++ 
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/impl/FormValues.java
 Wed Aug 10 12:39:52 2016
@@ -0,0 +1,116 @@
+/*******************************************************************************
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.sling.hapi.client.forms.impl;
+
+import java.io.UnsupportedEncodingException;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.utils.URLEncodedUtils;
+import org.apache.http.entity.mime.MultipartEntityBuilder;
+import org.apache.http.message.BasicNameValuePair;
+import org.jsoup.nodes.Element;
+
+/**
+ *
+ */
+public class FormValues {
+    private Element form;
+
+    private Iterable<NameValuePair> values;
+
+    private Vals list = new Vals();
+
+    public FormValues(Element form, Iterable<NameValuePair> values) {
+        this.form = form;
+        this.values = values;
+
+        build();
+        resolve();
+    }
+
+    /**
+     * @return
+     * {@see 
http://www.w3.org/TR/html5/forms.html#constructing-the-form-data-set}
+     */
+    private FormValues build() {
+        for (Element input : form.select("button, input, select, textarea")) {
+            String type = input.attr("type");
+
+            if (input.hasAttr("disabled")) continue;
+            if (input.tagName().equalsIgnoreCase("button") && 
!type.equals("submit")) continue;
+            if (input.tagName().equalsIgnoreCase("input") && 
(type.equals("button") || type.equals("reset"))) continue;
+            if (type.equals("checkbox") && input.hasAttr("checked")) continue;
+            if (type.equals("radio") && input.hasAttr("checked")) continue;
+            if (!type.equals("image") && input.attr("name").length() == 0) 
continue;
+            if (input.parents().is("datalist")) continue;
+
+            if (type.equals("image") || type.equals("file")) continue; // 
don't support files for now
+            String name = input.attr("name");
+
+            if (input.tagName().equalsIgnoreCase("select")) {
+                for (Element o : input.select("option[selected]")) {
+                    if (o.hasAttr("disabled")) continue;
+                    list.add(name, new BasicNameValuePair(name, o.val()));
+                }
+            } else if (type.equals("checkbox") || type.equals("radio")) {
+                String value = input.hasAttr("value") ? input.val() : "on";
+                list.add(name, new BasicNameValuePair(name, value));
+            } else {
+                list.add(name, new BasicNameValuePair(name, input.val()));
+            }
+        }
+        return this;
+    }
+
+    private FormValues resolve() {
+        for (NameValuePair o : values) {
+            if (list.has(o.getName())) {
+                list.set(o);
+            } else {
+                // for now just set the value even if the form doesn't have a 
submittable input for the name.
+                // this is to support custom field that generate input 
dynamically
+                list.set(o);
+            }
+        }
+        return this;
+    }
+
+    public String toString() {
+        return URLEncodedUtils.format(list.flatten(), "UTF-8");
+    }
+
+    public HttpEntity toUrlEncodedEntity() {
+        try {
+            return new UrlEncodedFormEntity(list.flatten(), "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public HttpEntity toMultipartEntity() {
+        MultipartEntityBuilder eb = MultipartEntityBuilder.create();
+        for (NameValuePair p : list.flatten()) {
+            eb.addTextBody(p.getName(), p.getValue());
+        }
+        return eb.build();
+    }
+}

Added: 
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/impl/Vals.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/impl/Vals.java?rev=1755732&view=auto
==============================================================================
--- 
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/impl/Vals.java
 (added)
+++ 
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/impl/Vals.java
 Wed Aug 10 12:39:52 2016
@@ -0,0 +1,60 @@
+/*******************************************************************************
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.sling.hapi.client.forms.impl;
+
+import org.apache.http.NameValuePair;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+public class Vals {
+    private Map<String, List<NameValuePair>> data = new LinkedHashMap<String, 
List<NameValuePair>>();
+
+    public void add(String name, NameValuePair pair) {
+        if (data.containsKey(name)) {
+            data.get(name).add(pair);
+        } else {
+            ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
+            list.add(pair);
+            data.put(name, list);
+        }
+    }
+
+    public boolean has(String name) {
+        return data.containsKey(name);
+    }
+
+    public void set(NameValuePair pair) {
+        ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
+        list.add(pair);
+        data.put(pair.getName(), list);
+    }
+
+    public List<? extends NameValuePair> flatten() {
+        List<NameValuePair> result = new ArrayList<NameValuePair>();
+
+        for (List<NameValuePair> c : data.values()) {
+            result.addAll(c);
+        }
+
+        return result;
+    }
+}

Added: 
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/impl/package-info.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/impl/package-info.java?rev=1755732&view=auto
==============================================================================
--- 
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/impl/package-info.java
 (added)
+++ 
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/forms/impl/package-info.java
 Wed Aug 10 12:39:52 2016
@@ -0,0 +1,23 @@
+/*******************************************************************************
+ * 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.
+ 
******************************************************************************/
+
+@Version("1.0.0")
+package org.apache.sling.hapi.client.forms.impl;
+
+import aQute.bnd.annotation.Version;

Modified: 
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/impl/microdata/MicrodataDocument.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/impl/microdata/MicrodataDocument.java?rev=1755732&r1=1755731&r2=1755732&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/impl/microdata/MicrodataDocument.java
 (original)
+++ 
sling/trunk/contrib/extensions/hapi/client/src/main/java/org/apache/sling/hapi/client/impl/microdata/MicrodataDocument.java
 Wed Aug 10 12:39:52 2016
@@ -21,7 +21,7 @@ package org.apache.sling.hapi.client.imp
 
 import org.apache.http.NameValuePair;
 import org.apache.sling.hapi.client.*;
-import org.apache.sling.hapi.client.forms.FormValues;
+import org.apache.sling.hapi.client.forms.impl.FormValues;
 import org.jsoup.Jsoup;
 import org.jsoup.nodes.Element;
 import org.jsoup.select.Elements;


Reply via email to