TransformActionExecuter extends ActionExecuterAbstractBase { public static String transformName(MimetypeService mimetypeService, String original, String newMimetype, boolean alwaysAdd) { int dotIndex = original.lastIndexOf('.'); StringBuilder sb = new StringBuilder(original.length()); if (dotIndex > -1) { String nameBeforeDot = original.substring(0, dotIndex); String originalExtension = original.substring(dotIndex + 1, original.length()); boolean originalExtensionIsReasonable = isExtensionReasonable(originalExtension); String newExtension = mimetypeService.getExtension(newMimetype); if (originalExtensionIsReasonable) { sb.append(nameBeforeDot); sb.append('.').append(newExtension); } else { sb.append(original); if (alwaysAdd == true) { if (sb.charAt(sb.length() - 1) != '.') { sb.append('.'); } sb.append(newExtension); } } } else { sb.append(original); if (alwaysAdd == true) { String newExtension = mimetypeService.getExtension(newMimetype); sb.append('.').append(newExtension); } } return sb.toString(); }  void setMimetypeService(MimetypeService mimetypeService); void setNodeService(NodeService nodeService); void setCheckOutCheckInService(CheckOutCheckInService checkOutCheckInService); void setDictionaryService(DictionaryService dictionaryService); void setContentService(ContentService contentService); void setCopyService(CopyService copyService); static String transformName(MimetypeService mimetypeService, String original, String newMimetype, boolean alwaysAdd); static final String ERR_OVERWRITE; static final String NAME; static final String PARAM_MIME_TYPE; static final String PARAM_DESTINATION_FOLDER; static final String PARAM_ASSOC_TYPE_QNAME; static final String PARAM_ASSOC_QNAME; static final String PARAM_OVERWRITE_COPY; }
@Test public void transformName() throws Exception { MimetypeService dummyMimetypeService = new DummyMimetypeService("txt"); String original = "Letter to Bank Manager.doc"; final String newMimetype = MimetypeMap.MIMETYPE_TEXT_PLAIN; String newName = TransformActionExecuter.transformName(dummyMimetypeService, original, newMimetype, true); assertEquals("Letter to Bank Manager.txt", newName); original = "No.1 - First Document Title"; newName = TransformActionExecuter.transformName(dummyMimetypeService, original, newMimetype, true); assertEquals(original + ".txt", newName); original = "No.1 - First Document Title"; newName = TransformActionExecuter.transformName(dummyMimetypeService, original, newMimetype, false); assertEquals(original, newName); original = "Letter to Bank Manager"; newName = TransformActionExecuter.transformName(dummyMimetypeService, original, newMimetype, true); assertEquals(original + ".txt", newName); original = "Letter to Bank Manager"; newName = TransformActionExecuter.transformName(dummyMimetypeService, original, newMimetype, false); assertEquals(original, newName); original = "Letter to Bank Manager."; newName = TransformActionExecuter.transformName(dummyMimetypeService, original, newMimetype, true); assertEquals(original + "txt", newName); original = "Letter to Bank Manager."; newName = TransformActionExecuter.transformName(dummyMimetypeService, original, newMimetype, false); assertEquals(original, newName); original = "Letter to Bank Manager.txt"; final String unknownMimetype = "Marcel/Marceau"; dummyMimetypeService = new DummyMimetypeService("bin"); newName = TransformActionExecuter.transformName(dummyMimetypeService, original, unknownMimetype, true); assertEquals("Letter to Bank Manager.bin", newName); }