PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/plugins/com.liferay.ide.portlet.core/src/com/liferay/ide/portlet/core/PluginPropertiesConfigurationLayout.java

https://gitlab.com/4615833/liferay-ide
Java | 321 lines | 296 code | 7 blank | 18 comment | 0 complexity | 68383635300b5331ee406734476552ac MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. *
  14. *******************************************************************************/
  15. package com.liferay.ide.portlet.core;
  16. import com.liferay.ide.core.util.CoreUtil;
  17. import java.io.IOException;
  18. import java.io.Writer;
  19. import java.util.Arrays;
  20. import java.util.Collections;
  21. import java.util.Comparator;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import org.apache.commons.configuration.ConfigurationException;
  25. import org.apache.commons.configuration.PropertiesConfiguration;
  26. import org.apache.commons.configuration.PropertiesConfiguration.PropertiesWriter;
  27. import org.apache.commons.configuration.PropertiesConfigurationLayout;
  28. import org.apache.commons.lang.ArrayUtils;
  29. import org.apache.commons.lang.StringEscapeUtils;
  30. import org.apache.commons.lang.StringUtils;
  31. /**
  32. * @author Greg Amerson
  33. */
  34. @SuppressWarnings( "rawtypes" )
  35. public class PluginPropertiesConfigurationLayout extends PropertiesConfigurationLayout
  36. {
  37. public static class PluginPropertiesWriter extends PropertiesWriter
  38. {
  39. /** Constant for the escaping character. */
  40. private static final String ESCAPE = "\\"; //$NON-NLS-1$
  41. /** The list of possible key/value separators */
  42. private static final char[] SEPARATORS = new char[] { '=', ':' };
  43. /** The white space characters used as key/value separators. */
  44. private static final char[] WHITE_SPACE = new char[] { ' ', '\t', '\f' };
  45. private char delimiter;
  46. public PluginPropertiesWriter( Writer writer, char delimiter )
  47. {
  48. super( writer, delimiter );
  49. this.delimiter = delimiter;
  50. }
  51. public void writeProperty( String key, Object value, boolean forceSingleLine, boolean wrappedProperty )
  52. throws IOException
  53. {
  54. String v;
  55. if( value instanceof List )
  56. {
  57. List values = (List) value;
  58. if( forceSingleLine )
  59. {
  60. v = makeSingleLineValue( values );
  61. }
  62. else
  63. {
  64. writeProperty( key, values );
  65. return;
  66. }
  67. }
  68. else if( wrappedProperty )
  69. {
  70. String[] values = value.toString().split( "," ); //$NON-NLS-1$
  71. StringBuffer buf = new StringBuffer();
  72. for( String val : values )
  73. {
  74. if( key.equals( IPluginPackageModel.PROPERTY_DEPLOY_EXCLUDE ) &&
  75. !val.startsWith( "**/WEB-INF/lib/" ) )
  76. {
  77. val = "**/WEB-INF/lib/" + val;
  78. }
  79. if( CoreUtil.isNullOrEmpty( buf.toString() ) )
  80. {
  81. buf.append( "\\\n" ); //$NON-NLS-1$
  82. buf.append( " " + escapeValue( val ) ); //$NON-NLS-1$
  83. }
  84. else
  85. {
  86. buf.append( ",\\\n " + escapeValue( val ) ); //$NON-NLS-1$
  87. }
  88. }
  89. v = buf.toString();
  90. }
  91. else
  92. {
  93. v = escapeValue( value );
  94. }
  95. write( escapeKey( key ) );
  96. write( "=" ); //$NON-NLS-1$
  97. write( v );
  98. writeln( null );
  99. }
  100. private String escapeKey( String key )
  101. {
  102. StringBuffer newkey = new StringBuffer();
  103. for( int i = 0; i < key.length(); i++ )
  104. {
  105. char c = key.charAt( i );
  106. if( ArrayUtils.contains( SEPARATORS, c ) || ArrayUtils.contains( WHITE_SPACE, c ) )
  107. {
  108. // escape the separator
  109. newkey.append( '\\' );
  110. newkey.append( c );
  111. }
  112. else
  113. {
  114. newkey.append( c );
  115. }
  116. }
  117. return newkey.toString();
  118. }
  119. private String escapeValue( Object value )
  120. {
  121. String escapedValue = StringEscapeUtils.escapeJava( String.valueOf( value ) );
  122. if( delimiter != 0 )
  123. {
  124. escapedValue = StringUtils.replace( escapedValue, String.valueOf( delimiter ), ESCAPE + delimiter );
  125. }
  126. return escapedValue;
  127. }
  128. private String makeSingleLineValue( List values )
  129. {
  130. if( !values.isEmpty() )
  131. {
  132. Iterator it = values.iterator();
  133. String lastValue = escapeValue( it.next() );
  134. StringBuffer buf = new StringBuffer( lastValue );
  135. while( it.hasNext() )
  136. {
  137. // if the last value ended with an escape character, it has
  138. // to be escaped itself; otherwise the list delimiter will
  139. // be escaped
  140. if( lastValue.endsWith( ESCAPE ) )
  141. {
  142. buf.append( ESCAPE ).append( ESCAPE );
  143. }
  144. buf.append( delimiter );
  145. lastValue = escapeValue( it.next() );
  146. buf.append( lastValue );
  147. }
  148. return buf.toString();
  149. }
  150. else
  151. {
  152. return null;
  153. }
  154. }
  155. }
  156. public static final String[] sortedKeys = new String[]
  157. {
  158. "name", //$NON-NLS-1$
  159. "module-group-id", //$NON-NLS-1$
  160. "module-incremental-version", //$NON-NLS-1$
  161. "tags", //$NON-NLS-1$
  162. "short-description", //$NON-NLS-1$
  163. "long-description", //$NON-NLS-1$
  164. "change-log", //$NON-NLS-1$
  165. "page-url", //$NON-NLS-1$
  166. "author", //$NON-NLS-1$
  167. "licenses", //$NON-NLS-1$
  168. "liferay-versions", //$NON-NLS-1$
  169. "portal-dependency-jars", //$NON-NLS-1$
  170. "deploy-excludes", //$NON-NLS-1$
  171. "portal-dependency-tlds", //$NON-NLS-1$
  172. "speed-filters-enabled" //$NON-NLS-1$
  173. };
  174. public PluginPropertiesConfigurationLayout( PropertiesConfiguration config )
  175. {
  176. super( config );
  177. this.setForceSingleLine( true );
  178. this.setSingleLine( IPluginPackageModel.PROPERTY_PORTAL_DEPENDENCY_JARS, false );
  179. this.setSingleLine( IPluginPackageModel.PROPERTY_PORTAL_DEPENDENCY_TLDS, false );
  180. this.setSingleLine( IPluginPackageModel.PROPERTY_DEPLOY_EXCLUDE, false );
  181. this.setSingleLine( IPluginPackageModel.PROPERTY_REQUIRED_DEPLOYMENT_CONTEXTS, false );
  182. }
  183. public boolean isWrappedProperty( String key )
  184. {
  185. return key.equals( IPluginPackageModel.PROPERTY_PORTAL_DEPENDENCY_JARS ) ||
  186. key.equals( IPluginPackageModel.PROPERTY_PORTAL_DEPENDENCY_TLDS ) ||
  187. key.equals( IPluginPackageModel.PROPERTY_DEPLOY_EXCLUDE ) ||
  188. key.equals( IPluginPackageModel.PROPERTY_REQUIRED_DEPLOYMENT_CONTEXTS );
  189. }
  190. public void save( Writer out ) throws ConfigurationException
  191. {
  192. try
  193. {
  194. char delimiter =
  195. getConfiguration().isDelimiterParsingDisabled() ? 0 : getConfiguration().getListDelimiter();
  196. PluginPropertiesWriter writer = new PluginPropertiesWriter( out, delimiter );
  197. if( getHeaderComment() != null )
  198. {
  199. writer.writeln( getCanonicalHeaderComment( true ) );
  200. writer.writeln( null );
  201. }
  202. List<Object> keyList = Arrays.asList( getKeys().toArray() );
  203. Collections.sort( keyList, new Comparator<Object>()
  204. {
  205. public int compare( Object o1, Object o2 )
  206. {
  207. int index1 = Integer.MAX_VALUE;
  208. int index2 = Integer.MAX_VALUE;
  209. for( int i = 0; i < sortedKeys.length; i++ )
  210. {
  211. if( sortedKeys[i].equals( o1 ) )
  212. {
  213. index1 = i;
  214. }
  215. if( sortedKeys[i].equals( o2 ) )
  216. {
  217. index2 = i;
  218. }
  219. }
  220. if( index1 < index2 )
  221. {
  222. return -1;
  223. }
  224. else if( index1 > index2 )
  225. {
  226. return 1;
  227. }
  228. return 0;
  229. }
  230. } );
  231. for( Iterator it = keyList.iterator(); it.hasNext(); )
  232. {
  233. String key = (String) it.next();
  234. if( getConfiguration().containsKey( key ) )
  235. {
  236. // Output blank lines before property
  237. for( int i = 0; i < getBlancLinesBefore( key ); i++ )
  238. {
  239. writer.writeln( null );
  240. }
  241. // Output the comment
  242. if( getComment( key ) != null )
  243. {
  244. writer.writeln( getCanonicalComment( key, true ) );
  245. }
  246. // Output the property and its value
  247. boolean singleLine =
  248. ( isForceSingleLine() || isSingleLine( key ) ) &&
  249. !getConfiguration().isDelimiterParsingDisabled();
  250. boolean wrappedProperty = isWrappedProperty( key );
  251. writer.writeProperty( key, getConfiguration().getProperty( key ), singleLine, wrappedProperty );
  252. }
  253. }
  254. writer.flush();
  255. writer.close();
  256. }
  257. catch( IOException ioex )
  258. {
  259. throw new ConfigurationException( ioex );
  260. }
  261. }
  262. }