Skip to content

Commit

Permalink
fix bug and add escapingSpecialCharacters
Browse files Browse the repository at this point in the history
  • Loading branch information
aoliaoaoaojiao committed Nov 16, 2022
1 parent 3681d8e commit 2025707
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.cloud.sonic.driver.poco.util.pocoJsonToXml;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;

import java.util.*;
Expand Down Expand Up @@ -107,8 +108,8 @@ public String pageSourceForJsonString() throws SonicRespException {
@Override
public Element pageSourceForXmlElement() throws SonicRespException {
pageSourceForJsonString();
String pocoJson = "{\"Root\"" + source.substring("{\"result\"".length());
Element rootXmlElement = Jsoup.parse(pocoJsonToXml.jsonObjToXml(JSON.parseObject(pocoJson).getJSONObject("Root")));
// String pocoJson = "{\"Root\"" + source.substring("{\"result\"".length());
Element rootXmlElement = Jsoup.parse(pocoJsonToXml.jsonObjToXml(JSON.parseObject(source).getJSONObject("result")),"", Parser.xmlParser());

rootNode.updateVersion(rootXmlElement);

Expand All @@ -118,7 +119,7 @@ public Element pageSourceForXmlElement() throws SonicRespException {
@Override
public PocoElement findElement(String selector, String expression) throws SonicRespException {
List<PocoElement> pocoElements = findElements(selector, expression);
return pocoElements.size() <= 0 ? null : pocoElements.get(0);
return pocoElements.get(0);
}

@Override
Expand All @@ -142,14 +143,18 @@ public List<PocoElement> findElements(String selector, String expression) throws
}
}
}
expression = newExpress;
xmlNodes = rootNode.getXmlElement().selectXpath(newExpress);
break;
case "xpath":
xmlNodes = rootNode.getXmlElement().selectXpath(expression);
break;
case "cssSelector":
xmlNodes = rootNode.getXmlElement().select(expression);
break;
}
if (xmlNodes == null ||xmlNodes.isEmpty()){
throw new SonicRespException(String.format("poco element not found for selector:%s, value:%s",selector,expression));
}
List<PocoElement> result = new ArrayList<>();
for (Element node : xmlNodes) {
PocoElement pocoElement = new PocoElement(rootNode, node);
Expand Down
51 changes: 45 additions & 6 deletions src/main/java/org/cloud/sonic/driver/poco/util/pocoJsonToXml.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (C) [SonicCloudOrg] Sonic Project
*
* 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:https://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.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.cloud.sonic.driver.poco.util;

import com.alibaba.fastjson.JSONArray;
Expand All @@ -19,7 +35,7 @@ public static String jsonObjToXml(JSONObject jo){
}

/**
* json对象转xml
* json object to xml
*
* @param jo JSONObject
* @param gt "\n" shifter
Expand All @@ -36,7 +52,12 @@ private static String jsonToXml(JSONObject jo, String gt) {

xmlStr.append(gt);
xmlStr.append("<");
xmlStr.append(payload.get("name").toString());
String name = String.join("__",payload.get("name").toString().split(" "));

if (name.equals("<Root>"))name = "Root";
name = escapingSpecialCharacters(name);

xmlStr.append(name);
xmlStr.append(getAttrStr(payload));
xmlStr.append(">\n");
if (children!=null){
Expand All @@ -45,6 +66,10 @@ private static String jsonToXml(JSONObject jo, String gt) {
xmlStr.append(jsonToXml(child, gt + "\t"));
}
}
xmlStr.append(gt);
xmlStr.append("</");
xmlStr.append(name);
xmlStr.append(">\n");
} catch (Exception e) {
return "<error>1</error>";
}
Expand All @@ -60,12 +85,26 @@ public static StringBuilder getAttrStr(JSONObject payload){
String val = entry.getValue().toString();
if (key.equals("zOrders")){
JSONObject zOrders = JSONObject.parseObject(val);
sb.append(String.format("global:\"%s\" ", zOrders.get("global")));
sb.append(String.format("local:\"%s\" ", zOrders.get("local")));
}else {
sb.append(String.format("%s:\"%s\" ", key, val));
sb.append(String.format("global=\"%s\" ", zOrders.get("global")));
sb.append(String.format("local=\"%s\" ", zOrders.get("local")));
}else if (key.equals("components")){
val = val.replace("\"","");
sb.append(String.format("%s=\"%s\" ", key, val));
} else {
val = escapingSpecialCharacters(val);
sb.append(String.format("%s=\"%s\" ", key, val));
}
}
return sb;
}

private static String escapingSpecialCharacters(String originStr){
if (originStr==null)return null;
originStr = originStr.replace("&","&amp;");
originStr = originStr.replace("<","&lt;");
originStr = originStr.replace(">","&gt;");
originStr = originStr.replace("\"","&quot;");
originStr = originStr.replace("'","&apos;");
return originStr;
}
}
8 changes: 4 additions & 4 deletions src/test/java/org/cloud/sonic/driver/poco/PocoDriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ public static void beforeClass() {

@Test
public void testPageSource() throws SonicRespException {
Assert.assertEquals("Root", pocoDriver.getPageSource().getPayload().getType());
// Assert.assertEquals("Root", pocoDriver.getPageSource().getPayload().getType());
Assert.assertTrue(pocoDriver.getPageSourceForJsonString().length() > 0);
Assert.assertNotNull(pocoDriver.getPageSourceForXmlElement().toString());
System.out.println(pocoDriver.getPageSourceForXmlElement().toString());
}

@Test
public void testFindElement() throws SonicRespException {
// String expression = "poco(\"playDragAndDrop\").child(\"star\")[0]";
String expression = "star";
List<PocoElement> pocoElements = pocoDriver.findElements(PocoSelector.CSS_SELECTOR, expression);
String expression = "poco(\"playDragAndDrop\").child(\"star\")[4]";
// String expression = "star";
List<PocoElement> pocoElements = pocoDriver.findElements(PocoSelector.POCO, expression);
System.out.println(pocoElements.size());
for (PocoElement pocoElement : pocoElements) {
Assert.assertNotNull(pocoElement.getPayload().getName());
Expand Down

0 comments on commit 2025707

Please sign in to comment.