You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2025/03/31 19:56:31 UTC

(myfaces-tobago) branch main updated: demo: cleanup and simplify (#6217)

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

lofwyr pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git


The following commit(s) were added to refs/heads/main by this push:
     new c16e176ddb demo: cleanup and simplify (#6217)
c16e176ddb is described below

commit c16e176ddbe164b185965a14586d915d23f0acfc
Author: Udo Schnurpfeil <lo...@apache.org>
AuthorDate: Mon Mar 31 21:56:25 2025 +0200

    demo: cleanup and simplify (#6217)
    
    * cleanup and
    * show sources in demo without license
---
 .../tobago/example/demo/CkeditorController.java    |  6 +--
 .../tobago/example/demo/Migration4Controller.java  | 34 --------------
 .../tobago/example/demo/Migration5Controller.java  | 34 --------------
 .../tobago/example/demo/ObjectController.java      | 34 --------------
 .../tobago/example/demo/SourceFileReader.java      | 29 ++++++++++--
 .../tobago/example/demo/TinyMceController.java     |  6 +--
 .../50-migration/95-migration/4.0_to_5.0.xhtml     |  2 +-
 .../50-migration/96-migration/3.0_to_4.0.xhtml     |  2 +-
 .../content/020-output/60-object/Object.xhtml      |  2 +-
 .../content/110-wysiwyg/00-tinymce/TinyMCE.xhtml   |  2 +-
 .../content/110-wysiwyg/01-ckeditor/CKEditor.xhtml |  9 ++--
 .../example/demo/SourceFileReaderUnitTest.java     | 53 ++++++++++++++++++++++
 .../src/test/resources/simple.js                   | 18 ++++++++
 .../src/test/resources/simple.sh                   | 19 ++++++++
 14 files changed, 126 insertions(+), 124 deletions(-)

diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/CkeditorController.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/CkeditorController.java
index a105a7c158..3cb5152fd9 100644
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/CkeditorController.java
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/CkeditorController.java
@@ -26,7 +26,7 @@
 
 @SessionScoped
 @Named
-public class CkeditorController extends SourceFileReader implements Serializable {
+public class CkeditorController implements Serializable {
 
   private String text;
 
@@ -42,8 +42,4 @@ public String getText() {
   public void setText(final String text) {
     this.text = text;
   }
-
-  public String getSource() {
-    return getSource("init-ckeditor.js");
-  }
 }
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Migration4Controller.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Migration4Controller.java
deleted file mode 100644
index e08b0ccb6b..0000000000
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Migration4Controller.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * 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.myfaces.tobago.example.demo;
-
-import jakarta.inject.Named;
-import jakarta.inject.Singleton;
-
-import java.io.Serializable;
-
-@Singleton
-@Named
-public class Migration4Controller extends SourceFileReader implements Serializable {
-
-  public String getScriptSource() {
-    return getSource("migrate4-layout.sh");
-  }
-}
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Migration5Controller.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Migration5Controller.java
deleted file mode 100644
index d856703b87..0000000000
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Migration5Controller.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * 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.myfaces.tobago.example.demo;
-
-import jakarta.enterprise.context.ApplicationScoped;
-import jakarta.inject.Named;
-
-import java.io.Serializable;
-
-@ApplicationScoped
-@Named
-public class Migration5Controller extends SourceFileReader implements Serializable {
-
-  public String getJavaSource() {
-    return getSource("ConvertXmlToProperties.java");
-  }
-}
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/ObjectController.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/ObjectController.java
deleted file mode 100644
index 403d7ddf3a..0000000000
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/ObjectController.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * 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.myfaces.tobago.example.demo;
-
-import jakarta.enterprise.context.SessionScoped;
-import jakarta.inject.Named;
-
-import java.io.Serializable;
-
-@SessionScoped
-@Named
-public class ObjectController extends SourceFileReader implements Serializable {
-
-  public String getJavaScriptSource() {
-    return getSource("object.js");
-  }
-}
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SourceFileReader.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SourceFileReader.java
index 4e226fdf2f..a99d8d7ca4 100644
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SourceFileReader.java
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SourceFileReader.java
@@ -23,29 +23,48 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import jakarta.enterprise.context.ApplicationScoped;
 import jakarta.faces.context.ExternalContext;
 import jakarta.faces.context.FacesContext;
-
+import jakarta.inject.Named;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.invoke.MethodHandles;
 import java.nio.charset.StandardCharsets;
 
-public abstract class SourceFileReader {
+@ApplicationScoped
+@Named
+public class SourceFileReader {
 
   private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
-  public String getSource(final String filename) {
+  protected static final String JAVA = "(?s)/\\*.*?Licensed.*?\\*/";
+  protected static final String SHELL = "(?s)\n# Licensed.*License\\.\n";
+
+  public String getJavaSource(final String filename, final boolean filterLicense) {
+    return getSource(filename, filterLicense ? JAVA : null);
+  }
+
+  public String getShellSource(final String filename, final boolean filterLicense) {
+    return getSource(filename, filterLicense ? SHELL : null);
+  }
+
+  protected String getSource(final String filename, final String ignoreRegex) {
     final FacesContext facesContext = FacesContext.getCurrentInstance();
     final ExternalContext externalContext = facesContext.getExternalContext();
     final String viewId = facesContext.getViewRoot().getViewId();
     final String file = viewId.substring(0, viewId.lastIndexOf("/")) + "/" + filename;
     try (InputStream resourceAsStream = externalContext.getResourceAsStream(file)) {
       if (resourceAsStream != null) {
-        return IOUtils.toString(resourceAsStream, StandardCharsets.UTF_8);
+        final String string = IOUtils.toString(resourceAsStream, StandardCharsets.UTF_8);
+        if (ignoreRegex != null) {
+          return string.replaceAll(ignoreRegex, "");
+        } else {
+          return string;
+        }
       }
     } catch (final IOException e) {
-      LOG.error("", e);
+      LOG.error("Error while loading '" + filename + "'!", e);
     }
     return null;
   }
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/TinyMceController.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/TinyMceController.java
index 55c8cc878f..08dca4ea33 100644
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/TinyMceController.java
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/TinyMceController.java
@@ -26,7 +26,7 @@
 
 @SessionScoped
 @Named
-public class TinyMceController extends SourceFileReader implements Serializable {
+public class TinyMceController implements Serializable {
 
   private String text;
 
@@ -42,8 +42,4 @@ public String getText() {
   public void setText(final String text) {
     this.text = text;
   }
-
-  public String getSource() {
-    return getSource("init-tinymce.js");
-  }
 }
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/000-intro/50-migration/95-migration/4.0_to_5.0.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/content/000-intro/50-migration/95-migration/4.0_to_5.0.xhtml
index 49014c1762..e1774adc0c 100644
--- a/tobago-example/tobago-example-demo/src/main/webapp/content/000-intro/50-migration/95-migration/4.0_to_5.0.xhtml
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/000-intro/50-migration/95-migration/4.0_to_5.0.xhtml
@@ -42,7 +42,7 @@
         <p>Here is a simple <tc:link label="Java Class" link="ConvertXmlToProperties.java"/>,
           that may help you to change the layout attributes in the
           facelets XHTML files.</p>
-        <demo-highlight language="java"><tc:out value="#{migration5Controller.javaSource}" escape="true" keepLineBreaks="false"/></demo-highlight>
+        <demo-highlight language="java"><tc:out value="#{sourceFileReader.getJavaSource('ConvertXmlToProperties.java', true)}" escape="true" keepLineBreaks="false"/></demo-highlight>
       </tc:section>
 
       <tc:section label="TobagoContext: ResourceBundle Changes">
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/000-intro/50-migration/96-migration/3.0_to_4.0.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/content/000-intro/50-migration/96-migration/3.0_to_4.0.xhtml
index 3332c1f8da..2274578481 100644
--- a/tobago-example/tobago-example-demo/src/main/webapp/content/000-intro/50-migration/96-migration/3.0_to_4.0.xhtml
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/000-intro/50-migration/96-migration/3.0_to_4.0.xhtml
@@ -157,7 +157,7 @@
       <tc:section id="layout-script" label="Migration script for layout attributes">
         <p>Here is a simple <tc:link label="migration script" link="migrate4-layout.sh"/>, that may help you to change the layout attributes in the
           facelets XHTML files.</p>
-        <demo-highlight language="bash"><tc:out value="#{migration4Controller.scriptSource}"/></demo-highlight>
+        <demo-highlight language="bash"><tc:out value="#{sourceFileReader.getShellSource('migrate4-layout.sh', true)}"/></demo-highlight>
       </tc:section>
 
     </tc:section>
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/020-output/60-object/Object.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/content/020-output/60-object/Object.xhtml
index 4d248938bd..5fe726a160 100644
--- a/tobago-example/tobago-example-demo/src/main/webapp/content/020-output/60-object/Object.xhtml
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/020-output/60-object/Object.xhtml
@@ -72,7 +72,7 @@
           </tc:button>
         </tc:buttons>
       </f:facet>
-      <demo-highlight language="javascript"><tc:out value="#{objectController.javaScriptSource}"/></demo-highlight>
+      <demo-highlight language="javascript"><tc:out value="#{sourceFileReader.getJavaSource('object.js', true)}"/></demo-highlight>
     </tc:box>
     <tc:buttons id="t1">
       <tc:button id="c1" label="Show Tobago" omit="true">
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/110-wysiwyg/00-tinymce/TinyMCE.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/content/110-wysiwyg/00-tinymce/TinyMCE.xhtml
index 6d5c57f353..812d9f7b4f 100644
--- a/tobago-example/tobago-example-demo/src/main/webapp/content/110-wysiwyg/00-tinymce/TinyMCE.xhtml
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/110-wysiwyg/00-tinymce/TinyMCE.xhtml
@@ -65,7 +65,7 @@
           </tc:button>
         </tc:buttons>
       </f:facet>
-      <demo-highlight language="javascript"><tc:out value="#{tinyMceController.source}"/></demo-highlight>
+      <demo-highlight language="javascript"><tc:out value="#{sourceFileReader.getJavaSource('init-tinymce.js', true)}"/></demo-highlight>
     </tc:box>
 
     <tc:textarea value="#{tinyMceController.text}">
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/110-wysiwyg/01-ckeditor/CKEditor.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/content/110-wysiwyg/01-ckeditor/CKEditor.xhtml
index 237610a47a..6257fef4f5 100644
--- a/tobago-example/tobago-example-demo/src/main/webapp/content/110-wysiwyg/01-ckeditor/CKEditor.xhtml
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/110-wysiwyg/01-ckeditor/CKEditor.xhtml
@@ -31,7 +31,7 @@
       This demo uses the CDN of CKEditor as a showcase.
       There are other integration possibilities.
       For more information to setup see
-      <tc:link image="bi-box-arrow-up-right" link="https://ckeditor.com//" label="CKEditor"/>.
+      <tc:link image="bi-box-arrow-up-right" link="https://ckeditor.com/" label="CKEditor"/>.
     </p>
 
     <p>
@@ -42,12 +42,15 @@
 
     <p>
       The setup needs entries in <code>tobago-config.xml</code> for CSP.
+      The simple way would be, to turn it of with
+      <code class="language-markup">&lt;content-security-policy mode="on"&gt;</code>,
+      but this is not recommended for security reasons.
       More information about these entries you find also on their website.
     </p>
 
     <p>
       <tc:badge
-          value="Not working with CSP mode='on'! Can be turned off in tobago-config.xml, or configured correctly."
+          value="Not working with CSP mode='on'! Can be turned off or configured in tobago-config.xml."
           markup="warning"/>
     </p>
 
@@ -71,7 +74,7 @@
           </tc:button>
         </tc:buttons>
       </f:facet>
-      <demo-highlight language="javascript"><tc:out value="#{ckeditorController.source}"/></demo-highlight>
+      <demo-highlight language="javascript"><tc:out value="#{sourceFileReader.getJavaSource('init-ckeditor.js', true)}"/></demo-highlight>
     </tc:box>
 
     <tc:textarea value="#{ckeditorController.text}">
diff --git a/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/SourceFileReaderUnitTest.java b/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/SourceFileReaderUnitTest.java
new file mode 100644
index 0000000000..75fcb3155d
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/SourceFileReaderUnitTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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.myfaces.tobago.example.demo;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.util.stream.Collectors;
+
+public class SourceFileReaderUnitTest {
+
+  @Test
+  public void testJavaSource() throws IOException {
+    try (InputStream stream = SourceFileReader.class.getClassLoader().getResourceAsStream("simple.js")) {
+      BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
+      String content = reader.lines().collect(Collectors.joining("\n"));
+      final String result = content.replaceAll(SourceFileReader.JAVA, "").trim();
+      Assertions.assertEquals("console.log('simple');", result);
+    }
+  }
+
+  @Test
+  public void testShellSource() throws IOException {
+    try (InputStream stream = SourceFileReader.class.getClassLoader().getResourceAsStream("simple.sh")) {
+      BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
+      String content = reader.lines().collect(Collectors.joining("\n"));
+      final String result = content.replaceAll(SourceFileReader.SHELL, "").trim();
+      Assertions.assertEquals("#! /bin/bash\n\n# simple test\necho 'simple'", result);
+    }
+  }
+}
diff --git a/tobago-example/tobago-example-demo/src/test/resources/simple.js b/tobago-example/tobago-example-demo/src/test/resources/simple.js
new file mode 100644
index 0000000000..9ff48c9a02
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/test/resources/simple.js
@@ -0,0 +1,18 @@
+/*
+ * 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.
+ */
+
+console.log('simple');
diff --git a/tobago-example/tobago-example-demo/src/test/resources/simple.sh b/tobago-example/tobago-example-demo/src/test/resources/simple.sh
new file mode 100644
index 0000000000..bdc3a39531
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/test/resources/simple.sh
@@ -0,0 +1,19 @@
+#! /bin/bash
+
+# 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.
+
+# simple test
+echo 'simple'