myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lof...@apache.org
Subject [myfaces-tobago] branch master updated: TOBAGO-1943: Resources in tobago-config.xml should be possible to order
Date Thu, 18 Oct 2018 09:54:54 GMT
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/master by this push:
     new 6a1812f  TOBAGO-1943: Resources in tobago-config.xml should be possible to order
6a1812f is described below

commit 6a1812fa05571ba96bff4f7d5b6075c54ded9fbb
Author: Udo Schnurpfeil <lofwyr@apache.org>
AuthorDate: Thu Oct 18 11:54:33 2018 +0200

    TOBAGO-1943: Resources in tobago-config.xml should be possible to order
---
 .../apache/myfaces/tobago/context/ThemeImpl.java   | 27 +++----
 .../myfaces/tobago/context/ThemeResource.java      |  9 +++
 .../tobago/internal/config/TobagoConfigParser.java | 24 ++++--
 .../myfaces/tobago/config/tobago-config-5.0.xsd    |  2 +
 .../config/TobagoConfigMergingUnitTest.java        | 26 +++++++
 .../config/TobagoConfigParserUnitTest.java         |  8 +-
 .../test/resources/tobago-config-5.0-replace.xml   | 54 ++++++++++++++
 .../src/test/resources/tobago-config-5.0.xml       | 87 ++++++++++++++++++++++
 8 files changed, 210 insertions(+), 27 deletions(-)

diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/context/ThemeImpl.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/context/ThemeImpl.java
index c1605d8..b39fe5e 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/context/ThemeImpl.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/context/ThemeImpl.java
@@ -25,6 +25,7 @@ import org.slf4j.LoggerFactory;
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 
 public class ThemeImpl implements Theme, Serializable {
@@ -158,24 +159,16 @@ public class ThemeImpl implements Theme, Serializable {
 
   public void init() {
     checkLocked();
-    productionScripts = new String[productionResources.getScriptList().size()];
-    for (int i = 0; i < productionResources.getScriptList().size(); i++) {
-      productionScripts[i] = productionResources.getScriptList().get(i).getName();
-    }
-    productionStyles = new String[productionResources.getStyleList().size()];
-    for (int i = 0; i < productionResources.getStyleList().size(); i++) {
-      productionStyles[i] = productionResources.getStyleList().get(i).getName();
-    }
-
-    scripts = new String[resources.getScriptList().size()];
-    for (int i = 0; i < resources.getScriptList().size(); i++) {
-      scripts[i] = resources.getScriptList().get(i).getName();
-    }
-    styles = new String[resources.getStyleList().size()];
-    for (int i = 0; i < resources.getStyleList().size(); i++) {
-      styles[i] = resources.getStyleList().get(i).getName();
-    }
+    productionScripts = sort(productionResources.getScriptList());
+    productionStyles = sort(productionResources.getStyleList());
+    scripts = sort(resources.getScriptList());
+    styles = sort(resources.getStyleList());
+  }
 
+  private String[] sort(List<? extends ThemeResource> list) {
+    final List<ThemeResource> copy = new ArrayList<>(list);
+    copy.sort(Comparator.comparingInt(ThemeResource::getPriority));
+    return copy.stream().map(ThemeResource::getName).toArray(String[]::new);
   }
 
   @Override
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/context/ThemeResource.java
b/tobago-core/src/main/java/org/apache/myfaces/tobago/context/ThemeResource.java
index 927ff8b..3b22eed 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/context/ThemeResource.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/context/ThemeResource.java
@@ -27,6 +27,7 @@ import java.io.Serializable;
 public class ThemeResource implements Serializable {
 
   private String name;
+  private int priority;
 
   public String getName() {
     return name;
@@ -36,6 +37,14 @@ public class ThemeResource implements Serializable {
     this.name = name;
   }
 
+  public int getPriority() {
+    return priority;
+  }
+
+  public void setPriority(int priority) {
+    this.priority = priority;
+  }
+
   @Override
   public boolean equals(final Object o) {
     if (this == o) {
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParser.java
b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParser.java
index 5fd55ea..3e387ad 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParser.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParser.java
@@ -104,6 +104,14 @@ public class TobagoConfigParser extends TobagoConfigEntityResolver {
   private static final int EXTENSION = -612557761;
   private static final int TYPE = 3575610;
 
+  private static final String ATTR_MODE = "mode";
+  private static final String ATTR_PRODUCTION = "production";
+  private static final String ATTR_NAME = "name";
+  private static final String ATTR_KEY = "key";
+  private static final String ATTR_PRIORITY = "priority";
+
+  private static final int MAX_PRIORITY = 65536;
+
   private TobagoConfigFragment tobagoConfig;
   private ThemeImpl currentTheme;
   private Boolean production;
@@ -181,7 +189,7 @@ public class TobagoConfigParser extends TobagoConfigEntityResolver {
         break;
 
       case CONTENT_SECURITY_POLICY:
-        final String mode = attributes.getValue("mode");
+        final String mode = attributes.getValue(ATTR_MODE);
         tobagoConfig.setContentSecurityPolicy(new ContentSecurityPolicy(mode));
         break;
 
@@ -191,7 +199,7 @@ public class TobagoConfigParser extends TobagoConfigEntityResolver {
         break;
 
       case RESOURCES:
-        production = Boolean.parseBoolean(attributes.getValue("production"));
+        production = Boolean.parseBoolean(attributes.getValue(ATTR_PRODUCTION));
         break;
 
       case EXCLUDES:
@@ -200,7 +208,9 @@ public class TobagoConfigParser extends TobagoConfigEntityResolver {
 
       case SCRIPT:
         final ThemeScript script = new ThemeScript();
-        script.setName(attributes.getValue("name"));
+        script.setName(attributes.getValue(ATTR_NAME));
+        final String scriptPriority = attributes.getValue(ATTR_PRIORITY);
+        script.setPriority(scriptPriority != null ? Integer.parseUnsignedInt(scriptPriority)
: MAX_PRIORITY);
         if (production) {
           currentTheme.getProductionResources().addScript(script, exclude);
         } else {
@@ -210,7 +220,9 @@ public class TobagoConfigParser extends TobagoConfigEntityResolver {
 
       case STYLE:
         final ThemeStyle style = new ThemeStyle();
-        style.setName(attributes.getValue("name"));
+        style.setName(attributes.getValue(ATTR_NAME));
+        final String stylePriority = attributes.getValue(ATTR_PRIORITY);
+        style.setPriority(stylePriority != null ? Integer.parseUnsignedInt(stylePriority)
: MAX_PRIORITY);
         if (production) {
           currentTheme.getProductionResources().addStyle(style, exclude);
         } else {
@@ -223,11 +235,11 @@ public class TobagoConfigParser extends TobagoConfigEntityResolver {
         break;
 
       case ENTRY:
-        entryKey = attributes.getValue("key");
+        entryKey = attributes.getValue(ATTR_KEY);
         break;
 
       case DIRECTIVE:
-        directiveName = attributes.getValue("name");
+        directiveName = attributes.getValue(ATTR_NAME);
         break;
 
       case NAME:
diff --git a/tobago-core/src/main/resources/org/apache/myfaces/tobago/config/tobago-config-5.0.xsd
b/tobago-core/src/main/resources/org/apache/myfaces/tobago/config/tobago-config-5.0.xsd
index 34debb7..977b8aa 100644
--- a/tobago-core/src/main/resources/org/apache/myfaces/tobago/config/tobago-config-5.0.xsd
+++ b/tobago-core/src/main/resources/org/apache/myfaces/tobago/config/tobago-config-5.0.xsd
@@ -364,10 +364,12 @@
 
   <xs:complexType name="script-type">
     <xs:attribute name="name" type="xs:string" use="required"/>
+    <xs:attribute name="priority" type="xs:unsignedShort"/>
   </xs:complexType>
 
   <xs:complexType name="style-type">
     <xs:attribute name="name" type="xs:string" use="required"/>
+    <xs:attribute name="priority" type="xs:unsignedShort"/>
   </xs:complexType>
 
   <xs:simpleType name="name-type">
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigMergingUnitTest.java
b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigMergingUnitTest.java
index a9566bc..057a806 100644
--- a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigMergingUnitTest.java
+++ b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigMergingUnitTest.java
@@ -133,6 +133,32 @@ public class TobagoConfigMergingUnitTest {
     Assert.assertEquals("test/three", mimeTypes.get("test-3"));
   }
 
+  @Test
+  public void testResourcePriority()
+      throws IOException, SAXException, ParserConfigurationException, URISyntaxException
{
+
+    final TobagoConfigImpl config = loadAndMerge(
+        "tobago-config-5.0.xml", "tobago-config-5.0-replace.xml");
+
+    final String[] expected = new String[] {
+        "script-1.js",
+        "script-2.js",
+        "script-3-replacement.js",
+        "script-4.js",
+        "script-5.js",
+        "script-undefined-a.js",
+        "script-undefined-b.js",
+        "script-undefined-c.js",
+        "script-undefined-d.js",
+        "script-undefined-e.js"
+    };
+
+    config.resolveThemes();
+    final String[] scripts = config.getDefaultTheme().getScriptResources(true);
+
+    Assert.assertArrayEquals(expected, scripts);
+  }
+
   public static TobagoConfigImpl loadAndMerge(final String... names)
       throws IOException, SAXException, ParserConfigurationException, URISyntaxException
{
 
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParserUnitTest.java
b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParserUnitTest.java
index 7f7d5f8..9b73065 100644
--- a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParserUnitTest.java
+++ b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParserUnitTest.java
@@ -19,8 +19,8 @@
 
 package org.apache.myfaces.tobago.internal.config;
 
-import org.junit.Assert;
 import org.apache.myfaces.tobago.context.ThemeImpl;
+import org.junit.Assert;
 import org.junit.Test;
 import org.xml.sax.SAXException;
 
@@ -64,9 +64,9 @@ public class TobagoConfigParserUnitTest {
     Assert.assertEquals("my-before-1", fragment.getBefore().get(0));
     Assert.assertEquals("my-before-2", fragment.getBefore().get(1));
 
-    Assert.assertEquals(false, fragment.getCreateSessionSecret().booleanValue());
-    Assert.assertEquals(false, fragment.getCheckSessionSecret().booleanValue());
-    Assert.assertEquals(false, fragment.getPreventFrameAttacks().booleanValue());
+    Assert.assertFalse(fragment.getCreateSessionSecret());
+    Assert.assertFalse(fragment.getCheckSessionSecret());
+    Assert.assertFalse(fragment.getPreventFrameAttacks());
 
     final Map<String, String> directiveMap = fragment.getContentSecurityPolicy().getDirectiveMap();
     final Set<Map.Entry<String, String>> entries = directiveMap.entrySet();
diff --git a/tobago-core/src/test/resources/tobago-config-5.0-replace.xml b/tobago-core/src/test/resources/tobago-config-5.0-replace.xml
new file mode 100644
index 0000000..c059526
--- /dev/null
+++ b/tobago-core/src/test/resources/tobago-config-5.0-replace.xml
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ * 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.
+-->
+
+<tobago-config
+    xmlns="http://myfaces.apache.org/tobago/tobago-config"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://myfaces.apache.org/tobago/tobago-config http://myfaces.apache.org/tobago/tobago-config-5.0.xsd"
+    version="5.0">
+
+  <name>my-name-replace</name>
+
+  <ordering>
+    <after>
+      <name>my-name</name>
+    </after>
+  </ordering>
+
+  <theme-config>
+    <default-theme>my-theme-3</default-theme>
+  </theme-config>
+
+  <theme-definitions>
+    <theme-definition>
+      <name>my-theme-3</name>
+      <fallback>my-theme-1</fallback>
+      <resources production="true">
+        <includes>
+          <script name="script-3-replacement.js" priority="3"/>
+        </includes>
+        <excludes>
+          <script name="script-3.js"/>
+        </excludes>
+      </resources>
+    </theme-definition>
+
+  </theme-definitions>
+
+</tobago-config>
diff --git a/tobago-core/src/test/resources/tobago-config-5.0.xml b/tobago-core/src/test/resources/tobago-config-5.0.xml
new file mode 100644
index 0000000..7da951b
--- /dev/null
+++ b/tobago-core/src/test/resources/tobago-config-5.0.xml
@@ -0,0 +1,87 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ * 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.
+-->
+
+<tobago-config
+    xmlns="http://myfaces.apache.org/tobago/tobago-config"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://myfaces.apache.org/tobago/tobago-config http://myfaces.apache.org/tobago/tobago-config-5.0.xsd"
+    version="5.0">
+
+  <name>my-name</name>
+
+  <ordering>
+    <after>
+      <name>my-after</name>
+    </after>
+    <before>
+      <name>my-before-1</name>
+      <name>my-before-2</name>
+    </before>
+  </ordering>
+
+  <theme-config>
+    <default-theme>my-theme-1</default-theme>
+    <supported-theme>my-theme-2</supported-theme>
+  </theme-config>
+
+  <create-session-secret>false</create-session-secret>
+  <check-session-secret>false</check-session-secret>
+  <prevent-frame-attacks>false</prevent-frame-attacks>
+  <set-nosniff-header>false</set-nosniff-header>
+
+  <content-security-policy mode="on">
+    <directive name="default-src">'self'</directive>
+    <directive name="child-src"> http://apache.org</directive>
+  </content-security-policy>
+
+  <security-annotation>ignore</security-annotation>
+
+  <theme-definitions>
+    <theme-definition>
+      <name>my-theme-1</name>
+      <display-name>My Theme 1</display-name>
+      <versioned>true</versioned>
+      <resources production="true">
+        <includes>
+          <script name="script-1.js" priority="1"/>
+          <script name="script-undefined-a.js"/>
+          <script name="script-4.js" priority="4"/>
+          <script name="script-undefined-b.js"/>
+          <script name="script-3.js" priority="3"/>
+          <script name="script-undefined-c.js"/>
+          <script name="script-5.js" priority="5"/>
+          <script name="script-undefined-d.js"/>
+          <script name="script-2.js" priority="2"/>
+          <script name="script-undefined-e.js"/>
+          <style name="style-2.css" priority="2"/>
+          <style name="style.css"/>
+          <style name="style-1.css" priority="1"/>
+        </includes>
+      </resources>
+    </theme-definition>
+
+    <theme-definition>
+      <name>my-theme-2</name>
+      <fallback>my-theme-1</fallback>
+      <resources production="false"/>
+    </theme-definition>
+
+  </theme-definitions>
+
+</tobago-config>


Mime
View raw message