Java Code Examples for java.text.MessageFormat#parse()

The following examples show how to use java.text.MessageFormat#parse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LargeMessageFormat.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testParse() throws ParseException {
    StringBuffer parseTemplate = new StringBuffer();
    StringBuffer parseInput = new StringBuffer();
    for (int i = 0; i < REPEATS; i++) {
        parseTemplate.append("{" + i + ", number} ");
        parseInput.append(i + " ");
    }
    MessageFormat parseFormat = new MessageFormat(parseTemplate.toString());
    Object[] parseResult = parseFormat.parse(parseInput.toString());
    for (int i = 0; i < REPEATS; i++) {
        if (((Number) parseResult[i]).intValue() != i) {
            throw new RuntimeException("got wrong parse result");
        }
    }
}
 
Example 2
Source File: LargeMessageFormat.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void testParse() throws ParseException {
    StringBuffer parseTemplate = new StringBuffer();
    StringBuffer parseInput = new StringBuffer();
    for (int i = 0; i < REPEATS; i++) {
        parseTemplate.append("{" + i + ", number} ");
        parseInput.append(i + " ");
    }
    MessageFormat parseFormat = new MessageFormat(parseTemplate.toString());
    Object[] parseResult = parseFormat.parse(parseInput.toString());
    for (int i = 0; i < REPEATS; i++) {
        if (((Number) parseResult[i]).intValue() != i) {
            throw new RuntimeException("got wrong parse result");
        }
    }
}
 
Example 3
Source File: LargeMessageFormat.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void testParse() throws ParseException {
    StringBuffer parseTemplate = new StringBuffer();
    StringBuffer parseInput = new StringBuffer();
    for (int i = 0; i < REPEATS; i++) {
        parseTemplate.append("{" + i + ", number} ");
        parseInput.append(i + " ");
    }
    MessageFormat parseFormat = new MessageFormat(parseTemplate.toString());
    Object[] parseResult = parseFormat.parse(parseInput.toString());
    for (int i = 0; i < REPEATS; i++) {
        if (((Number) parseResult[i]).intValue() != i) {
            throw new RuntimeException("got wrong parse result");
        }
    }
}
 
Example 4
Source File: HighlightImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static HighlightImpl parse(StyledDocument doc, String line) throws ParseException, BadLocationException {
    MessageFormat f = new MessageFormat("[{0}], {1,number,integer}:{2,number,integer}-{3,number,integer}:{4,number,integer}");
    Object[] args = f.parse(line);
    
    String attributesString = (String) args[0];
    int    lineStart  = ((Long) args[1]).intValue();
    int    columnStart  = ((Long) args[2]).intValue();
    int    lineEnd  = ((Long) args[3]).intValue();
    int    columnEnd  = ((Long) args[4]).intValue();
    
    String[] attrElements = attributesString.split(",");
    List<ColoringAttributes> attributes = new ArrayList<ColoringAttributes>();
    
    for (String a : attrElements) {
        a = a.trim();
        
        attributes.add(ColoringAttributes.valueOf(a));
    }
    
    if (attributes.contains(null))
        throw new NullPointerException();
    
    int offsetStart = NbDocument.findLineOffset(doc, lineStart) + columnStart;
    int offsetEnd = NbDocument.findLineOffset(doc, lineEnd) + columnEnd;
    
    return new HighlightImpl(doc, offsetStart, offsetEnd, attributes);
}
 
Example 5
Source File: HighlightImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static HighlightImpl parse(StyledDocument doc, String line) throws ParseException, BadLocationException {
    MessageFormat f = new MessageFormat("[{0}], {1,number,integer}:{2,number,integer}-{3,number,integer}:{4,number,integer}");
    Object[] args = f.parse(line);
    
    String attributesString = (String) args[0];
    int    lineStart  = ((Long) args[1]).intValue();
    int    columnStart  = ((Long) args[2]).intValue();
    int    lineEnd  = ((Long) args[3]).intValue();
    int    columnEnd  = ((Long) args[4]).intValue();
    
    String[] attrElements = attributesString.split(",");
    List<ColoringAttributes> attributes = new ArrayList<ColoringAttributes>();
    
    for (String a : attrElements) {
        a = a.trim();
        
        attributes.add(ColoringAttributes.valueOf(a));
    }
    
    if (attributes.contains(null))
        throw new NullPointerException();
    
    int offsetStart = NbDocument.findLineOffset(doc, lineStart) + columnStart;
    int offsetEnd = NbDocument.findLineOffset(doc, lineEnd) + columnEnd;
    
    return new HighlightImpl(doc, offsetStart, offsetEnd, attributes);
}
 
