Processus de workflow personnalisé pour convertir JSON en XML
Le code fourni convertit JSON en XML, stockant le XML résultant dans une variable de processus de workflow nommée dataXml
.
import org.slf4j.LoggerFactory;
import com.adobe.granite.workflow.WorkflowException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.jcr.Node;
import javax.jcr.Session;
import org.json.JSONObject;
import org.json.XML;
import org.slf4j.Logger;
import org.osgi.service.component.annotations.Component;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
@Component(property = {
"service.description=Convert JSON to XML",
"process.label=Convert JSON to XML"
})
public class ConvertJSONToXML implements WorkflowProcess {
private static final Logger log = LoggerFactory.getLogger(ConvertJSONToXML.class);
@Override
public void execute(final WorkItem workItem, final WorkflowSession workflowSession, final MetaDataMap arg2) throws WorkflowException {
String processArgs = arg2.get("PROCESS_ARGS", "string");
log.debug("The process argument I got was " + processArgs);
String submittedDataFile = processArgs;
String payloadPath = workItem.getWorkflowData().getPayload().toString();
log.debug("The payload in convert json to xml " + payloadPath);
String dataFilePath = payloadPath + "/" + submittedDataFile + "/jcr:content";
try {
Session session = workflowSession.adaptTo(Session.class);
Node submittedJsonDataNode = session.getNode(dataFilePath);
InputStream jsonDataStream = submittedJsonDataNode.getProperty("jcr:data").getBinary().getStream();
BufferedReader streamReader = new BufferedReader(new InputStreamReader(jsonDataStream, "UTF-8"));
StringBuilder stringBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null) {
stringBuilder.append(inputStr);
}
JSONObject submittedJson = new JSONObject(stringBuilder.toString());
log.debug(submittedJson.toString());
String xmlString = XML.toString(submittedJson);
log.debug("The json converted to XML " + xmlString);
MetaDataMap metaDataMap = workItem.getWorkflow().getWorkflowData().getMetaDataMap();
metaDataMap.put("xmlData", xmlString);
} catch (Exception e) {
log.error("Error converting JSON to XML: " + e.getMessage(), e);
}
}
}