Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better error messages when someone tries to get an invalid @@... subv…
…ariable of an XML DOM node (now it's not issued by the XPath implementation, which just sees it as a syntactical error). Some optimizations and cleanups regarding the matching of special keys (@@... and some more) in freemarker.ext.dom.
- Loading branch information
Showing
11 changed files
with
339 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 freemarker.ext.dom; | ||
|
||
/** | ||
* The special hash keys that start with "@@". | ||
*/ | ||
enum AtAtKey { | ||
|
||
MARKUP("@@markup"), | ||
NESTED_MARKUP("@@nested_markup"), | ||
ATTRIBUTES_MARKUP("@@attributes_markup"), | ||
TEXT("@@text"), | ||
START_TAG("@@start_tag"), | ||
END_TAG("@@end_tag"), | ||
QNAME("@@qname"), | ||
NAMESPACE("@@namespace"), | ||
LOCAL_NAME("@@local_name"), | ||
ATTRIBUTES("@@"), | ||
PREVIOUS_SIGNIFICANT("@@previous_significant"), | ||
NEXT_SIGNIFICANT("@@next_significant"); | ||
|
||
private final String key; | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
|
||
private AtAtKey(String key) { | ||
this.key = key; | ||
} | ||
|
||
public static boolean containsKey(String key) { | ||
for (AtAtKey item : AtAtKey.values()) { | ||
if (item.getKey().equals(key)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* 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 freemarker.ext.dom; | ||
|
||
import freemarker.core.Environment; | ||
import freemarker.template.Template; | ||
|
||
/** | ||
* For internal use only; don't depend on this, there's no backward compatibility guarantee at all! | ||
* This class is to work around the lack of module system in Java, i.e., so that other FreeMarker packages can | ||
* access things inside this package that users shouldn't. | ||
*/ | ||
final class DomStringUtil { | ||
|
||
private DomStringUtil() { | ||
// Not meant to be instantiated | ||
} | ||
|
||
static boolean isXMLID(String name) { | ||
return isXMLID(name, 0); | ||
} | ||
|
||
/** | ||
* Check if the subvariable name is just an element name, or a more complex XPath expression. | ||
* | ||
* @param firstCharIdx The index of the character in the string parameter that we treat as the beginning of the | ||
* string to check. This is to spare substringing that has become more expensive in Java 7. | ||
* | ||
* @return whether the name is a valid XML element name. (This routine might only be 99% accurate. REVISIT) | ||
*/ | ||
static boolean isXMLID(String name, int firstCharIdx) { | ||
int ln = name.length(); | ||
for (int i = firstCharIdx; i < ln; i++) { | ||
char c = name.charAt(i); | ||
if (i == firstCharIdx && (c == '-' || c == '.' || Character.isDigit(c))) { | ||
return false; | ||
} | ||
if (!Character.isLetterOrDigit(c) && c != '_' && c != '-' && c != '.') { | ||
if (c == ':') { | ||
if (i + 1 < ln && name.charAt(i + 1) == ':') { | ||
// "::" is used in XPath | ||
return false; | ||
} | ||
// We don't return here, as a lonely ":" is allowed. | ||
} else { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* @return whether the qname matches the combination of nodeName, nsURI, and environment prefix settings. | ||
*/ | ||
static boolean matchesName(String qname, String nodeName, String nsURI, Environment env) { | ||
String defaultNS = env.getDefaultNS(); | ||
if ((defaultNS != null) && defaultNS.equals(nsURI)) { | ||
return qname.equals(nodeName) | ||
|| qname.equals(Template.DEFAULT_NAMESPACE_PREFIX + ":" + nodeName); | ||
} | ||
if ("".equals(nsURI)) { | ||
if (defaultNS != null) { | ||
return qname.equals(Template.NO_NS_PREFIX + ":" + nodeName); | ||
} else { | ||
return qname.equals(nodeName) || qname.equals(Template.NO_NS_PREFIX + ":" + nodeName); | ||
} | ||
} | ||
String prefix = env.getPrefixForNamespace(nsURI); | ||
if (prefix == null) { | ||
return false; // Is this the right thing here??? | ||
} | ||
return qname.equals(prefix + ":" + nodeName); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.