1 19 20 package org.netbeans.lib.editor.codetemplates; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import org.netbeans.lib.editor.codetemplates.spi.CodeTemplateParameter; 26 import org.netbeans.lib.editor.util.swing.PositionRegion; 27 28 33 public final class ParametrizedTextParser { 34 35 private final CodeTemplateInsertHandler handler; 36 37 private final String parametrizedText; 38 39 private List paramImpls; 41 44 private List parametrizedTextFragments; 45 46 public ParametrizedTextParser(CodeTemplateInsertHandler handler, String parametrizedText) { 47 this.handler = handler; this.parametrizedText = parametrizedText; 49 if (handler == null) { paramImpls = new ArrayList (); 51 } 52 } 53 54 public void parse() { 55 parametrizedTextFragments = new ArrayList (); 56 57 StringBuffer textFrag = new StringBuffer (); 58 int copyStartIndex = 0; 59 int index = 0; boolean atEOT = false; 61 while (!atEOT) { 62 int dollarIndex = parametrizedText.indexOf('$', index); 65 if (dollarIndex != -1) { switch (parametrizedText.charAt(dollarIndex + 1)) { case '{': textFrag.append(parametrizedText.substring(copyStartIndex, dollarIndex)); 70 copyStartIndex = dollarIndex; 71 parametrizedTextFragments.add(textFrag.toString()); 72 textFrag.setLength(0); 73 74 CodeTemplateParameterImpl paramImpl = new CodeTemplateParameterImpl( 76 handler, parametrizedText, dollarIndex); 77 78 int afterClosingBraceIndex = paramImpl.getParametrizedTextEndOffset(); 79 if (afterClosingBraceIndex <= parametrizedText.length()) { if (handler != null) { 81 handler.notifyParameterParsed(paramImpl); 82 } else { paramImpls.add(paramImpl); 84 } 85 index = afterClosingBraceIndex; 86 copyStartIndex = index; 87 88 } else { atEOT = true; 90 break; 91 } 92 break; 93 94 case '$': textFrag.append(parametrizedText.substring(copyStartIndex, dollarIndex + 1)); 96 index = dollarIndex + 2; 97 copyStartIndex = index; 98 break; 99 100 default: index = dollarIndex + 1; 102 break; 103 } 104 105 } else { textFrag.append(parametrizedText.substring(copyStartIndex)); 107 parametrizedTextFragments.add(textFrag.toString()); 108 atEOT = true; 109 } 110 } 111 } 112 113 public String buildInsertText(List allParameters) { 114 StringBuffer insertTextBuffer = new StringBuffer (parametrizedText.length()); 115 insertTextBuffer.append(parametrizedTextFragments.get(0)); 116 int fragIndex = 1; 117 for (Iterator it = allParameters.iterator(); it.hasNext();) { 118 CodeTemplateParameterImpl param = CodeTemplateParameterImpl.get( 119 (CodeTemplateParameter)it.next()); 120 int startOffset = insertTextBuffer.length(); 121 insertTextBuffer.append(param.getValue()); 122 param.resetPositions( 123 PositionRegion.createFixedPosition(startOffset), 124 PositionRegion.createFixedPosition(insertTextBuffer.length()) 125 ); 126 insertTextBuffer.append(parametrizedTextFragments.get(fragIndex)); 127 fragIndex++; 128 } 129 return insertTextBuffer.toString(); 130 } 131 132 private static String toHtmlText(String text) { 133 return CodeTemplateCompletionItem.toHtmlText(text); 134 } 135 136 public void appendHtmlText(StringBuffer htmlTextBuffer) { 137 htmlTextBuffer.append(toHtmlText((String )parametrizedTextFragments.get(0))); 138 139 int fragIndex = 1; 140 for (Iterator it = paramImpls.iterator(); it.hasNext();) { 141 CodeTemplateParameterImpl paramImpl = (CodeTemplateParameterImpl)it.next(); 142 htmlTextBuffer.append("<b>"); if (CodeTemplateParameter.CURSOR_PARAMETER_NAME.equals(paramImpl.getName())) { 144 htmlTextBuffer.append("|"); } else { 146 htmlTextBuffer.append(toHtmlText(paramImpl.getValue())); 147 } 148 htmlTextBuffer.append("</b>"); htmlTextBuffer.append(toHtmlText((String )parametrizedTextFragments.get(fragIndex))); 150 fragIndex++; 151 } 152 } 153 154 } 155 | Popular Tags |