Example 6
Source File: LargeMessageFormat.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void testParse() throws ParseException {
    StringBuffer parseTemplate = new StringBuffer();
    StringBuffer parseInput = new StringBuffer();
    for (int i = 0; i < REPEATS; i++) {
        parseTemplate.append("{" + i + ", number} ");
        parseInput.append(i + " ");
    }
    MessageFormat parseFormat = new MessageFormat(parseTemplate.toString());
    Object[] parseResult = parseFormat.parse(parseInput.toString());
    for (int i = 0; i < REPEATS; i++) {
        if (((Number) parseResult[i]).intValue() != i) {
            throw new RuntimeException("got wrong parse result");
        }
    }
}
 
Example 7
Source File: ApiAuthenticationTest.java    From amex-api-java-client-core with Apache License 2.0 5 votes vote down vote up
@Test
public void genHmacHeaders() throws ParseException, NoSuchAlgorithmException, InvalidKeyException, URISyntaxException, MalformedURLException {
    String payload = "{some data}";
    String httpMethod = "GET";
    String clientId = configurationProvider.getValue(ConfigurationKeys.CLIENT_KEY);
    String clientSecret = configurationProvider.getValue(ConfigurationKeys.CLIENT_SECRET);
    String targetURL = configurationProvider.getValue(ConfigurationKeys.BASE_URL);

    AuthProviderBuilder builder = HmacAuthBuilder.getBuilder();
    AuthProvider authProvider = builder.setConfiguration(configurationProvider).build();

    authHeaders = authProvider.generateAuthHeaders(payload, targetURL, httpMethod);


    String pattern = "MAC id=\"{0}\",ts=\"{1}\",nonce=\"{2}\",bodyhash=\"{4}\",mac=\"{3}\"";
    MessageFormat msgFormat = new MessageFormat(pattern);
          
    String header = authHeaders.get(AuthHeaderNames.AUTHORIZATION);
    assertNotNull(header);
    Object[] headers = msgFormat.parse(header);

    assertEquals(5,  headers.length);
    assertEquals(clientId, headers[0]);

    String ts = "" + headers[1];
    String nonce = (String)headers[2];
    String messageHash = (String)headers[4];
    String messageSignature = (String)headers[3];

    validateHMAC(clientSecret, ts, nonce, httpMethod, targetURL, payload, messageHash, messageSignature);
}
 
Example 8
Source File: MetaDataScanner.java    From qaf with MIT License 5 votes vote down vote up
private static boolean matches(String formatStr, String s) {
	MessageFormat messageFormat = new MessageFormat(formatStr);
	try {
		return messageFormat.parse(s).length > 0;
	} catch (ParseException e) {
	}
	return false;
}
 
Example 9
Source File: LargeMessageFormat.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testParse() throws ParseException {
    StringBuffer parseTemplate = new StringBuffer();
    StringBuffer parseInput = new StringBuffer();
    for (int i = 0; i < REPEATS; i++) {
        parseTemplate.append("{" + i + ", number} ");
        parseInput.append(i + " ");
    }
    MessageFormat parseFormat = new MessageFormat(parseTemplate.toString());
    Object[] parseResult = parseFormat.parse(parseInput.toString());
    for (int i = 0; i < REPEATS; i++) {
        if (((Number) parseResult[i]).intValue() != i) {
            throw new RuntimeException("got wrong parse result");
        }
    }
}
 
Example 10
Source File: MessageFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_parse() throws ParseException {
  // Regression for HARMONY-63
  MessageFormat mf = new MessageFormat("{0,number,#,##}", Locale.US);
  Object[] res = mf.parse("1,00,00");
  assertEquals("Assert 0: incorrect size of parsed data ", 1, res.length);
  assertEquals("Assert 1: parsed value incorrectly", new Long(10000), (Long)res[0]);
}
 
Example 11
Source File: JdkEmbeddedXercesSchemaValidationFailureParser.java    From cougar with Apache License 2.0 5 votes vote down vote up
@Override
public CougarException parse(SAXParseException spe, String format, boolean client) {
    String toParse = spe.getMessage();

    // only worth looking through those we've defined
    for (String key : faultCodes.keySet()) {
        MessageFormat mf = new MessageFormat(schemaResourceBundle.getString(key));
        try {
            Object[] args = mf.parse(toParse);
            String result = mf.format(args);
            if (result.equals(toParse)) {
                // we've found the key, if we have a mapping then return the appropriate exception, otherwise no point continuing
                ServerFaultCode sfc = faultCodes.get(key);
                if (sfc == null && deserialisationFailures.contains(key)) {
                    return CougarMarshallingException.unmarshallingException(format, spe.getMessage(), spe, client);
                }
                if (sfc != null) {
                    return new CougarValidationException(sfc, spe);
                }
                return null;
            }
        } catch (ParseException e) {
            // no match
        }
    }
    return null;
}