List of usage examples for java.lang Character isJavaIdentifierPart
public static boolean isJavaIdentifierPart(int codePoint)
From source file:com.jaspersoft.ireport.designer.data.fieldsproviders.ejbql.EJBQLFieldsReader.java
/** * Checks if the input is a valid Java literal * @param literal//ww w .j a v a 2 s . co m * @author Gaganis Giorgos (gaganis@users.sourceforge.net) */ private static boolean isValidLiteral(String literal) { boolean result = true; char[] literalChars = new char[literal.length()]; literal.getChars(0, literalChars.length, literalChars, 0); for (int i = 0; i < literalChars.length; i++) { if (i == 0 && !Character.isJavaIdentifierStart(literalChars[i])) { result = false; break; } if (i != 0 && !Character.isJavaIdentifierPart(literalChars[i])) { result = false; break; } } return result; }
From source file:com.github.magicsky.sya.checkers.TestSourceReader.java
/** * Returns an array of StringBuilder objects for each comment section found preceding the named * test in the source code./*from w ww . j a v a 2 s.co m*/ * * @param srcRoot the directory inside the bundle containing the packages * @param clazz the name of the class containing the test * @param testName the name of the test * @param numSections the number of comment sections preceding the named test to return. * Pass zero to get all available sections. * @return an array of StringBuilder objects for each comment section found preceding the named * test in the source code. * @throws IOException */ public static StringBuilder[] getContentsForTest(String srcRoot, Class clazz, final String testName, int numSections) throws IOException { // Walk up the class inheritance chain until we find the test method. try { while (clazz.getMethod(testName).getDeclaringClass() != clazz) { clazz = clazz.getSuperclass(); } } catch (SecurityException e) { Assert.fail(e.getMessage()); } catch (NoSuchMethodException e) { Assert.fail(e.getMessage()); } while (true) { // Find and open the .java file for the class clazz. String fqn = clazz.getName().replace('.', '/'); fqn = fqn.indexOf("$") == -1 ? fqn : fqn.substring(0, fqn.indexOf("$")); String classFile = fqn + ".java"; InputStream in; Class superclass = clazz.getSuperclass(); try { in = FileUtils.openInputStream(new File(srcRoot + '/' + classFile)); } catch (IOException e) { if (superclass == null || !superclass.getPackage().equals(clazz.getPackage())) { throw e; } clazz = superclass; continue; } BufferedReader br = new BufferedReader(new InputStreamReader(in)); try { // Read the java file collecting comments until we encounter the test method. List<StringBuilder> contents = new ArrayList<StringBuilder>(); StringBuilder content = new StringBuilder(); for (String line = br.readLine(); line != null; line = br.readLine()) { line = line.replaceFirst("^\\s*", ""); // Replace leading whitespace, preserve trailing if (line.startsWith("//")) { content.append(line.substring(2) + "\n"); } else { if (!line.startsWith("@") && content.length() > 0) { contents.add(content); if (numSections > 0 && contents.size() == numSections + 1) contents.remove(0); content = new StringBuilder(); } if (line.length() > 0 && !contents.isEmpty()) { int idx = line.indexOf(testName); if (idx != -1 && !Character.isJavaIdentifierPart(line.charAt(idx + testName.length()))) { return contents.toArray(new StringBuilder[contents.size()]); } if (!line.startsWith("@")) { contents.clear(); } } } } } finally { br.close(); } if (superclass == null || !superclass.getPackage().equals(clazz.getPackage())) { throw new IOException("Test data not found for " + clazz.getName() + "." + testName); } clazz = superclass; } }
From source file:org.apache.sqoop.orm.ClassWriter.java
/** * Given some character that can't be in an identifier, * try to map it to a string that can.//from w ww . j a v a 2 s . c o m * * @param c a character that can't be in a Java identifier * @return a string of characters that can, or null if there's * no good translation. */ private static String getIdentifierStrForChar(char c) { if (Character.isJavaIdentifierPart(c)) { return "" + c; } else if (Character.isWhitespace(c)) { // Eliminate whitespace. return null; } else { // All other characters map to underscore. StringBuilder sb = new StringBuilder(); for (int i = 1; i <= underscores; i++) { sb.append("_"); } return sb.toString(); } }
From source file:com.mirth.connect.connectors.jms.transformers.AbstractJmsTransformer.java
/** * Encode a string so that is is a valid java identifier * //from w w w . j a v a2 s .co m * @param name * @return */ public static String encodeHeader(String name) { StringBuffer sb = new StringBuffer(name.length()); for (int i = 0; i < name.length(); i++) { char c = name.charAt(i); if (i == 0) { if (!Character.isJavaIdentifierStart(c)) { c = REPLACEMENT_CHAR; } } else { if (!Character.isJavaIdentifierPart(c)) { c = REPLACEMENT_CHAR; } } sb.append(c); } return sb.toString(); }
From source file:com.legstar.codegen.tasks.SourceToXsdCobolTask.java
/** * Converts a URI into a package name. We assume a hierarchical, * server-based URI with the following syntax: * [scheme:][//host[:port]][path][?query][#fragment] * The package name is derived from host, path and fragment. * /*from w w w . j a va 2 s. c o m*/ * @param namespaceURI the input namespace URI * @return the result package name */ public static String packageFromURI(final URI namespaceURI) { StringBuilder result = new StringBuilder(); URI nURI = namespaceURI.normalize(); boolean firstToken = true; /* * First part of package name is built from host with tokens in * reverse order. */ if (nURI.getHost() != null && nURI.getHost().length() != 0) { Vector<String> v = new Vector<String>(); StringTokenizer t = new StringTokenizer(nURI.getHost(), "."); while (t.hasMoreTokens()) { v.addElement(t.nextToken()); } for (int i = v.size(); i > 0; i--) { if (!firstToken) { result.append('.'); } else { firstToken = false; } result.append(v.get(i - 1)); } } /* Next part of package is built from the path tokens */ if (nURI.getPath() != null && nURI.getPath().length() != 0) { Vector<String> v = new Vector<String>(); StringTokenizer t = new StringTokenizer(nURI.getPath(), "/"); while (t.hasMoreTokens()) { v.addElement(t.nextToken()); } for (int i = 0; i < v.size(); i++) { String token = v.get(i); /* ignore situations such as /./../ */ if (token.equals(".") || token.equals("..")) { continue; } if (!firstToken) { result.append('.'); } else { firstToken = false; } result.append(v.get(i)); } } /* Finally append any fragment */ if (nURI.getFragment() != null && nURI.getFragment().length() != 0) { if (!firstToken) { result.append('.'); } else { firstToken = false; } result.append(nURI.getFragment()); } /* * By convention, namespaces are lowercase and should not contain * invalid Java identifiers */ String s = result.toString().toLowerCase(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length(); i++) { Character c = s.charAt(i); if (Character.isJavaIdentifierPart(c) || c.equals('.')) { sb.append(c); } else { sb.append("_"); } } return sb.toString(); }
From source file:org.apache.solr.search.SolrReturnFields.java
public static String getFieldName(StrParser sp) { sp.eatws();//from www .j a v a 2 s . co m int id_start = sp.pos; char ch; if (sp.pos < sp.end && (ch = sp.val.charAt(sp.pos)) != '$' && Character.isJavaIdentifierStart(ch)) { sp.pos++; while (sp.pos < sp.end) { ch = sp.val.charAt(sp.pos); if (!Character.isJavaIdentifierPart(ch) && ch != '.' && ch != '-') { break; } sp.pos++; } return sp.val.substring(id_start, sp.pos); } return null; }
From source file:com.google.jenkins.flakyTestHandler.plugin.TestFlakyStatsOverRevision.java
/** * Get the name of a test that's URL-safe. * * @param testName input test name//w w w .jav a2 s. c o m * @return name of the test with illegal characters replaced with '_' */ public static String getSafeTestName(String testName) { StringBuilder buf = new StringBuilder(testName); for (int i = 0; i < buf.length(); i++) { char ch = buf.charAt(i); if (!Character.isJavaIdentifierPart(ch)) { buf.setCharAt(i, '_'); } } return buf.toString(); }
From source file:com.safi.workshop.sqlexplorer.sqleditor.ExtendedCompletionProposal.java
/** * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, * int)/*from w w w . ja v a2 s . com*/ */ public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { if (dictionary == null) return null; String text = viewer.getDocument().get(); String string = text.substring(0, documentOffset); if (string.equals("")) //$NON-NLS-1$ return null; int position = string.length() - 1; char character; while (position > 0) { character = string.charAt(position); if (!Character.isJavaIdentifierPart(character) && (character != '.')) break; --position; } if (position == 0) position = -1; string = string.substring(position + 1); // JFaceDbcPlugin.error("String: "+string,new Exception()); if (StringUtils.isBlank(string)) { List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>(); String[] names = dictionary.getAllCatalogSchemas(); ICompletionProposal[] tempProps = new ICompletionProposal[names.length]; for (int i = 0; i < names.length; i++) { tempProps[i] = new CompletionProposal(names[i], documentOffset, 0, names[i].length(), catalogImage, names[i], null, null); } proposals.addAll(Arrays.asList(tempProps)); names = dictionary.getAllTables(); tempProps = new ICompletionProposal[names.length]; for (int i = 0; i < names.length; i++) { tempProps[i] = new CompletionProposal(names[i], documentOffset, 0, names[i].length(), tableImage, names[i], null, null); } proposals.addAll(Arrays.asList(tempProps)); names = dictionary.getAllExternalObjects(); tempProps = new ICompletionProposal[names.length]; for (int i = 0; i < names.length; i++) { tempProps[i] = new CompletionProposal(names[i], documentOffset, 0, names[i].length(), keywordImage, names[i], null, null); } proposals.addAll(Arrays.asList(tempProps)); return proposals.toArray(new ICompletionProposal[proposals.size()]); } // if (string == null || string.equals("")) // return null; string = string.toLowerCase(); int length = string.length(); // if (length < 1) // return null; int dotIndex = string.lastIndexOf("."); //$NON-NLS-1$ if (string.charAt(length - 1) == ' ') { return null; } else if (string.charAt(length - 1) == '.') {// Last typed character // is '.' String name = string.substring(0, length - 1); if (name == null) return null; int otherDot = name.lastIndexOf("."); if (otherDot != -1) name = name.substring(otherDot + 1); if (name == null || name.equals("")) return null; TreeSet st = (TreeSet) dictionary.getColumnListByTableName(name); if (st != null) { ArrayList list = (ArrayList) dictionary.getByTableName(name); if (list == null) return null; TableNode nd = null; if (list.size() >= 1) nd = (TableNode) list.get(0); else return null; Object[] obj = st.toArray(); String[] arr = new String[obj.length]; System.arraycopy(obj, 0, arr, 0, obj.length); ICompletionProposal[] result = new ICompletionProposal[arr.length]; String tableDesc = null; if (nd != null) tableDesc = nd.getTableDesc(); for (int i = 0; i < arr.length; i++) { result[i] = new CompletionProposal(arr[i], documentOffset, 0, arr[i].length(), colImage, arr[i], null, tableDesc); } return result; } INode node = (INode) dictionary.getByCatalogSchemaName(name); if (node != null) { Object children[] = node.getChildNodes(); ArrayList propList = new ArrayList(); if (children != null) { for (Object element : children) { String childName = element.toString().toLowerCase(); if (childName.equals("table") || childName.equals("view") || childName.equals("tables")) { Object[] tables = ((INode) element).getChildNodes(); if (tables != null) { for (Object table : tables) { Image tmpImage = null; String tableName = table.toString(); if (table instanceof TableNode) { if (((TableNode) table).isTable()) tmpImage = tableImage; else if (((TableNode) table).isView()) tmpImage = viewImage; propList.add(new ExtendedCompletionProposal(tableName, documentOffset, 0, tableName.length(), tmpImage, tableName, (TableNode) table)); } } } } } } ICompletionProposal[] res = new ICompletionProposal[propList.size()]; System.arraycopy(propList.toArray(), 0, res, 0, propList.size()); Arrays.sort(res, new ICompletionProposalComparator()); return res; } } else if (dotIndex == -1)// The string does not contain "." { String[] keywordProposal = Dictionary.matchKeywordsPrefix(string); ICompletionProposal[] resKey = new ICompletionProposal[keywordProposal.length]; for (int i = 0; i < keywordProposal.length; i++) { resKey[i] = new CompletionProposal(keywordProposal[i], documentOffset - length, length, keywordProposal[i].length(), keywordImage, keywordProposal[i], null, null); } String[] proposalsString = dictionary.matchTablePrefix(string.toLowerCase()); ArrayList propList = new ArrayList(); for (String element : proposalsString) { ArrayList ls = dictionary.getTableObjectList(element); for (int j = 0; j < ls.size(); j++) { TableNode tbNode = (TableNode) ls.get(j); Image tmpImage = null; if (tbNode.isView()) tmpImage = viewImage; else if (tbNode.isTable()) tmpImage = tableImage; ICompletionProposal cmp = new ExtendedCompletionProposal(element, documentOffset - length, length, element.length(), tmpImage, element, tbNode); propList.add(cmp); } } String[] proposalsString2 = dictionary.matchCatalogSchemaPrefix(string.toLowerCase()); ICompletionProposal[] resKey2 = new ICompletionProposal[proposalsString2.length]; for (int i = 0; i < proposalsString2.length; i++) { resKey2[i] = new CompletionProposal(proposalsString2[i], documentOffset - length, length, proposalsString2[i].length(), catalogImage, proposalsString2[i], null, null); } ICompletionProposal[] res = new ICompletionProposal[propList.size() + keywordProposal.length + resKey2.length]; System.arraycopy(resKey, 0, res, 0, resKey.length); System.arraycopy(propList.toArray(), 0, res, resKey.length, propList.size()); System.arraycopy(resKey2, 0, res, resKey.length + propList.size(), resKey2.length); Arrays.sort(res, new ICompletionProposalComparator()); return res; } else if (dotIndex != -1) { String firstPart = string.substring(0, dotIndex); int otherDot = firstPart.indexOf("."); if (otherDot != -1) firstPart = firstPart.substring(otherDot + 1); String lastPart = string.substring(dotIndex + 1); if (lastPart == null || firstPart == null || lastPart.equals("") || firstPart.equals("")) return null; TreeSet st = (TreeSet) dictionary.getColumnListByTableName(firstPart); if (st != null) { Iterator iter = st.iterator(); ArrayList propList = new ArrayList(); while (iter.hasNext()) { String colName = (String) iter.next(); int length2 = lastPart.length(); if (colName.length() >= length2) { if ((colName.substring(0, lastPart.length())).equalsIgnoreCase(lastPart)) { CompletionProposal cmp = new CompletionProposal(colName, documentOffset - length2, length2, colName.length(), colImage, colName, null, null); propList.add(cmp); } } } ICompletionProposal[] res = new ICompletionProposal[propList.size()]; System.arraycopy(propList.toArray(), 0, res, 0, propList.size()); return res; } INode node = (INode) dictionary.getByCatalogSchemaName(firstPart); if (node != null) { String[] proposalsString = dictionary.matchTablePrefix(lastPart.toLowerCase()); ArrayList propList = new ArrayList(); for (String element : proposalsString) { ArrayList ls = dictionary.getTableObjectList(element); for (int j = 0; j < ls.size(); j++) { TableNode tbNode = (TableNode) ls.get(j); Image tmpImage = null; TableFolderNode totn = (TableFolderNode) tbNode.getParent(); INode catSchema = totn.getParent(); if (catSchema == node) { if (tbNode.isView()) tmpImage = viewImage; else if (tbNode.isTable()) tmpImage = tableImage; ICompletionProposal cmp = new ExtendedCompletionProposal(element, documentOffset - lastPart.length(), lastPart.length(), element.length(), tmpImage, element, tbNode); propList.add(cmp); } } } ICompletionProposal[] res = new ICompletionProposal[propList.size()]; System.arraycopy(propList.toArray(), 0, res, 0, propList.size()); return res; } } return null; }
From source file:com.cloudera.sqoop.orm.ClassWriter.java
/** * Coerce a candidate name for an identifier into one which will * definitely compile./*from w ww.jav a 2s . co m*/ * * Ensures that the returned identifier matches [A-Za-z_][A-Za-z0-9_]* * and is not a reserved word. * * @param candidate A string we want to use as an identifier * @return A string naming an identifier which compiles and is * similar to the candidate. */ public static String toIdentifier(String candidate) { StringBuilder sb = new StringBuilder(); boolean first = true; for (char c : candidate.toCharArray()) { if (Character.isJavaIdentifierStart(c) && first) { // Ok for this to be the first character of the identifier. sb.append(c); first = false; } else if (Character.isJavaIdentifierPart(c) && !first) { // Ok for this character to be in the output identifier. sb.append(c); } else { // We have a character in the original that can't be // part of this identifier we're building. // If it's just not allowed to be the first char, add a leading '_'. // If we have a reasonable translation (e.g., '-' -> '_'), do that. // Otherwise, drop it. if (first && Character.isJavaIdentifierPart(c) && !Character.isJavaIdentifierStart(c)) { sb.append("_"); sb.append(c); first = false; } else { // Try to map this to a different character or string. // If we can't just give up. String translated = getIdentifierStrForChar(c); if (null != translated) { sb.append(translated); first = false; } } } } String output = sb.toString(); if (isReservedWord(output)) { // e.g., 'class' -> '_class'; return "_" + output; } return output; }
From source file:org.apache.solr.search.ReturnFields.java
String getFieldName(QueryParsing.StrParser sp) throws ParseException { sp.eatws();//from www . j a v a 2 s. co m int id_start = sp.pos; char ch; if (sp.pos < sp.end && (ch = sp.val.charAt(sp.pos)) != '$' && Character.isJavaIdentifierStart(ch)) { sp.pos++; while (sp.pos < sp.end) { ch = sp.val.charAt(sp.pos); if (!Character.isJavaIdentifierPart(ch) && ch != '.' && ch != '-') { break; } sp.pos++; } return sp.val.substring(id_start, sp.pos); } return null; }