// Path: ui/src/content/jcr_root/libs/foundation/src/impl/src/main/java/com/day/cq/wcm/foundation/forms/impl/FormsHandlingResponse.java
// public class FormsHandlingResponse extends SlingHttpServletResponseWrapper {
// 
//     private ServletOutputStream nullstream;
//     private PrintWriter nullwriter;
// 
//     public FormsHandlingResponse(SlingHttpServletResponse wrappedResponse) {
//         super(wrappedResponse);
//     }
// 
//     @Override
//     public PrintWriter getWriter() {
//         if (nullwriter == null) {
//             nullwriter = new PrintWriter(getOutputStream());
//         }
//         return nullwriter;
//     }
// 
//     @Override
//     public ServletOutputStream getOutputStream() {
//         if (nullstream == null) {
//             nullstream = new ServletOutputStream() {
// 
//                 @Override
//                 public void write(int b) {
//                     // noop
//                 }
// 
//                 @Override
//                 public void write(byte[] b) {
//                     // noop
//                 }
// 
//                 @Override
//                 public void write(byte[] b, int off, int len)
//                         {
//                     // noop
//                 }
// 
//                 @Override
//                 public void flush() {
//                     // noop
//                 }
// 
//                 @Override
//                 public void close() {
//                     // noop
//                 }
//             };
//         }
//         return nullstream;
//     }
// 
//     @Override
//     public void flushBuffer() {
//         // noop
//     }
// 
//     @Override
//     public void reset() {
//         // noop
//     }
// 
//     @Override
//     public void resetBuffer() {
//         // noop
//     }
// }
// 
// Path: ui/src/content/jcr_root/libs/foundation/src/impl/src/main/java/com/day/cq/wcm/foundation/forms/impl/ResourceWrapper.java
// public class ResourceWrapper extends org.apache.sling.api.resource.ResourceWrapper {
// 
//     private final String resourceType;
// 
//     private final String resourceSuperType;
// 
//     public ResourceWrapper(final Resource r, final String rt, final String rst) {
//         super(r);
//         this.resourceType = rt;
//         this.resourceSuperType = rst;
//     }
// 
//     public String getResourceSuperType() {
//         return this.resourceSuperType;
//     }
// 
//     public String getResourceType() {
//         return this.resourceType;
//     }
// }


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.sling.api.SlingException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import com.day.cq.wcm.foundation.forms.impl.FormsHandlingResponse;
import com.day.cq.wcm.foundation.forms.impl.ResourceWrapper;
        out.write(getConstraintMessage(desc, request));
        out.write("', i); return false;}}} else {" +
                  "if (!cq5forms_regcheck(obj.value, ");
        out.write(regexp);
        out.write(")) {" +
                  "cq5forms_showMsg('");
        out.write(StringEscapeUtils.escapeEcmaScript(FormsHelper.getFormId(request)));
        out.write("','");
        out.write(StringEscapeUtils.escapeEcmaScript(desc.getName()));
        out.write("','");
        out.write(StringEscapeUtils.escapeEcmaScript(getConstraintMessage(desc, request)));
        out.write("'); return false;}}}");
    }

    /**
     * Write the client java script code to check a constraint on
     * form submit.
     */
    public static void writeClientConstraintCheck(final SlingHttpServletRequest request,
                                                  final SlingHttpServletResponse response,
                                                  final FieldDescription        desc)
    throws IOException, ServletException {
        if ( desc.getConstraintType() != null ) {
            final PrintWriter out = response.getWriter();
            final String qualifier = getClientFieldQualifier(request, desc);
            out.write("if (!cq5forms_isEmpty(");
            out.write(qualifier);
            out.write(")){");
            try {
                request.setAttribute(ATTR_DESC, desc);
// Path: ui/src/content/jcr_root/libs/foundation/src/impl/src/main/java/com/day/cq/wcm/foundation/forms/impl/FormsHandlingResponse.java
// public class FormsHandlingResponse extends SlingHttpServletResponseWrapper {
// 
//     private ServletOutputStream nullstream;
//     private PrintWriter nullwriter;
// 
//     public FormsHandlingResponse(SlingHttpServletResponse wrappedResponse) {
//         super(wrappedResponse);
//     }
// 
//     @Override
//     public PrintWriter getWriter() {
//         if (nullwriter == null) {
//             nullwriter = new PrintWriter(getOutputStream());
//         }
//         return nullwriter;
//     }
// 
//     @Override
//     public ServletOutputStream getOutputStream() {
//         if (nullstream == null) {
//             nullstream = new ServletOutputStream() {
// 
//                 @Override
//                 public void write(int b) {
//                     // noop
//                 }
// 
//                 @Override
//                 public void write(byte[] b) {
//                     // noop
//                 }
// 
//                 @Override
//                 public void write(byte[] b, int off, int len)
//                         {
//                     // noop
//                 }
// 
//                 @Override
//                 public void flush() {
//                     // noop
//                 }
// 
//                 @Override
//                 public void close() {
//                     // noop
//                 }
//             };
//         }
//         return nullstream;
//     }
// 
//     @Override
//     public void flushBuffer() {
//         // noop
//     }
// 
//     @Override
//     public void reset() {
//         // noop
//     }
// 
//     @Override
//     public void resetBuffer() {
//         // noop
//     }
// }
// 
// Path: ui/src/content/jcr_root/libs/foundation/src/impl/src/main/java/com/day/cq/wcm/foundation/forms/impl/ResourceWrapper.java
// public class ResourceWrapper extends org.apache.sling.api.resource.ResourceWrapper {
// 
//     private final String resourceType;
// 
//     private final String resourceSuperType;
// 
//     public ResourceWrapper(final Resource r, final String rt, final String rst) {
//         super(r);
//         this.resourceType = rt;
//         this.resourceSuperType = rst;
//     }
// 
//     public String getResourceSuperType() {
//         return this.resourceSuperType;
//     }
// 
//     public String getResourceType() {
//         return this.resourceType;
//     }
// }


// Path: ui/src/content/jcr_root/libs/foundation/src/impl/src/main/java/com/day/cq/wcm/foundation/forms/FieldHelper.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.sling.api.SlingException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import com.day.cq.wcm.foundation.forms.impl.FormsHandlingResponse;
import com.day.cq.wcm.foundation.forms.impl.ResourceWrapper;

        out.write(getConstraintMessage(desc, request));
        out.write("', i); return false;}}} else {" +
                  "if (!cq5forms_regcheck(obj.value, ");
        out.write(regexp);
        out.write(")) {" +
                  "cq5forms_showMsg('");
        out.write(StringEscapeUtils.escapeEcmaScript(FormsHelper.getFormId(request)));
        out.write("','");
        out.write(StringEscapeUtils.escapeEcmaScript(desc.getName()));
        out.write("','");
        out.write(StringEscapeUtils.escapeEcmaScript(getConstraintMessage(desc, request)));
        out.write("'); return false;}}}");
    }

    /**
     * Write the client java script code to check a constraint on
     * form submit.
     */
    public static void writeClientConstraintCheck(final SlingHttpServletRequest request,
                                                  final SlingHttpServletResponse response,
                                                  final FieldDescription        desc)
    throws IOException, ServletException {
        if ( desc.getConstraintType() != null ) {
            final PrintWriter out = response.getWriter();
            final String qualifier = getClientFieldQualifier(request, desc);
            out.write("if (!cq5forms_isEmpty(");
            out.write(qualifier);
            out.write(")){");
            try {
                request.setAttribute(ATTR_DESC, desc);
                final Resource includeResource = new ResourceWrapper(desc.getFieldResource(),