return JavaStringEscapes.wrapJavaString((String)focus);
/** converts normal string to java escaped for double-quotes and wrapped in those double quotes */ public static String wrapJavaString(String value) { StringBuilder out = new StringBuilder(); try { wrapJavaString(value, out); } catch (IOException e) { //shouldn't happen for string builder throw Exceptions.propagate(e); } return out.toString(); } public static List<String> wrapJavaStrings(Iterable<String> values) {
return (T) wrappedType.getMethod("valueOf", String.class).invoke(null, value); } catch (Exception e) { ClassCoercionException tothrow = new ClassCoercionException("Cannot coerce "+JavaStringEscapes.wrapJavaString(value)+" to "+targetType.getCanonicalName()+" ("+value+"): adapting failed"); tothrow.initCause(e); throw tothrow;
/** returns an object which jersey will handle nicely, converting to json, * sometimes wrapping in quotes if needed (for outermost json return types); * if json is not preferred, this simply applies a toString-style rendering */ public static Object getValueForDisplay(ObjectMapper mapper, Object value, boolean preferJson, boolean isJerseyReturnValue) { if (preferJson) { if (value==null) return null; Object result = value; // no serialization checks required, with new smart-mapper which does toString // (note there is more sophisticated logic in git history however) result = value; if (isJerseyReturnValue) { if (result instanceof String) { // Jersey does not do json encoding if the return type is a string, // expecting the returner to do the json encoding himself // cf discussion at https://github.com/dropwizard/dropwizard/issues/231 result = JavaStringEscapes.wrapJavaString((String)result); } } return result; } else { if (value==null) return ""; return value.toString(); } }
@Test public void testWithEntityLocationsAndStartInLocation() throws Exception { Entity app = createAndStartApplication(loadYaml("test-entity-basic-template.yaml", " location: localhost:(name=localhost name)"), // must pass as JSON list because otherwise the comma confuses the list parser MutableMap.of("locations", "[ "+JavaStringEscapes.wrapJavaString( "byon:(hosts=\"1.1.1.1\", name=\"byon name\")")+" ]") ); waitForApplicationTasks(app); Assert.assertEquals(app.getLocations().size(), 1); Assert.assertEquals(app.getChildren().size(), 1); Entity entity = app.getChildren().iterator().next(); Assert.assertEquals(entity.getLocations().size(), 2); Iterator<Location> entityLocationIterator = entity.getLocations().iterator(); Assert.assertEquals(entityLocationIterator.next().getDisplayName(), "localhost name"); Assert.assertEquals(entityLocationIterator.next().getDisplayName(), "byon name"); Location appLocation = app.getLocations().iterator().next(); Assert.assertEquals(appLocation.getDisplayName(), "byon name"); }
public void testParseString() { assertEquals(new DslParser("\"hello world\"").parse(), new QuotedString(JavaStringEscapes.wrapJavaString("hello world"))); }
@Test public void testJavaEscape() { Assert.assertEquals(JavaStringEscapes.wrapJavaString("Hello \"World\""), "\"Hello \\\"World\\\"\""); }
public static List<String> wrapJavaStrings(Iterable<String> values) { if (values==null) return null; List<String> result = MutableList.of(); for (String v: values) result.add(wrapJavaString(v)); return result; }