Skip to content

Commit

Permalink
Merge pull request #9 from gcheng/Jerusalem
Browse files Browse the repository at this point in the history
encode special characters when serializing entity to XML
  • Loading branch information
Albert Cheng committed Aug 2, 2012
2 parents 3a646ab + 4a759eb commit 17cf30a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public void write(XMLStreamWriter writer) throws XMLStreamException {
}

String value = edmValueConverter.serialize(edmType, entry.getValue().getValue());

if ((edmType != null) && (edmType == "Edm.String")) {
value = encodeNumericCharacterReference(value);
}

if (value != null) {
writer.writeCharacters(value);
}
Expand Down Expand Up @@ -327,4 +332,21 @@ private void expect(XMLStreamReader xmlr, int eventType, String localName) throw
xmlr.require(eventType, null, localName);
nextSignificant(xmlr);
}

private String encodeNumericCharacterReference(String value) {
if (value == null) {
return null;
}
else {
char[] charArray = value.toCharArray();
StringBuffer stringBuffer = new StringBuffer();
for (int index = 0; index < charArray.length; index++) {
if (charArray[index] < 0x20 || charArray[index] > 0x7f)
stringBuffer.append("&#x").append(Integer.toHexString(charArray[index])).append(";");
else
stringBuffer.append(charArray[index]);
}
return stringBuffer.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,60 @@ public void insertEntityWorks() throws Exception {
assertEquals(uuid.toString(), result.getEntity().getProperty("test7").getValue().toString());
}

@Test
public void insertEntityEscapeCharactersWorks() throws Exception {
// Arrange
Configuration config = createConfiguration();
TableContract service = TableService.create(config);

Entity entity = new Entity().setPartitionKey("001").setRowKey("insertEntityEscapeCharactersWorks")
.setProperty("test", EdmType.STRING, "\u0005").setProperty("test2", EdmType.STRING, "\u0011")
.setProperty("test3", EdmType.STRING, "\u0025").setProperty("test4", EdmType.STRING, "\uaaaa")
.setProperty("test5", EdmType.STRING, "\ub2e2").setProperty("test6", EdmType.STRING, " \ub2e2")
.setProperty("test7", EdmType.STRING, "ok \ub2e2");

// Act
InsertEntityResult result = service.insertEntity(TEST_TABLE_2, entity);

// Assert
assertNotNull(result);
assertNotNull(result.getEntity());

assertEquals("001", result.getEntity().getPartitionKey());
assertEquals("insertEntityEscapeCharactersWorks", result.getEntity().getRowKey());
assertNotNull(result.getEntity().getTimestamp());
assertNotNull(result.getEntity().getEtag());

assertNotNull(result.getEntity().getProperty("test"));
String actualTest1 = (String) result.getEntity().getProperty("test").getValue();
assertEquals("&#x5;", actualTest1);

assertNotNull(result.getEntity().getProperty("test2"));
String actualTest2 = (String) result.getEntity().getProperty("test2").getValue();
assertEquals("&#x11;", actualTest2);

assertNotNull(result.getEntity().getProperty("test3"));
String actualTest3 = (String) result.getEntity().getProperty("test3").getValue();
assertEquals("%", actualTest3);

assertNotNull(result.getEntity().getProperty("test4"));
String actualTest4 = (String) result.getEntity().getProperty("test4").getValue();
assertEquals("&#xaaaa;", actualTest4);

assertNotNull(result.getEntity().getProperty("test5"));
String actualTest5 = (String) result.getEntity().getProperty("test5").getValue();
assertEquals("&#xb2e2;", actualTest5);

assertNotNull(result.getEntity().getProperty("test6"));
String actualTest6 = (String) result.getEntity().getProperty("test6").getValue();
assertEquals(" &#xb2e2;", actualTest6);

assertNotNull(result.getEntity().getProperty("test7"));
String actualTest7 = (String) result.getEntity().getProperty("test7").getValue();
assertEquals("ok &#xb2e2;", actualTest7);

}

@Test
public void updateEntityWorks() throws Exception {
System.out.println("updateEntityWorks()");
Expand Down

0 comments on commit 17cf30a

Please sign in to comment.