本文整理汇总了Java中org.jdom2.output.Format.getPrettyFormat方法的典型用法代码示例。如果您正苦于以下问题:Java Format.getPrettyFormat方法的具体用法?Java Format.getPrettyFormat怎么用?Java Format.getPrettyFormat使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jdom2.output.Format
的用法示例。
在下文中一共展示了Format.getPrettyFormat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sloppyPrint
import org.jdom2.output.Format; //导入方法依赖的package包/类
/**
* Creates an outputter and writes an XML tree.
*
* The encoding argument here only sets an attribute in the
* XML declaration. It's up to the caller to ensure the writer
* is encoding bytes to match. If encoding is null, the default
* is "UTF-8".
*
* If XML character entity escaping is allowed, otherwise unmappable
* characters may be written without errors. If disabled, an
* UnmappableCharacterException will make their presence obvious and fatal.
*
* LineEndings will be CR-LF. Except for comments!?
*
* @param doc
* @param writer
* @param disableEscaping
*/
public static void sloppyPrint( Document doc, Writer writer, String encoding, boolean allowEscaping ) throws IOException {
Format format = Format.getPrettyFormat();
format.setTextMode( Format.TextMode.PRESERVE ); // Permit leading/trailing space.
format.setExpandEmptyElements( false );
format.setOmitDeclaration( false );
format.setIndent( "\t" );
format.setLineSeparator( LineSeparator.CRNL );
if ( encoding != null ) {
format.setEncoding( encoding );
}
if ( !allowEscaping ) {
format.setEscapeStrategy(new EscapeStrategy() {
@Override
public boolean shouldEscape( char ch ) {
return false;
}
});
}
XMLOutputter outputter = new XMLOutputter( format, new SloppyXMLOutputProcessor() );
outputter.output( doc, writer );
}
示例2: toXML
import org.jdom2.output.Format; //导入方法依赖的package包/类
@Test
public void toXML() throws Exception {
JAXBContext jc = JAXBContext.newInstance(MCRNavigationItem.class);
Marshaller m = jc.createMarshaller();
JDOMResult JDOMResult = new JDOMResult();
m.marshal(this.item, JDOMResult);
Element itemElement = JDOMResult.getDocument().getRootElement();
XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
out.output(itemElement, System.out);
assertEquals("template_mysample", itemElement.getAttributeValue("template"));
assertEquals("bold", itemElement.getAttributeValue("style"));
assertEquals("_self", itemElement.getAttributeValue("target"));
assertEquals("intern", itemElement.getAttributeValue("type"));
assertEquals("true", itemElement.getAttributeValue("constrainPopUp"));
assertEquals("false", itemElement.getAttributeValue("replaceMenu"));
assertEquals("item.test.key", itemElement.getAttributeValue("i18nKey"));
Element label1 = itemElement.getChildren().get(0);
Element label2 = itemElement.getChildren().get(1);
assertEquals("Deutschland", label1.getValue());
assertEquals("England", label2.getValue());
}
示例3: toXML
import org.jdom2.output.Format; //导入方法依赖的package包/类
@Test
public void toXML() throws Exception {
JAXBContext jc = JAXBContext.newInstance(MCRNavigation.class);
Marshaller m = jc.createMarshaller();
JDOMResult JDOMResult = new JDOMResult();
m.marshal(this.navigation, JDOMResult);
Element navigationElement = JDOMResult.getDocument().getRootElement();
XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
out.output(navigationElement, System.out);
// test attributes
assertEquals("template_mysample", navigationElement.getAttributeValue("template"));
assertEquals("/content", navigationElement.getAttributeValue("dir"));
assertEquals("History Title", navigationElement.getAttributeValue("historyTitle"));
assertEquals("/content/below/index.xml", navigationElement.getAttributeValue("hrefStartingPage"));
assertEquals("Main Title", navigationElement.getAttributeValue("mainTitle"));
// test children
assertEquals(2, navigationElement.getChildren("menu").size());
assertEquals(1, navigationElement.getChildren("insert").size());
}
示例4: toXML
import org.jdom2.output.Format; //导入方法依赖的package包/类
@Test
public final void toXML() throws IOException {
MCRUserManager.updatePasswordHashToSHA256(this.user, this.user.getPassword());
this.user.setEMail("[email protected]");
this.user.setHint("JUnit Test");
this.user.getSystemRoleIDs().add("admin");
this.user.getSystemRoleIDs().add("editor");
this.user.setLastLogin(new Date());
this.user.setRealName("Test Case");
this.user.getAttributes().put("tel", "555 4812");
this.user.getAttributes().put("street", "Heidestraße 12");
this.user.setOwner(this.user);
MCRUserManager.updateUser(this.user);
startNewTransaction();
assertEquals("Too many users", 1, MCRUserManager.countUsers(null, null, null));
assertEquals("Too many users", 1, MCRUserManager.listUsers(this.user).size());
Document exportableXML = MCRUserTransformer.buildExportableXML(this.user);
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
xout.output(exportableXML, System.out);
}
示例5: writeXML
import org.jdom2.output.Format; //导入方法依赖的package包/类
/**
* Output xml
* @param eRoot - the root element
* @param lang - the language which should be filtered or null for no filter
* @return a string representation of the XML
* @throws IOException
*/
private static String writeXML(Element eRoot, String lang) throws IOException {
StringWriter sw = new StringWriter();
if (lang != null) {
// <label xml:lang="en" text="part" />
XPathExpression<Element> xpE = XPathFactory.instance().compile("//label[@xml:lang!='" + lang + "']",
Filters.element(), null, Namespace.XML_NAMESPACE);
for (Element e : xpE.evaluate(eRoot)) {
e.getParentElement().removeContent(e);
}
}
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
Document docOut = new Document(eRoot.detach());
xout.output(docOut, sw);
return sw.toString();
}
示例6: getMCRObject
import org.jdom2.output.Format; //导入方法依赖的package包/类
/**
* creates a MCRObject instance on base of JDOM document
* @param doc
* MyCoRe object as XML.
* @return
* @throws JDOMException
* exception from underlying {@link MCREditorOutValidator}
* @throws IOException
* exception from underlying {@link MCREditorOutValidator} or {@link XMLOutputter}
* @throws SAXParseException
* @throws MCRException
*/
static MCRObject getMCRObject(Document doc) throws JDOMException, IOException, MCRException, SAXParseException {
String id = doc.getRootElement().getAttributeValue("ID");
MCRObjectID objectID = MCRObjectID.getInstance(id);
MCREditorOutValidator ev = new MCREditorOutValidator(doc, objectID);
Document jdom_out = ev.generateValidMyCoReObject();
if (ev.getErrorLog().size() > 0 && LOGGER.isDebugEnabled()) {
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
StringWriter swOrig = new StringWriter();
xout.output(doc, swOrig);
LOGGER.debug("Input document \n{}", swOrig);
for (String logMsg : ev.getErrorLog()) {
LOGGER.debug(logMsg);
}
StringWriter swClean = new StringWriter();
xout.output(jdom_out, swClean);
LOGGER.debug("Results in \n{}", swClean);
}
return new MCRObject(jdom_out);
}
示例7: generateValidMyCoReObject
import org.jdom2.output.Format; //导入方法依赖的package包/类
/**
* tries to generate a valid MCRObject as JDOM Document.
*
* @return MCRObject
*/
public Document generateValidMyCoReObject() throws JDOMException, SAXParseException, IOException {
MCRObject obj;
// load the JDOM object
XPathFactory.instance()
.compile("/mycoreobject/*/*/*/@editor.output", Filters.attribute())
.evaluate(input)
.forEach(Attribute::detach);
try {
byte[] xml = new MCRJDOMContent(input).asByteArray();
obj = new MCRObject(xml, true);
} catch (SAXParseException e) {
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
LOGGER.warn("Failure while parsing document:\n{}", xout.outputString(input));
throw e;
}
Date curTime = new Date();
obj.getService().setDate("modifydate", curTime);
// return the XML tree
input = obj.createXML();
return input;
}
示例8: getGlobalCostParameters
import org.jdom2.output.Format; //导入方法依赖的package包/类
/**
* Get the global cost parameters
*
* @param request the HTTP Request
* @return the HTTP Response
*/
@GET
@Path("/remediation-cost-parameters/global")
@Produces(MediaType.APPLICATION_JSON)
public Response getGlobalCostParameters(@Context HttpServletRequest request) {
String costParametersFolderPath = ProjectProperties.getProperty("cost-parameters-path");
GlobalParameters globalParameters = new GlobalParameters();
try {
globalParameters.loadFromXMLFile(costParametersFolderPath + "/" + GlobalParameters.FILE_NAME);
} catch (Exception e) {
return RestApplication.returnErrorMessage(request, "The global parameters " +
"can not be load: " + e.getMessage());
}
XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
return RestApplication.returnJsonObject(request, XML.toJSONObject(output.outputString(globalParameters.toDomElement())));
}
示例9: createOPFContent
import org.jdom2.output.Format; //导入方法依赖的package包/类
public static byte[] createOPFContent(Book book) throws IllegalArgumentException, IllegalStateException
{
Document opfDocument = write(book);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLOutputter outputter = new XMLOutputter();
Format xmlFormat = Format.getPrettyFormat();
outputter.setFormat(xmlFormat);
try
{
outputter.output(opfDocument, baos);
}
catch (IOException e)
{
logger.error("", e);
}
return baos.toByteArray();
}
示例10: getAttackGraph
import org.jdom2.output.Format; //导入方法依赖的package包/类
/**
* Get the whole attack graph
*
* @param request the HTTP Request
* @return the HTTP Response
*/
@GET
@Path("attack_graph")
@Produces(MediaType.APPLICATION_JSON)
public Response getAttackGraph(@Context HttpServletRequest request) {
Monitoring monitoring = ((Monitoring) request.getSession(true).getAttribute("monitoring"));
if (monitoring == null) {
return RestApplication.returnErrorMessage(request, "The monitoring object is empty. Did you forget to " +
"initialize it ?");
}
Element attackGraphXML = monitoring.getAttackGraph().toDomElement();
XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
return RestApplication.returnJsonObject(request, XML.toJSONObject(output.outputString(attackGraphXML)));
}
示例11: wrap
import org.jdom2.output.Format; //导入方法依赖的package包/类
private String wrap(Object text) {
if (text != null) {
if (text instanceof Attribute) {
return StringEscapeUtils.unescapeHtml4(((Attribute) text).getValue());
} else if (text instanceof Content) {
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
if (text instanceof Element) {
return StringEscapeUtils.unescapeHtml4(xout.outputString((Element) text));
}
if (text instanceof Text) {
return StringEscapeUtils.unescapeHtml4(xout.outputString((Text) text));
}
} else {
LOGGER.error("unsupported document type", text.getClass());
}
}
return "";
}
示例12: repair
import org.jdom2.output.Format; //导入方法依赖的package包/类
public static String repair(String originalHtml)
{
String content = null;
try
{
HtmlCleaner cleaner = createHtmlCleaner();
//wir probieren es erstmal mit UTF-8
TagNode rootNode = cleaner.clean(originalHtml);
Document jdomDocument = new JDomSerializer(cleaner.getProperties(), false).createJDom(rootNode);
jdomDocument.setDocType(Constants.DOCTYPE_XHTML.clone());
XMLOutputter outputter = new XMLOutputter();
Format xmlFormat = Format.getPrettyFormat();
outputter.setFormat(xmlFormat);
outputter.setXMLOutputProcessor(new XHTMLOutputProcessor());
content = outputter.outputString(jdomDocument);
}
catch (IllegalAddException e)
{
logger.error("", e);
}
return content;
}
示例13: createXml
import org.jdom2.output.Format; //导入方法依赖的package包/类
private void createXml(ISendMessageCallback callback) {
Document generatedXml = new Document();
Stack<DocElement> parentStack = new Stack<DocElement>();
ArrayList<String> outboundPayload = new ArrayList<String>();
for (Message msg : messagesToProcess) {
processMsgEntities(parentStack, msg, generatedXml);
}
XMLOutputter xmlOutputter = new XMLOutputter();
Format format = null;
if (xmlFormat.equals(COMPACT_FORMAT)) {
format = Format.getCompactFormat();
} else if (xmlFormat.equals(RAW_FORMAT)) {
format = Format.getRawFormat();
} else {
format = Format.getPrettyFormat();
}
xmlOutputter.setFormat(format);
outboundPayload.add(xmlOutputter.outputString(generatedXml));
callback.sendTextMessage(null, outboundPayload);
}
示例14: buildSettingsXML
import org.jdom2.output.Format; //导入方法依赖的package包/类
/**
* Read {@code baseFilename.settings.xml} into a string if it exists.
*
* @return contents of {@code baseFilename.settings.xml} or {@code null} if
* that file couldn't be read.
*/
private static String buildSettingsXML( final String baseFilename )
{
final String settings = baseFilename + ".settings.xml";
if ( new File( settings ).exists() )
{
try
{
final SAXBuilder sax = new SAXBuilder();
final Document doc = sax.build( settings );
final XMLOutputter xout = new XMLOutputter( Format.getPrettyFormat() );
final StringWriter sw = new StringWriter();
xout.output( doc, sw );
return sw.toString();
}
catch ( JDOMException | IOException e )
{
LOG.warn( "Could not read settings file \"" + settings + "\"" );
LOG.warn( e.getMessage() );
}
}
return null;
}
示例15: buildJnlpResponse
import org.jdom2.output.Format; //导入方法依赖的package包/类
protected String buildJnlpResponse(JnlpTemplate launcher) throws ServletErrorException {
launcher.rootElt.setAttribute(JNLP_TAG_ATT_CODEBASE,
ServletUtil.getFirstParameter(launcher.parameterMap.get(PARAM_CODEBASE)));
launcher.rootElt.removeAttribute(JNLP_TAG_ATT_HREF); // this tag has not to be used inside dynamic JNLP
handleRequestPropertyParameter(launcher);
handleRequestArgumentParameter(launcher);
filterRequestParameterMarker(launcher);
String outputStr = null;
try {
Format format = Format.getPrettyFormat();
// Converts native encodings to ASCII with escaped Unicode like (ô è é...),
// necessary for jnlp
format.setEncoding("US-ASCII");
outputStr = new XMLOutputter(format).outputString(launcher.rootElt);
} catch (Exception e) {
throw new ServletErrorException(HttpServletResponse.SC_NOT_MODIFIED, "Can't build Jnlp launcher ", e);
}
return outputStr;
}