Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table: Reduce characters that get encoded to just illegal in XML ones #258

Merged
merged 3 commits into from
Feb 22, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Copyright Microsoft Corporation
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -84,8 +84,7 @@ public void write(XMLStreamWriter writer) throws XMLStreamException {

if (value != null) {
writer.writeCharacters(value);
}
else {
} else {
writer.writeAttribute("m:null", "true");
}

Expand All @@ -108,8 +107,7 @@ public List<TableEntry> parseTableEntries(InputStream stream) {
// Process "entry" elements only
if (isStartElement(xmlr, "entry")) {
result.add(parseTableEntry(xmlr));
}
else {
} else {
nextSignificant(xmlr);
}
}
Expand All @@ -118,8 +116,7 @@ public List<TableEntry> parseTableEntries(InputStream stream) {
expect(xmlr, XMLStreamConstants.END_DOCUMENT);

return result;
}
catch (XMLStreamException e) {
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}
Expand All @@ -133,8 +130,7 @@ public TableEntry parseTableEntry(InputStream stream) {
expect(xmlr, XMLStreamConstants.END_DOCUMENT);

return result;
}
catch (XMLStreamException e) {
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}
Expand All @@ -151,8 +147,7 @@ public List<Entity> parseEntityEntries(InputStream stream) {
// Process "entry" elements only
if (isStartElement(xmlr, "entry")) {
result.add(parseEntityEntry(xmlr));
}
else {
} else {
nextSignificant(xmlr);
}
}
Expand All @@ -161,8 +156,7 @@ public List<Entity> parseEntityEntries(InputStream stream) {
expect(xmlr, XMLStreamConstants.END_DOCUMENT);

return result;
}
catch (XMLStreamException e) {
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}
Expand All @@ -176,8 +170,7 @@ public Entity parseEntityEntry(InputStream stream) {
expect(xmlr, XMLStreamConstants.END_DOCUMENT);

return result;
}
catch (XMLStreamException e) {
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}
Expand Down Expand Up @@ -227,8 +220,7 @@ private InputStream generateEntry(PropertiesWriter propertiesWriter) {
writer.close();

return new ByteArrayInputStream(stream.toByteArray());
}
catch (XMLStreamException e) {
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}
Expand All @@ -243,8 +235,7 @@ private TableEntry parseTableEntry(XMLStreamReader xmlr) throws XMLStreamExcepti
Map<String, Property> properties = parseEntryProperties(xmlr);

result.setName((String) properties.get("TableName").getValue());
}
else {
} else {
nextSignificant(xmlr);
}
}
Expand All @@ -263,8 +254,7 @@ private Entity parseEntityEntry(XMLStreamReader xmlr) throws XMLStreamException
while (!isEndElement(xmlr, "entry")) {
if (isStartElement(xmlr, "properties")) {
result.setProperties(parseEntryProperties(xmlr));
}
else {
} else {
nextSignificant(xmlr);
}
}
Expand Down Expand Up @@ -336,17 +326,21 @@ private void expect(XMLStreamReader xmlr, int eventType, String localName) throw
private String encodeNumericCharacterReference(String value) {
if (value == null) {
return null;
}
else {
} else {
char[] charArray = value.toCharArray();
StringBuffer stringBuffer = new StringBuffer();
for (int index = 0; index < charArray.length; index++) {
if (charArray[index] < 0x20 || charArray[index] > 0x7f)
if (isIllegalChar(charArray[index]))
stringBuffer.append("&#x").append(Integer.toHexString(charArray[index])).append(";");
else
stringBuffer.append(charArray[index]);
}
return stringBuffer.toString();
}
}

private boolean isIllegalChar(char c) {
return !(c == 9 || c == 0xA || c == 0xD || (c >= 0x20 && c < 0xD800) ||
(c >= 0xE000 && c < 0xFFFE) || (c >= 0x10000 && c < 0x110000));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -377,22 +377,81 @@ public void insertEntityEscapeCharactersWorks() throws Exception {

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

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

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

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

}

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

String partition = "001";
String row = "insertEntityEscapeCharactersRoundTripsFromService";
Entity insertedEntity = new Entity().setPartitionKey(partition).setRowKey(row)
.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")
.setProperty("test8", EdmType.STRING, "\uD840");
;

service.insertEntity(TEST_TABLE_2, insertedEntity);

GetEntityResult result = service.getEntity(TEST_TABLE_2, "001", "insertEntityEscapeCharactersRoundTripsFromService");
assertNotNull(result);

Entity entity = result.getEntity();

assertNotNull(entity.getProperty("test"));
String actualTest1 = (String) entity.getPropertyValue("test");
assertEquals("&#x5;", actualTest1);

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

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

assertNotNull(result.getEntity().getProperty("test4"));
String actualTest4 = (String) result.getEntity().getPropertyValue("test4");
assertEquals("\uaaaa", actualTest4);

assertNotNull(result.getEntity().getProperty("test5"));
String actualTest5 = (String) result.getEntity().getPropertyValue("test5");
assertEquals("\ub2e2", actualTest5);

assertNotNull(result.getEntity().getProperty("test6"));
String actualTest6 = (String) result.getEntity().getPropertyValue("test6");
assertEquals(" \ub2e2", actualTest6);

assertNotNull(result.getEntity().getProperty("test7"));
String actualTest7 = (String) result.getEntity().getPropertyValue("test7");
assertEquals("ok \ub2e2", actualTest7);

String actualTest8 = (String)entity.getPropertyValue("test8");
assertEquals("&#xd840;", actualTest8);
}


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