Search in sources :

Example 71 with Format

use of org.jdom2.output.Format in project mycore by MyCoRe-Org.

the class MCRRestAPIClassifications method writeChildrenAsJSONCBTree.

/**
 * output children in JSON format used as input for Dijit Checkbox Tree
 *
 * @param eParent - the parent xml element
 * @param writer - the JSON writer
 * @param lang - the language to be filtered or null if all languages should be displayed
 *
 * @throws IOException
 */
private static void writeChildrenAsJSONCBTree(Element eParent, JsonWriter writer, String lang, boolean checked) throws IOException {
    writer.beginArray();
    for (Element e : eParent.getChildren("category")) {
        writer.beginObject();
        writer.name("ID").value(e.getAttributeValue("ID"));
        for (Element eLabel : e.getChildren("label")) {
            if (lang == null || lang.equals(eLabel.getAttributeValue("lang", Namespace.XML_NAMESPACE))) {
                writer.name("text").value(eLabel.getAttributeValue("text"));
            }
        }
        writer.name("checked").value(checked);
        if (e.getChildren("category").size() > 0) {
            writer.name("children");
            writeChildrenAsJSONCBTree(e, writer, lang, checked);
        }
        writer.endObject();
    }
    writer.endArray();
}
Also used : Element(org.jdom2.Element)

Example 72 with Format

use of org.jdom2.output.Format in project mycore by MyCoRe-Org.

the class MCRRestAPIMessages method getMessage.

/**
 * returns a single messages entry.
 *
 * @param info - the injected Jersey context object for URI
 * @param request - the injected HTTPServletRequest object
 * @param key - the message key
 * @param lang - the language
 * @param format
 *     Possible values are: props (default) | json | xml (required)
 * @return a Jersey Response Object
 * @throws MCRRestAPIException
 */
@GET
@Path("/{value}")
@Produces({ MediaType.TEXT_XML + ";charset=UTF-8", MediaType.APPLICATION_JSON + ";charset=UTF-8", MediaType.TEXT_PLAIN + ";charset=UTF-8" })
public Response getMessage(@Context UriInfo info, @Context HttpServletRequest request, @PathParam("value") String key, @QueryParam("lang") @DefaultValue("de") String lang, @QueryParam("format") @DefaultValue("text") String format) throws MCRRestAPIException {
    MCRRestAPIUtil.checkRestAPIAccess(request, MCRRestAPIACLPermission.READ, "/v1/messages");
    Locale locale = Locale.forLanguageTag(lang);
    String result = MCRTranslation.translate(key, locale);
    String authHeader = MCRJSONWebTokenUtil.createJWTAuthorizationHeader(MCRJSONWebTokenUtil.retrieveAuthenticationToken(request));
    try {
        if (FORMAT_PROPERTY.equals(format)) {
            return Response.ok(key + "=" + result).type("text/plain; charset=ISO-8859-1").header(HEADER_NAME_AUTHORIZATION, authHeader).build();
        }
        if (FORMAT_XML.equals(format)) {
            Document doc = new Document();
            Element root = new Element("entry");
            root.setAttribute("key", key);
            root.setText(result);
            doc.addContent(root);
            StringWriter sw = new StringWriter();
            XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
            outputter.output(doc, sw);
            return Response.ok(sw.toString()).type("application/xml; charset=UTF-8").header(HEADER_NAME_AUTHORIZATION, authHeader).build();
        }
        if (FORMAT_JSON.equals(format)) {
            StringWriter sw = new StringWriter();
            JsonWriter writer = new JsonWriter(sw);
            writer.setIndent("    ");
            writer.beginObject();
            writer.name(key);
            writer.value(result);
            writer.endObject();
            writer.close();
            return Response.ok(sw.toString()).type("application/json; charset=UTF-8").header(HEADER_NAME_AUTHORIZATION, authHeader).build();
        }
        // text only
        return Response.ok(result).type("text/plain; charset=UTF-8").header(HEADER_NAME_AUTHORIZATION, authHeader).build();
    } catch (IOException e) {
    // toDo
    }
    return Response.status(Status.BAD_REQUEST).build();
}
Also used : Locale(java.util.Locale) XMLOutputter(org.jdom2.output.XMLOutputter) StringWriter(java.io.StringWriter) Element(org.jdom2.Element) IOException(java.io.IOException) Document(org.jdom2.Document) JsonWriter(com.google.gson.stream.JsonWriter) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 73 with Format

use of org.jdom2.output.Format in project dq-easy-cloud by dq-open-cloud.

the class XmlUtilsTest method testAddXmlElement.

/**
 * 新增节点
 * @throws Exception
 */
@Test
public void testAddXmlElement() throws Exception {
    // 获取根元素
    Element root = doc.getRootElement();
    // 设置新增的person的信息
    Element newEle = new Element("result");
    newEle.setAttribute("column", "update_by");
    newEle.setAttribute("jdbcType", "VARCHAR");
    newEle.setAttribute("property", "updateBy");
    Element resultMap = root.getChild("resultMap");
    List<Element> resultMaps = resultMap.getChildren();
    // 1:获取其他
    List<Element> others = new ArrayList();
    for (int i = 0; i < resultMaps.size(); ++i) {
        if (!resultMaps.get(i).getName().equals("result") && !resultMaps.get(i).getName().equals("id")) {
            others.add(resultMaps.get(i));
        }
    }
    List<Element> results = new ArrayList<>();
    // 设置新增的person的信息
    Element idElement = new Element("id");
    idElement.setAttribute("column", "id");
    idElement.setAttribute("jdbcType", "INTEGER");
    idElement.setAttribute("property", "id");
    results.add(idElement);
    for (int i = 0; i < 5; ++i) {
        // 设置新增的person的信息
        Element resultElement = new Element("result");
        resultElement.setAttribute("column", "customer_type" + i);
        resultElement.setAttribute("jdbcType", "INTEGER");
        resultElement.setAttribute("property", "id");
        results.add(resultElement);
    }
    results.addAll(others);
    resultMaps.clear();
    resultMaps.addAll(results);
    System.out.println("result.size" + resultMaps.size());
    System.out.println("new:---" + JSONObject.toJSONString(resultMaps.get(0).clone()));
    System.out.println("result.size" + resultMaps.size());
    Format format = Format.getPrettyFormat();
    format.setIndent("    ");
    format.setEncoding("UTF-8");
    XMLOutputter out = new XMLOutputter(format);
    // 写文件
    out.output(doc, new FileWriter(xmlFile));
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Format(org.jdom2.output.Format) Element(org.jdom2.Element) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Element (org.jdom2.Element)58 XMLOutputter (org.jdom2.output.XMLOutputter)22 Document (org.jdom2.Document)19 IOException (java.io.IOException)15 Format (org.jdom2.output.Format)10 StringWriter (java.io.StringWriter)9 Attribute (org.jdom2.Attribute)9 ArrayList (java.util.ArrayList)7 File (java.io.File)5 FileWriter (java.io.FileWriter)5 Test (org.junit.Test)5 MCRRestAPIError (org.mycore.restapi.v1.errors.MCRRestAPIError)5 MCRRestAPIException (org.mycore.restapi.v1.errors.MCRRestAPIException)5 JsonWriter (com.google.gson.stream.JsonWriter)4 Date (java.util.Date)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 SimpleDateFormat (java.text.SimpleDateFormat)3 JDOMException (org.jdom2.JDOMException)3 FileOutputStream (java.io.FileOutputStream)2 MalformedURLException (java.net.MalformedURLException)2