sdedic commented on a change in pull request #3262:
URL: https://github.com/apache/netbeans/pull/3262#discussion_r740143241
##########
File path:
platform/api.templates/src/org/netbeans/api/templates/CreateFromTemplateHandler.java
##########
@@ -59,4 +64,171 @@
CreateDescriptor desc
) throws IOException;
+ /**
+ * Replaces ${param} tokens in a String. The parameter reference has
syntax "${" paramName [":" defaultValue ] "}".
+ * Parameters are replaced recursively. "${" can be escaped by \, so "\${"
will not act as parameter reference start.
+ * @param expression string template
+ * @param parameters parameter values
+ * @return evaluated string.
+ * @since 1.23
+ */
+ public static String mapParameters(String expression, Map<String, ?>
parameters) {
+ int start = 0;
+ int pos = 0;
+ StringBuilder sb = null;
+
+ while ((pos = expression.indexOf("${", pos)) != -1) { // NOI18N
+ if (pos > 0 && expression.charAt(pos - 1) == '\\') { // NOI18N
+ pos += 2;
+ continue;
+ }
+ int endPos = pos + 2;
+ int state = 0;
+ int nested = 0;
+ A: while (endPos < expression.length()) {
+ char c = expression.charAt(endPos);
+ if (state == 1) {
+ state = 0;
+ endPos++;
+ continue;
+ }
+ switch (c) {
+ case '\\': // NOI18N
+ state = 1;
+ break;
+ case '$': // NOI18N
+ state = 2;
+ break;
+ case '{': // NOI18N
+ if (state == 2) {
+ state = 0;
+ nested++;
+ }
+ break;
+ case '}': // NOI18N
+ if (state == 0) {
+ if (nested-- == 0) {
+ break A;
+ }
+ }
+ break;
+ }
+ endPos++;
+ }
+ if (endPos >= expression.length()) {
+ pos += 2;
+ continue;
+ }
+ String token = expression.substring(pos + 2, endPos);
+ String defValue = null;
+
+ int colon = token.indexOf(':'); // NOI18N
+ if (colon != -1) {
+ defValue = token.substring(colon + 1);
+ token = token.substring(0, colon);
+ }
+
+ if (sb == null) {
+ sb = new StringBuilder();
+ }
+ Object v = parameters.get(token);
+ if (v == null) {
+ v = defValue;
+ }
+ if (v == null) {
+ pos += 2;
+ continue;
+ }
+ sb.append(expression.substring(start, pos)).append(v.toString());
+ start = endPos + 1;
+ pos = start;
+ }
+ if (sb == null) {
+ return expression;
+ }
+ if (start < expression.length()) {
+ sb.append(expression.substring(start));
+ }
+
+ return mapParameters(sb.toString(), parameters);
+ }
+
+ private static final String PROP_TEMPLATE = "template"; // NOI18N
+
+ /**
+ * Prefix for template-controlling attributes. Such attributes will not be
copied
+ * during template instantiation.
+ */
+ private static final String ATTR_TEMPLATE_PREFIX = "template.";
+
+ /**
+ * Copies template attributes over to the created file. Copies over from
the source (template) to the target (usually
+ * the produced file), except attributes that parametrize template
creation. By default, attributes that are prefixed
+ * with {@code "template."} are not copied. As the operation is not
atomic, the method should be called in a
+ * {@link
org.openide.filesystems.FileSystem#runAtomicAction(org.openide.filesystems.FileSystem.AtomicAction)}.
+ * The {@code "template"} attribute is never copied.
+ *
+ * @param h the Handler responsible for file creation, usually the calling
handler.
+ * @param from the original file
+ * @param to the target file
+ * @throws IOException if an I/O error occurs.
+ */
+ public static void copyAttributesFromTemplate(CreateFromTemplateHandler h,
FileObject from, FileObject to) throws IOException {
+ // copy attributes; some attributes are filtered by FileSystems API
already
+ FileUtil.copyAttributes(from, to);
+ // need to filter additional template-related attributes:
+ for (String a : Collections.list(to.getAttributes())) {
+ if ("javax.script.ScriptEngine".equals(a) // NOI18N
+ || a.startsWith(ATTR_TEMPLATE_PREFIX)) {
+ to.setAttribute(a, null);
+ }
+ }
+ // hack to overcome package-private modifier in setTemplate(fo,
boolean)
+ to.setAttribute(PROP_TEMPLATE, null);
+ }
+
+
+ /**
+ * Copies the files or folders contained in the specified folder to the
target. The original descriptor can be passed in,
+ * since it has Lookup, parameters etc set up. They will be passed on to
individual created files. The `contentsParent` may
+ * be {@code null}, in which case the {@link
CreateDescriptor#getTemplate()} folder will be used.
+ * @param origDescriptor original descriptor to get parameters / lookup
from
+ * @param contentsParent template folder whose contents should be copied
+ * @param target target folder
+ * @return created files
+ * @throws IOException
+ * @since 1.23
+ */
+ protected static List<FileObject> defaultCopyContents(CreateDescriptor
origDescriptor, FileObject contentsParent, FileObject target) throws
IOException {
Review comment:
Yes, you can always do a trampoline from some subclass and made it
public to whatever code you like. It was meant as a helper primarily for
CreateFromTemplateHandler imlementations (with limited use elsewhere), but can
be `public` as well.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists