下面列出了java.text.MessageFormat#parse ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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");
}
}
}
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");
}
}
}
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");
}
}
}
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);
}
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);
}
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");
}
}
}
@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);
}
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;
}
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");
}
}
}
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]);
}
@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;
}