Skip to content

Commit

Permalink
PIVOT-949: merge from trunk for first part of the fix (add the fallba…
Browse files Browse the repository at this point in the history
…ck, but without Java 7 specific features here)

git-svn-id: https://svn.apache.org/repos/asf/pivot/branches/2.0.x@1690105 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Sandro Martini committed Jul 9, 2015
1 parent 49b5b01 commit dd4c90b
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 4 deletions.
12 changes: 8 additions & 4 deletions core/src/org/apache/pivot/beans/BXMLSerializer.java
Expand Up @@ -252,6 +252,7 @@ public Object evaluate(final Object value) {
public static final char URL_PREFIX = '@';
public static final char RESOURCE_KEY_PREFIX = '%';
public static final char OBJECT_REFERENCE_PREFIX = '$';
public static final char SLASH_PREFIX = '/';

public static final String NAMESPACE_BINDING_PREFIX = OBJECT_REFERENCE_PREFIX + "{";
public static final String NAMESPACE_BINDING_SUFFIX = "}";
Expand Down Expand Up @@ -886,7 +887,7 @@ private void processStartElement() throws IOException, SerializationException {

// Determine location from src attribute
URL locationLocal;
if (src.charAt(0) == '/') {
if (src.charAt(0) == SLASH_PREFIX) {
locationLocal = classLoader.getResource(src.substring(1));
} else {
locationLocal = new URL(this.location, src);
Expand Down Expand Up @@ -1327,8 +1328,8 @@ private void processEndElement() throws SerializationException {

case SCRIPT: {
String src = null;
if (element.properties.containsKey(INCLUDE_SRC_ATTRIBUTE)) {
src = element.properties.get(INCLUDE_SRC_ATTRIBUTE);
if (element.properties.containsKey(SCRIPT_SRC_ATTRIBUTE)) {
src = element.properties.get(SCRIPT_SRC_ATTRIBUTE);
}

if (src != null) {
Expand All @@ -1350,8 +1351,11 @@ private void processEndElement() throws SerializationException {

try {
URL scriptLocation;
if (src.charAt(0) == '/') {
if (src.charAt(0) == SLASH_PREFIX) {
scriptLocation = classLoader.getResource(src.substring(1));
if (scriptLocation == null) { // add a fallback
scriptLocation = new URL(location, src.substring(1));
}
} else {
scriptLocation = new URL(location, src);
}
Expand Down
58 changes: 58 additions & 0 deletions tests/src/org/apache/pivot/tests/issues/pivot949/Pivot949.java
@@ -0,0 +1,58 @@
/*
* 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.pivot.tests.issues.pivot949;

import org.apache.pivot.beans.BXMLSerializer;
import org.apache.pivot.collections.Map;
import org.apache.pivot.wtk.Application;
import org.apache.pivot.wtk.DesktopApplicationContext;
import org.apache.pivot.wtk.Display;
import org.apache.pivot.wtk.Window;

public class Pivot949 extends Application.Adapter {

private Window window = null;

@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
System.out.println("Pivot949 startup(...)");
System.out.println("\n"
+ "Note that this issue is related to include scripts (with absolute path) "
+ "from a bxml file.\n"
+ "Here we'll test both relative and absolute scripts.\n"
+ "\n");

BXMLSerializer bxmlSerializer = new BXMLSerializer();
window = (Window) bxmlSerializer.readObject(Pivot949.class, "pivot_949.bxml");
System.out.println("got window = " + window);
window.open(display);
}

@Override
public boolean shutdown(boolean b) throws Exception {
if (window != null) {
window.close();
}

return false;
}

public static void main(String[] args) {
DesktopApplicationContext.main(Pivot949.class, args);
}

}
57 changes: 57 additions & 0 deletions tests/src/org/apache/pivot/tests/issues/pivot949/pivot_949.bxml
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
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.
-->

<Window title="Pivot-949" maximized="true"
bxml:id="window"
xmlns:bxml="http://pivot.apache.org/bxml"
xmlns:app="org.apache.pivot.tests.issues"
xmlns:content="org.apache.pivot.wtk.content"
xmlns="org.apache.pivot.wtk"
>

<!-- <bxml:script src="utilities_script.js"/> // ok if it's in the same package/folder -->
<!-- <bxml:script src="../../utilities_script.js"/> // ok with a relative package/folder -->
<bxml:script src="../../utilities_script.js"/>

<bxml:script>
<![CDATA[
// just as a sample, to use the JavaScript just defined in the included file ...
log("inline script 1 - just loaded a script with a relative URI");
var nextURI = '/pivot_949.js';
log("inline script 1 - now try to load another script, but with an absolute URI '"
+ nextURI + "' ...");
]]>
</bxml:script>


<!-- <bxml:script src="pivot_949.js"/> // ok if it's in the same package/folder -->
<!-- <bxml:script src="./pivot_949.js"/> // ok if it's in the same package/folder -->
<bxml:script src="/pivot_949.js"/>
<!-- <bxml:script src="$nextURI"/> // TODO: later ... -->

<Label text="$foo"/>

<bxml:script>
<![CDATA[
// just as a sample, to use the JavaScript just defined in the included file ...
log("inline script 2 - all is ok");
]]>
</bxml:script>

</Window>
23 changes: 23 additions & 0 deletions tests/src/org/apache/pivot/tests/issues/pivot949/pivot_949.js
@@ -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.
*/
importPackage(java.lang); // required to use System.out and System.err
importPackage(org.apache.pivot.collections); // required to use Pivot class ArrayList and other collections
importPackage(org.apache.pivot.util); // required to use Pivot Utility class Console
importPackage(org.apache.pivot.wtk); // required to use Pivot WTK classes


var foo = 'Hello from JavaScript in the JVM';

0 comments on commit dd4c90b

Please sign in to comment.