Package br.msf.commons.lang

Examples of br.msf.commons.lang.EnhancedStringBuilder


        assert fileObject != null && fileObject.isData() && FileType.isJava(fileObject.getExt());
        if (CharSequenceUtils.isBlank(params.getLicenseText())) {
            return;
        }
        try {
            EnhancedStringBuilder newSrc = getUpdatedSource(fileObject);
            if (newSrc != null) {
                if (!fileObject.canWrite()) {
                    throw new RuntimeIOException("Cant write to file: " + fileObject.getNameExt());
                }
                writeFile(fileObject, newSrc);
View Full Code Here


                                                  final String commentStart,
                                                  final String commentEnd,
                                                  final String leading,
                                                  final int maxLineLengh) {
        assert CharSequenceUtils.isNotBlank(text) && CharSequenceUtils.isNotBlank(commentStart) && CharSequenceUtils.isNotBlank(commentEnd);
        final EnhancedStringBuilder builder = new EnhancedStringBuilder(text).deletePattern("\r");
        final Collection<MatchEntry> newLines = builder.findPattern("\n");
        for (MatchEntry match : newLines) {
            if (match.getStart() > 0 && isPeriodFinalization(builder.charAt(match.getStart() - 1))
                && '\n' != builder.charAt(match.getStart() + 1)) {
                /* new lines not after a punctuation are removed */
                builder.replace(match, " ");
            }
        }
        builder.replacePattern(" +", " ");
        final Collection<String> lines = builder.split("\n");
        builder.clear().appendln(commentStart);
        int max = maxLineLengh - leading.length();
        for (CharSequence line : lines) {
            if (line.length() <= max) {
                // short line, just append the leading txt and the line itself
                builder.append(leading).appendln(line);
            } else {
                // long line, we need to break it down
                final List<String> subLines = breakdownLine(line.toString(), max);
                for (String subLine : subLines) {
                    builder.append(leading).appendln(subLine);
                }
            }
        }
        return builder.appendln(commentEnd);
    }
View Full Code Here

        }
        return builder.appendln(commentEnd);
    }

    protected EnhancedStringBuilder getUpdatedSource(final FileObject fileObject) throws IOException {
        final EnhancedStringBuilder builder = new EnhancedStringBuilder(fileObject.asText(FileEncodingQuery.getEncoding(fileObject).name()));
        final Collection<MatchEntry> currentLicense = builder.findPattern(JAVA_LICENSE);
        if (CollectionUtils.isNotEmpty(currentLicense)) {
            MatchEntry me = currentLicense.iterator().next();
            switch (params.getUpdateMode()) {
                case ALL_FILES:
                    /* source already have a license header. lets replace it */
                    builder.replace(me, getJavaLicense());
                    break;
                case WITHOUT_LICENSE_FILES:
                    /* we consider the default netbeans template as "file without license" */
                    if (builder.substring(me.getStart(), me.getEnd()).contains(NB_LICENSE_TEMPLATE)) {
                        builder.replace(me, getJavaLicense());
                    } else {
                        /* returning null sinalize that this FileObject will not be updated */
                        return null;
                    }
                    break;
            }
        } else {
            /* source dont have a license header. lets place it */
            builder.insert(0, getJavaLicense());
        }
        return builder;
    }
View Full Code Here

    protected List<String> breakdownLine(final String line, final int maxLenght) {
        assert line != null && maxLenght > 0;
        if (line.length() <= maxLenght || !CharSequenceUtils.containsPattern("\\s+", line)) {
            return Arrays.asList(line);
        }
        final EnhancedStringBuilder builder = new EnhancedStringBuilder(line);
        final List<String> lines = new ArrayList<String>();
        while (builder.length() > maxLenght) {
            int mark = -1;
            // here we look for the last ' ' occurrence before the MAX_LINE_LEN
            // or the first one if there is no ' ' before reaching the MAX_LINE_LEN limit
            for (int i = 0; i < builder.length(); i++) {
                if (builder.charAt(i) == ' ') {
                    if (i <= maxLenght || mark == -1) {
                        mark = i;
                    } else if (i > maxLenght) {
                        break;
                    }
                }
            }
            if (mark >= 0) {
                // if we found the ' ' occurrence we were looking for, we cut the line and create a subline with it
                lines.add(builder.substring(0, mark).trim());
                // remove the just added subline
                builder.delete(0, mark);
            } else {
                // it must be a super dupper mega word with no space! dont break it!
                lines.add(builder.trim().toString());
                builder.clear();
            }
        }
        if (builder.isNotBlank()) {
            // if there is some left-over on the buffer, we create the last subline with it.
            lines.add(builder.trim().toString());
        }
        return lines;
    }
View Full Code Here

        return null;
    }

    public static FileObject getPackageFileObject(final FileObject classPathRoot, final String fullPackageName) {
        assert FileObjectUtils.isValidDir(classPathRoot);
        final String pkg_dir = new EnhancedStringBuilder(fullPackageName).replacePattern(PKG_SEPARATOR, DIR_SEPARATOR).toString();
        return classPathRoot.getFileObject(pkg_dir);
    }
View Full Code Here

TOP

Related Classes of br.msf.commons.lang.EnhancedStringBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.