This is an automated email from the ASF dual-hosted git repository.

dklco pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit 5ebdd9e1cb5d178173a173cdfcbb98feb77f8221
Author: Dan Klco <[email protected]>
AuthorDate: Fri May 11 16:25:33 2018 -0400

    Adding rewriter classes and fixing the template / config editor
---
 .../sling/cms/core/rewriter/HTML5Serializer.java   | 159 +++++++++++++++++++++
 .../cms/core/rewriter/Html5SerializerFactory.java  |  44 ++++++
 .../core/rewriter/ReferenceMappingTransformer.java |   7 +-
 .../components/cms/rewriterconfig/edit.json        |  25 ++++
 .../rewriterconfig.jsp}                            |  27 ++--
 .../{template => templateeditor}/config/config.jsp |   0
 .../{template => templateeditor}/config/edit.json  |   0
 .../templateeditor.jsp}                            |   2 +-
 8 files changed, 250 insertions(+), 14 deletions(-)

diff --git 
a/cms/core/src/main/java/org/apache/sling/cms/core/rewriter/HTML5Serializer.java
 
b/cms/core/src/main/java/org/apache/sling/cms/core/rewriter/HTML5Serializer.java
new file mode 100644
index 0000000..67062f5
--- /dev/null
+++ 
b/cms/core/src/main/java/org/apache/sling/cms/core/rewriter/HTML5Serializer.java
@@ -0,0 +1,159 @@
+/*
+ * 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 org.apache.sling.cms.core.rewriter;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.caconfig.resource.ConfigurationResourceResolver;
+import org.apache.sling.rewriter.ProcessingComponentConfiguration;
+import org.apache.sling.rewriter.ProcessingContext;
+import org.apache.sling.rewriter.Serializer;
+import org.xml.sax.Attributes;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+
+/**
+ * Serializer for writing HTML5 compliant markup
+ *
+ */
+public class HTML5Serializer implements Serializer {
+
+       private static final int CHAR_EQ = 61;
+
+       private static final int CHAR_GT = 62;
+
+       private static final int CHAR_SP = 32;
+
+       private static final int CHAR_LT = 60;
+
+       private final Set<String> emptyTags = new HashSet<String>() {
+               private static final long serialVersionUID = 1L;
+               {
+                       add("br");
+                       add("area");
+                       add("link");
+                       add("img");
+                       add("param");
+                       add("hr");
+                       add("input");
+                       add("col");
+                       add("base");
+                       add("meta");
+               }
+       };
+
+       private PrintWriter writer;
+
+       private ConfigurationResourceResolver resolver;
+
+       private Resource rewriteConfig;
+
+       public HTML5Serializer(ConfigurationResourceResolver resolver) {
+               this.resolver = resolver;
+       }
+
+       public void characters(char buffer[], int offset, int length) throws 
SAXException {
+               if (length == 0) {
+                       writer.flush();
+               } else {
+                       writer.write(buffer, offset, length);
+               }
+       }
+
+       @Override
+       public void dispose() {
+               // TODO Auto-generated method stub
+
+       }
+
+       public void endDocument() throws SAXException {
+               writer.flush();
+       }
+
+       public void endElement(String uri, String localName, String name) 
throws SAXException {
+               if (!emptyTags.contains(localName)) {
+                       writer.write("</");
+                       writer.write(localName);
+                       writer.write(CHAR_GT);
+               }
+       }
+
+       public void endPrefixMapping(String s) throws SAXException {
+       }
+
+       public void ignorableWhitespace(char ac[], int i, int j) throws 
SAXException {
+       }
+
+       @Override
+       public void init(ProcessingContext context, 
ProcessingComponentConfiguration config) throws IOException {
+               if (context.getWriter() == null) {
+                       throw new IllegalArgumentException("Failed to 
initialize HTML5Serializer, null writer specified!");
+               } else {
+                       writer = context.getWriter();
+                       rewriteConfig = 
resolver.getResource(context.getRequest().getResource(), "site", "rewrite");
+                       return;
+               }
+       }
+
+       public void processingInstruction(String s, String s1) throws 
SAXException {
+       }
+
+       public void setDocumentLocator(Locator locator1) {
+       }
+
+       public void skippedEntity(String s) throws SAXException {
+       }
+
+       public void startDocument() throws SAXException {
+               writer.println(rewriteConfig.getValueMap().get("doctype", 
String.class));
+       }
+
+       public void startElement(String uri, String localName, String name, 
Attributes atts) throws SAXException {
+               boolean endSlash = false;
+               writer.write(CHAR_LT);
+               writer.write(localName);
+
+               for (int i = 0; i < atts.getLength(); i++) {
+                       if ("endSlash".equals(atts.getQName(i))) {
+                               endSlash = true;
+                               continue;
+                       }
+                       writer.write(CHAR_SP);
+                       writer.write(atts.getLocalName(i));
+                       String value = atts.getValue(i);
+                       if (value == null) {
+                               continue;
+                       }
+                       writer.write(CHAR_EQ);
+                       writer.write('"');
+                       writer.write(value);
+                       writer.write('"');
+               }
+
+               if (endSlash) {
+                       writer.write("/");
+               }
+               writer.write(CHAR_GT);
+       }
+
+       public void startPrefixMapping(String s, String s1) throws SAXException 
{
+       }
+}
diff --git 
a/cms/core/src/main/java/org/apache/sling/cms/core/rewriter/Html5SerializerFactory.java
 
b/cms/core/src/main/java/org/apache/sling/cms/core/rewriter/Html5SerializerFactory.java
new file mode 100644
index 0000000..db8cdef
--- /dev/null
+++ 
b/cms/core/src/main/java/org/apache/sling/cms/core/rewriter/Html5SerializerFactory.java
@@ -0,0 +1,44 @@
+/*
+ * 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 org.apache.sling.cms.core.rewriter;
+
+import org.apache.sling.caconfig.resource.ConfigurationResourceResolver;
+import org.apache.sling.rewriter.Serializer;
+import org.apache.sling.rewriter.SerializerFactory;
+import org.osgi.framework.Constants;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+
+/**
+ * This sax serializer serializes HTML5 Compliant Markup
+ */
+@Component(service = SerializerFactory.class, property = { 
Constants.SERVICE_VENDOR + "=The Apache Software Foundation",
+               "pipeline.type=html5-serializer" })
+public class Html5SerializerFactory implements SerializerFactory {
+
+       @Reference
+       private ConfigurationResourceResolver resolver;
+
+       /**
+        * @see org.apache.sling.rewriter.SerializerFactory#createSerializer()
+        */
+       @Override
+       public Serializer createSerializer() {
+               return new HTML5Serializer(resolver);
+       }
+
+}
diff --git 
a/cms/core/src/main/java/org/apache/sling/cms/core/rewriter/ReferenceMappingTransformer.java
 
b/cms/core/src/main/java/org/apache/sling/cms/core/rewriter/ReferenceMappingTransformer.java
index 4438628..aeb4614 100644
--- 
a/cms/core/src/main/java/org/apache/sling/cms/core/rewriter/ReferenceMappingTransformer.java
+++ 
b/cms/core/src/main/java/org/apache/sling/cms/core/rewriter/ReferenceMappingTransformer.java
@@ -22,6 +22,7 @@ import org.apache.commons.lang.ArrayUtils;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.caconfig.resource.ConfigurationResourceResolver;
+import org.apache.sling.cms.CMSConstants;
 import org.apache.sling.rewriter.ProcessingComponentConfiguration;
 import org.apache.sling.rewriter.ProcessingContext;
 import org.apache.sling.rewriter.Transformer;
@@ -145,7 +146,11 @@ public class ReferenceMappingTransformer implements 
Transformer {
        public void init(ProcessingContext context, 
ProcessingComponentConfiguration cfg) throws IOException {
                log.trace("init");
                slingRequest = context.getRequest();
-               if (config != null && config.enabledPaths() != null) {
+
+               // make sure that the configuration is specified and that we're 
not currently in
+               // edit mode
+               if (config != null && config.enabledPaths() != null
+                               && 
!"true".equals(slingRequest.getAttribute(CMSConstants.ATTR_EDIT_ENABLED))) {
                        for (String enabledPath : config.enabledPaths()) {
                                if 
(slingRequest.getResource().getPath().startsWith(enabledPath)) {
                                        enabled = true;
diff --git 
a/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/rewriterconfig/edit.json
 
b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/rewriterconfig/edit.json
new file mode 100644
index 0000000..9f296f1
--- /dev/null
+++ 
b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/rewriterconfig/edit.json
@@ -0,0 +1,25 @@
+ {
+       "jcr:primaryType": "nt:unstructured",
+       "sling:resourceType": "sling-cms/components/editor/slingform",
+       "button": "Save Rewrite Config",
+       "fields": {
+               "jcr:primaryType": "nt:unstructured",
+               "sling:resourceType": "sling-cms/components/general/container",
+               "doctype": {
+                       "jcr:primaryType": "nt:unstructured",
+                       "sling:resourceType": 
"sling-cms/components/editor/fields/text",
+                       "label": "Doctype",
+                       "name": "doctype",
+                       "type": "nt:unstructured",
+                       "required": true
+               },
+               "attributes": {
+                       "jcr:primaryType": "nt:unstructured",
+                       "sling:resourceType": 
"sling-cms/components/editor/fields/repeating",
+                       "label": "Rewritten Attributes",
+                       "name": "attributes",
+                       "type": "text",
+                       "required": true
+               }
+       }
+}
\ No newline at end of file
diff --git 
a/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/template/template.jsp
 
b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/rewriterconfig/rewriterconfig.jsp
similarity index 61%
copy from 
cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/template/template.jsp
copy to 
cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/rewriterconfig/rewriterconfig.jsp
index 138a080..0e5a4c4 100644
--- 
a/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/template/template.jsp
+++ 
b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/rewriterconfig/rewriterconfig.jsp
@@ -17,15 +17,18 @@
  * under the License.
  */ --%>
  <%@include file="/libs/sling-cms/global.jsp"%>
- 
-<a 
href="/cms/config/edit.html${slingRequest.requestPathInfo.suffixResource.parent.parent.path}">
-       &lt; Site Configuration
-</a>
- <br/>
- <c:set var="cmsEditEnabled" value="true" scope="request" />
-<sling:call script="/libs/sling-cms/components/editor/scripts/init.jsp" />
-
-<sling:include path="${slingRequest.requestPathInfo.suffix}" 
resourceType="sling-cms/components/cms/template/config" />
-
-<sling:call script="/libs/sling-cms/components/editor/scripts/finalize.jsp" />
-<c:set var="cmsEditEnabled" value="false" scope="request" />
\ No newline at end of file
+<h3>Rewrite Configuration</h3>
+<dl>
+       <dt>Doctype</dt>
+       <dd>
+               <sling:encode value="${resource.valueMap.doctype}" mode="HTML" 
/>
+       </dd>
+       <dt>Rewritten Attributes</dt>
+       <dd>
+               <ul>
+                       <c:forEach var="attribute" 
items="${resource.valueMap.attributes}">
+                               <sling:encode value="${attribute}" mode="HTML" 
/>
+                       </c:forEach>
+               </ul>
+       </dd>
+</dl>
\ No newline at end of file
diff --git 
a/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/template/config/config.jsp
 
b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/templateeditor/config/config.jsp
similarity index 100%
rename from 
cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/template/config/config.jsp
rename to 
cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/templateeditor/config/config.jsp
diff --git 
a/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/template/config/edit.json
 
b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/templateeditor/config/edit.json
similarity index 100%
rename from 
cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/template/config/edit.json
rename to 
cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/templateeditor/config/edit.json
diff --git 
a/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/template/template.jsp
 
b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/templateeditor/templateeditor.jsp
similarity index 91%
rename from 
cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/template/template.jsp
rename to 
cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/templateeditor/templateeditor.jsp
index 138a080..dc281ed 100644
--- 
a/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/template/template.jsp
+++ 
b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/templateeditor/templateeditor.jsp
@@ -25,7 +25,7 @@
  <c:set var="cmsEditEnabled" value="true" scope="request" />
 <sling:call script="/libs/sling-cms/components/editor/scripts/init.jsp" />
 
-<sling:include path="${slingRequest.requestPathInfo.suffix}" 
resourceType="sling-cms/components/cms/template/config" />
+<sling:include path="${slingRequest.requestPathInfo.suffix}" 
resourceType="sling-cms/components/cms/templateeditor/config" />
 
 <sling:call script="/libs/sling-cms/components/editor/scripts/finalize.jsp" />
 <c:set var="cmsEditEnabled" value="false" scope="request" />
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
[email protected].

Reply via email to