Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,39 @@
* under the License.
*/

package grails.doc.internal;
package grails.doc.internal

import java.net.URI;
import java.net.URISyntaxException;
import groovy.transform.CompileStatic
import org.apache.commons.text.StringEscapeUtils

import org.apache.commons.text.StringEscapeUtils;
@CompileStatic
class StringEscapeCategory {

public class StringEscapeCategory {
private StringEscapeCategory() {
}

public static String encodeAsUrlPath(String str) {
static String encodeAsUrlPath(String str) {
try {
String uri = new URI("http", "localhost", '/' + str, "").toASCIIString();
return uri.substring(17, uri.length() - 1);
String uri = new URI('http', 'localhost', '/' + str, '').toASCIIString()
return uri.substring(17, uri.length() - 1)
}
catch (URISyntaxException ex) {
throw new RuntimeException(ex);
throw new RuntimeException(ex)
}
}

public static String encodeAsUrlFragment(String str) {
static String encodeAsUrlFragment(String str) {
try {
String uri = new URI("http", "localhost", "/", str).toASCIIString();
return uri.substring(18, uri.length());
String uri = new URI('http', 'localhost', '/', str).toASCIIString()
return uri.substring(18, uri.length())
}
catch (URISyntaxException ex) {
throw new RuntimeException(ex);
throw new RuntimeException(ex)
}
}

public static String encodeAsHtml(String str) {
return StringEscapeUtils.escapeHtml4(str);
static String encodeAsHtml(String str) {
return StringEscapeUtils.escapeHtml4(str)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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
*
* https://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 grails.doc.internal

import spock.lang.Specification

class StringEscapeCategorySpec extends Specification {

void 'empty and reserved characters are encoded consistently'() {
expect:
StringEscapeCategory.encodeAsUrlPath('') == ''
StringEscapeCategory.encodeAsUrlPath('a/b?c#d') == 'a/b%3Fc%23d'
StringEscapeCategory.encodeAsUrlFragment('') == ''
StringEscapeCategory.encodeAsUrlFragment('a/b?c#d') == 'a/b?c%23d'
StringEscapeCategory.encodeAsHtml('') == ''
StringEscapeCategory.encodeAsHtml('"quoted" & \'single\'') == '"quoted" & \'single\''
StringEscapeCategory.encodeAsHtml(null) == null
}

void 'the category is a static utility'() {
expect:
StringEscapeCategory.declaredConstructors.every { java.lang.reflect.Modifier.isPrivate(it.modifiers) }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.grails.gradle.common;
package org.apache.grails.gradle.common

/**
* Registered SAX and Xerces parser feature identifiers used to harden XML parsing.
Expand All @@ -36,7 +36,7 @@
*
* @since 8.0.0
*/
public enum XmlParserFeature {
enum XmlParserFeature {

/**
* Rejects any document carrying a {@code DOCTYPE} declaration.
Expand All @@ -47,22 +47,22 @@ public enum XmlParserFeature {
* {@code web.xml} — routinely carry a DOCTYPE, so their readers need a parser that leaves this
* disabled while keeping the entity and DTD features below switched off.
*/
DISALLOW_DOCTYPE_DECL("http://apache.org/xml/features/disallow-doctype-decl"),
DISALLOW_DOCTYPE_DECL('http://apache.org/xml/features/disallow-doctype-decl'),

/**
* Blocks resolution of external general entities, the primary XXE vector.
*/
EXTERNAL_GENERAL_ENTITIES("http://xml.org/sax/features/external-general-entities"),
EXTERNAL_GENERAL_ENTITIES('http://xml.org/sax/features/external-general-entities'),

/**
* Blocks resolution of external parameter entities.
*/
EXTERNAL_PARAMETER_ENTITIES("http://xml.org/sax/features/external-parameter-entities"),
EXTERNAL_PARAMETER_ENTITIES('http://xml.org/sax/features/external-parameter-entities'),

/**
* Stops the parser building a grammar from a DTD.
*/
LOAD_DTD_GRAMMAR("http://apache.org/xml/features/nonvalidating/load-dtd-grammar"),
LOAD_DTD_GRAMMAR('http://apache.org/xml/features/nonvalidating/load-dtd-grammar'),

/**
* Skips external DTD subsets instead of retrieving them.
Expand All @@ -71,24 +71,24 @@ public enum XmlParserFeature {
* raises an error when a document references an external DTD. Skipping is what allows a
* descriptor that names a DTD, such as a JSP 1.2 tag library, to parse without retrieving it.
*/
LOAD_EXTERNAL_DTD("http://apache.org/xml/features/nonvalidating/load-external-dtd");
LOAD_EXTERNAL_DTD('http://apache.org/xml/features/nonvalidating/load-external-dtd')

private final String featureName;
private final String featureName

XmlParserFeature(String featureName) {
this.featureName = featureName;
this.featureName = featureName
}

/**
* @return the registered identifier to pass to {@code setFeature}
*/
public String getFeatureName() {
return featureName;
String getFeatureName() {
return featureName
}

@Override
public String toString() {
return featureName;
String toString() {
return featureName
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,52 @@
* specific language governing permissions and limitations
* under the License.
*/
package grails.util;
package grails.util

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import groovy.transform.CompileStatic

/**
* Collection utility methods.
*
* @author Burt Beckwith
*/
public class CollectionUtils {
@CompileStatic
class CollectionUtils {

private CollectionUtils() {
// static only
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public static <K, V> Map newMap(Object... keysAndValues) {
@SuppressWarnings([ 'rawtypes', 'unchecked' ])
static <K, V> Map newMap(Object... keysAndValues) {
if (keysAndValues == null) {
return Collections.emptyMap();
return Collections.emptyMap()
}
if (keysAndValues.length % 2 == 1) {
throw new IllegalArgumentException("Must have an even number of keys and values");
throw new IllegalArgumentException('Must have an even number of keys and values')
}

Map<K, V> map = new HashMap<>();
Map<K, V> map = new HashMap<>()
for (int i = 0; i < keysAndValues.length; i += 2) {
map.put((K) keysAndValues[i], (V) keysAndValues[i + 1]);
map.put((K) keysAndValues[i], (V) keysAndValues[i + 1])
}
return map;
return map
}

public static <T> Set<T> newSet(T... values) {
static <T> Set<T> newSet(T... values) {
if (values == null) {
return Collections.emptySet();
return Collections.emptySet()
}

return new HashSet<>(Arrays.asList(values));
return new HashSet<>(Arrays.asList(values))
}

public static <T> List<T> newList(T... values) {
static <T> List<T> newList(T... values) {
if (values == null) {
return Collections.emptyList();
return Collections.emptyList()
}

return new ArrayList<>(Arrays.asList(values));
return new ArrayList<>(Arrays.asList(values))
}

/**
Expand All @@ -78,11 +71,12 @@ public static <T> List<T> newList(T... values) {
* @param key The key that holds the child map
* @return The child map
*/
public static Map getOrCreateChildMap(Map parent, String key) {
Object o = parent.get(key);
static Map getOrCreateChildMap(Map parent, String key) {
Object o = parent[key]
if (o instanceof Map) {
return (Map) o;
return o
}
return new LinkedHashMap();
return [:]
}

}
Loading