Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] java improvements: #195

Merged
merged 1 commit into from
Jul 12, 2022
Merged
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 @@ -220,9 +220,8 @@ public String policyRoleTable()
s.append( " <colgroup span=\"" ).append( pageActionsLength * rolesLength ).append( "\" width=\"" ).append( colWidth ).append( "\" align=\"center\"/>\n" );
s.append( " <tr>\n" );
s.append( " <th rowspan=\"2\" valign=\"bottom\">Permission</th>\n" );
for( int i = 0; i < rolesLength; i++ )
{
s.append( " <th colspan=\"" ).append( pageActionsLength ).append( "\" title=\"" ).append( roles[i].getClass().getName() ).append( "\">" ).append( roles[i].getName() ).append( "</th>\n" );
for (final Principal principal : roles) {
s.append(" <th colspan=\"").append(pageActionsLength).append("\" title=\"").append(principal.getClass().getName()).append("\">").append(principal.getName()).append("</th>\n");
}
s.append( " </tr>\n" );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,9 @@ public class JDBCGroupDatabase implements GroupDatabase {
// Insert group member records
ps = conn.prepareStatement( m_insertGroupMembers );
final Principal[] members = group.members();
for( int i = 0; i < members.length; i++ )
{
final Principal member = members[i];
ps.setString( 1, group.getName() );
ps.setString( 2, member.getName() );
for (final Principal member : members) {
ps.setString(1, group.getName());
ps.setString(2, member.getName());
ps.execute();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,32 +238,19 @@ static int createMask(final String actions )
}
int mask = 0;
final String[] actionList = actions.split( "," );
for( int i = 0; i < actionList.length; i++ )
{
final String action = actionList[i];
if ( action.equalsIgnoreCase( CREATE_GROUPS_ACTION ) )
{
for (final String action : actionList) {
if (action.equalsIgnoreCase(CREATE_GROUPS_ACTION)) {
mask |= CREATE_GROUPS_MASK;
}
else if ( action.equalsIgnoreCase( CREATE_PAGES_ACTION ) )
{
} else if (action.equalsIgnoreCase(CREATE_PAGES_ACTION)) {
mask |= CREATE_PAGES_MASK;
}
else if ( action.equalsIgnoreCase( LOGIN_ACTION ) )
{
} else if (action.equalsIgnoreCase(LOGIN_ACTION)) {
mask |= LOGIN_MASK;
}
else if ( action.equalsIgnoreCase( EDIT_PREFERENCES_ACTION ) )
{
} else if (action.equalsIgnoreCase(EDIT_PREFERENCES_ACTION)) {
mask |= EDIT_PREFERENCES_MASK;
}
else if ( action.equalsIgnoreCase( EDIT_PROFILE_ACTION ) )
{
} else if (action.equalsIgnoreCase(EDIT_PROFILE_ACTION)) {
mask |= EDIT_PROFILE_MASK;
}
else
{
throw new IllegalArgumentException( "Unrecognized action: " + action );
} else {
throw new IllegalArgumentException("Unrecognized action: " + action);
}
}
return mask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ public class CreoleToJSPWikiTranslator

private static final String ESCAPE_PROTECTED = "~(\\*\\*|~|//|-|#|\\{\\{|}}|\\\\|~\\[~~[|]]|----|=|\\|)";

private static final Map<String, String> c_protectionMap = new HashMap<String, String>();
private static final Map<String, String> c_protectionMap = new HashMap<>();

private ArrayList<String> m_hashList = new ArrayList<String>();
private ArrayList<String> m_hashList = new ArrayList<>();

/**
* I have no idea what this method does. Could someone please tell me?
Expand Down Expand Up @@ -383,7 +383,7 @@ private String unprotectMarkup(String content, final boolean replacePlugins)
private String protectMarkup(String content)
{
c_protectionMap.clear();
m_hashList = new ArrayList<String>();
m_hashList = new ArrayList<>();
content = protectMarkup(content, PREFORMATTED_PROTECTED, "", "");
content = protectMarkup(content, URL_PROTECTED, "", "");
content = protectMarkup(content, ESCAPE_PROTECTED, "", "");
Expand All @@ -398,18 +398,16 @@ private ArrayList< String[] > readPlaceholderProperties(final Properties wikiPro
{
final Set< Object > keySet = wikiProps.keySet();
final Object[] keys = keySet.toArray();
final ArrayList<String[]> result = new ArrayList<String[]>();
final ArrayList<String[]> result = new ArrayList<>();

for( int i = 0; i < keys.length; i++ )
{
final String key = keys[i] + "";
final String value = wikiProps.getProperty( keys[i] + "" );
if( key.indexOf( "creole.imagePlugin.para.%" ) > -1 )
{
for (final Object o : keys) {
final String key = o + "";
final String value = wikiProps.getProperty(o + "");
if (key.contains("creole.imagePlugin.para.%")) {
final String[] pair = new String[2];
pair[0] = key.replaceAll( "creole.imagePlugin.para.%", "" );
pair[0] = key.replaceAll("creole.imagePlugin.para.%", "");
pair[1] = value;
result.add( pair );
result.add(pair);
}
}
return result;
Expand All @@ -433,14 +431,11 @@ private String replaceImageArea(final Properties wikiProps, final String content
{
final String[] params = paramsField.split(",");

for (int i = 0; i < params.length; i++)
{
final String param = params[i].replaceAll("\\||\\s", "").toUpperCase();
for (final String s : params) {
final String param = s.replaceAll("\\||\\s", "").toUpperCase();

// Replace placeholder params
for (int j = 0; j < plProperties.size(); j++)
{
final String[] pair = plProperties.get(j);
for (final String[] pair : plProperties) {
final String key = pair[0];
final String value = pair[1];
String code = param.replaceAll("(?i)([0-9]+)" + key, value + "<check>" + "$1" + "</check>");
Expand All @@ -451,13 +446,10 @@ private String replaceImageArea(final Properties wikiProps, final String content
}

// Check if it is a number
try
{
try {
Integer.parseInt(param);
paramsString.append(" width='").append(param).append("px'");
}
catch (final Exception e)
{
} catch (final Exception e) {

if (wikiProps.getProperty("creole.imagePlugin.para." + param) != null)
paramsString.append(" ").append(wikiProps.getProperty("creole.imagePlugin.para." + param)
Expand Down Expand Up @@ -538,9 +530,8 @@ private String protectMarkup(final String content, final String markupRegex, fin
private String bytesToHash(final byte[] b)
{
final StringBuilder hash = new StringBuilder();
for (int i = 0; i < b.length; i++)
{
hash.append(Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1));
for (final byte value : b) {
hash.append(Integer.toString((value & 0xff) + 0x100, 16).substring(1));
}
return hash.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1937,7 +1937,7 @@ else if( ch == '\n' || ch == '\r' )
{
// Fail quietly
}
else if( isSpan.booleanValue() )
else if(isSpan)
{
el = popElement( "span" );
}
Expand All @@ -1962,7 +1962,7 @@ else if( isSpan.booleanValue() )
try
{
style = StringEscapeUtils.unescapeHtml4(style);
if( style != null && style.indexOf("javascript:") != -1 )
if( style != null && style.contains("javascript:"))
{
log.debug("Attempt to output javascript within CSS:"+style);
final ResourceBundle rb = Preferences.getBundle( m_context, InternationalizationManager.CORE_BUNDLE );
Expand Down Expand Up @@ -2541,10 +2541,7 @@ private void paragraphify( final Element rootElement)
{
final Element newel = new Element("p");

for( final Iterator< Content > i = ls.iterator(); i.hasNext(); )
{
final Content c = i.next();

for (final Content c : ls) {
c.detach();
newel.addContent(c);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public Link parse(final String linktext ) throws ParseException

// parse attributes
// contains "='" that looks like attrib spec
if( attribs.indexOf(EQSQUO) != -1 )
if(attribs.contains(EQSQUO))
{
try
{
Expand Down
21 changes: 9 additions & 12 deletions jspwiki-main/src/main/java/org/apache/wiki/tags/CookieTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,20 +327,19 @@ private String getItemValue(final Cookie c, final String item )
*/
private Map<String, String> parseCookieValues(final String s )
{
final Map< String, String > rval = new HashMap< String, String >();
final Map< String, String > rval = new HashMap<>();
if( s == null ) {
return rval;
}
final String[] nvps = s.split( "&" );
if( nvps.length == 0 ) {
return rval;
}
for( int i = 0; i < nvps.length; i++ ) {
final String nvp = decode( nvps[i] );
final String[] nv = nvp.split( "=" );
if( nv[0] != null && !nv[0].trim().isEmpty() )
{
rval.put( nv[0], nv[1] );
for (final String value : nvps) {
final String nvp = decode(value);
final String[] nv = nvp.split("=");
if (nv[0] != null && !nv[0].trim().isEmpty()) {
rval.put(nv[0], nv[1]);
}
}

Expand Down Expand Up @@ -422,11 +421,9 @@ private Cookie findCookie(final String cname )
final Cookie[] cookies = req.getCookies();
if( cookies != null )
{
for( int i = 0; i < cookies.length; i++ )
{
if( cookies[i].getName().equals( cname ) )
{
return cookies[i];
for (final Cookie cookie : cookies) {
if (cookie.getName().equals(cname)) {
return cookie;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public final int doWikiStartTag() throws IOException {

if( ctx.getPage() != null ) {
final JspWriter out = pageContext.getOut();
final String diff = engine.getManager( DifferenceManager.class ).getDiff( ctx, vernew.intValue(), verold.intValue() );
final String diff = engine.getManager( DifferenceManager.class ).getDiff( ctx, vernew, verold);

if( diff.isEmpty() ) {
return EVAL_BODY_INCLUDE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,25 @@ public String doGet( final Context context ) {
final MBeanAttributeInfo[] attributes = info.getAttributes();
final StringBuilder sb = new StringBuilder();

for( int i = 0; i < attributes.length; i++ ) {
sb.append( "<div class='block'>\n" );
for (final MBeanAttributeInfo attribute : attributes) {
sb.append("<div class='block'>\n");

sb.append( "<label>" ).append( StringUtils.capitalize( attributes[i].getName() ) ).append( "</label>\n" );
sb.append("<label>").append(StringUtils.capitalize(attribute.getName())).append("</label>\n");

try {
final Object value = getAttribute( attributes[ i ].getName() );
if( attributes[ i ].isWritable() ) {
sb.append( "<input type='text' name='question' size='30' value='" ).append( value ).append( "' />\n" );
final Object value = getAttribute(attribute.getName());
if (attribute.isWritable()) {
sb.append("<input type='text' name='question' size='30' value='").append(value).append("' />\n");
} else {
sb.append( "<input type='text' class='readonly' readonly='true' size='30' value='" ).append( value ).append( "' />\n" );
sb.append("<input type='text' class='readonly' readonly='true' size='30' value='").append(value).append("' />\n");
}
} catch( final Exception e ) {
sb.append( "Exception: " ).append( e.getMessage() );
} catch (final Exception e) {
sb.append("Exception: ").append(e.getMessage());
}

sb.append( "<div class='description'>" ).append( attributes[i].getDescription() ).append( "</div>\n" );
sb.append("<div class='description'>").append(attribute.getDescription()).append("</div>\n");

sb.append( "</div>\n" );
sb.append("</div>\n");
}
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,9 @@ public synchronized Object put(final Object arg0, final Object arg1 )
public synchronized void putAll(final Map< ? , ? > arg0 )
{
// Shove all of the entries into the property string
for(final Iterator< ? > it = arg0.entrySet().iterator(); it.hasNext(); )
{
@SuppressWarnings("unchecked") final Entry< Object, Object > entry = ( Entry< Object, Object > )it.next();
writeProperty( entry.getKey(), entry.getValue() );
for (final Entry<?, ?> value : arg0.entrySet()) {
@SuppressWarnings("unchecked") final Entry<Object, Object> entry = (Entry<Object, Object>) value;
writeProperty(entry.getKey(), entry.getValue());
}

// Call the superclass method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Licensed to the Apache Software Foundation (ASF) under one
public class PriorityList<E>
extends AbstractList<E>
{
private final CopyOnWriteArrayList<Item<E>> m_elements = new CopyOnWriteArrayList<Item<E>>();
private final CopyOnWriteArrayList<Item<E>> m_elements = new CopyOnWriteArrayList<>();

/**
* This is the default priority, which is used if no priority
Expand Down Expand Up @@ -62,7 +62,7 @@ public void add(final E o, final int priority )
}
}

final Item<E> newItem = new Item<E>();
final Item<E> newItem = new Item<>();
newItem.m_priority = priority;
newItem.m_object = o;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Licensed to the Apache Software Foundation (ASF) under one
*/
public class TimedCounterList<T> extends AbstractList<T>
{
private final ArrayList<CounterItem<T>> m_list = new ArrayList<CounterItem<T>>();
private final ArrayList<CounterItem<T>> m_list = new ArrayList<>();
private final ReadWriteLock m_lock = new ReentrantReadWriteLock();

/**
Expand All @@ -51,7 +51,7 @@ public T set(final int index, final T element )

try
{
t = m_list.set(index,new CounterItem<T>(element)).m_obj;
t = m_list.set(index, new CounterItem<>(element)).m_obj;
}
finally
{
Expand Down Expand Up @@ -114,7 +114,7 @@ public void add(final int index, final T element )

try
{
m_list.add(index, new CounterItem<T>(element));
m_list.add(index, new CounterItem<>(element));
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ else if( Character.isLowerCase( c2 ) )
// If they're not digits, use character to character comparison
if( type1 != CharType.TYPE_DIGIT )
{
final Character ch1 = Character.valueOf( c1 );
final Character ch2 = Character.valueOf( c2 );
final Character ch1 = c1;
final Character ch2 = c2;
return ch1.compareTo( ch2 );
}

Expand Down Expand Up @@ -226,15 +226,15 @@ private int compareDigits(final char[] left, final char[] right, final int offse
idx++;
}
final int leftLen = idx - offset;
final int leftValue = Integer.valueOf( new String( left, offset, leftLen ) );
final int leftValue = Integer.parseInt( new String( left, offset, leftLen ) );

// Calculate the integer value of the right hand side
idx = offset;
while ( idx < right.length && Character.isDigit( right[idx] ) ) {
idx++;
}
final int rightLen = idx - offset;
final int rightValue = Integer.valueOf( new String( right, offset, rightLen ) );
final int rightValue = Integer.parseInt( new String( right, offset, rightLen ) );

if( leftValue == rightValue ) {
return leftLen - rightLen; // Same value so use the lengths
Expand Down