本文整理汇总了Java中org.jdom2.output.Format.setEncoding方法的典型用法代码示例。如果您正苦于以下问题:Java Format.setEncoding方法的具体用法?Java Format.setEncoding怎么用?Java Format.setEncoding使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jdom2.output.Format
的用法示例。
在下文中一共展示了Format.setEncoding方法的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: getStringFromElement
import org.jdom2.output.Format; //导入方法依赖的package包/类
/**
*
* @param element
* @param encoding
* @return
*/
public static String getStringFromElement(Element element, String encoding) {
if (element == null) {
throw new IllegalArgumentException("element may not be null");
}
if (encoding == null) {
encoding = DEFAULT_ENCODING;
}
Format format = Format.getRawFormat();
XMLOutputter outputter = new XMLOutputter(format);
Format xmlFormat = outputter.getFormat();
if (StringUtils.isNotEmpty(encoding)) {
xmlFormat.setEncoding(encoding);
}
xmlFormat.setExpandEmptyElements(true);
outputter.setFormat(xmlFormat);
String docString = outputter.outputString(element);
return docString;
}
示例3: writeXmlFile
import org.jdom2.output.Format; //导入方法依赖的package包/类
private void writeXmlFile( String pXmlLogFile, Document pDocument )
{
try
{
File lXmlLogFile = new File( pXmlLogFile );
if( !lXmlLogFile.getParentFile().exists() )
{
lXmlLogFile.getParentFile().mkdirs();
}
Format lFormat = Format.getPrettyFormat();
lFormat.setEncoding( _parameters.getEncodingForSqlLog().name() );
new XMLOutputter( lFormat ).output( pDocument, new FileOutputStream( lXmlLogFile ) );
}
catch( Exception e )
{
throw new RuntimeException( e );
}
}
示例4: 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;
}
示例5: writeXML
import org.jdom2.output.Format; //导入方法依赖的package包/类
public static void writeXML(Document doc, String path, String encoding) throws IOException{
//create path
PathUtil.mkDirs(path);
FileWriter writer = new FileWriter(path);
Format format = Format.getPrettyFormat();
format.setEncoding(encoding);
XMLOutputter outputter = new XMLOutputter(format);
outputter.output(doc, writer);
writer.close();
//test if file was created
File file = new File(path);
if(!file.isFile()){
throw new IOException("Not a file path: "+ path);
}
}
示例6: wrongSchema
import org.jdom2.output.Format; //导入方法依赖的package包/类
/**
* @param response
* @param string
* @throws IOException
*/
private static void wrongSchema(HttpServletResponse response, String parameter) throws IOException {
Element searchRetrieveResponse = new Element("searchRetrieveResponse", SRU_NAMESPACE);
Element version = new Element("version", SRU_NAMESPACE);
version.setText("1.2");
searchRetrieveResponse.addContent(version);
Element diagnostic = new Element("diagnostic", SRU_NAMESPACE);
searchRetrieveResponse.addContent(diagnostic);
Element uri = new Element("uri", DIAG_NAMESPACE);
uri.setText("info:srw/diagnostic/1/66");
diagnostic.addContent(uri);
Element details = new Element("details", DIAG_NAMESPACE);
details.setText(" Unknown schema for retrieval");
diagnostic.addContent(details);
Element message = new Element("message", DIAG_NAMESPACE);
message.setText("Unknown schema for retrieval / " + parameter);
diagnostic.addContent(message);
Document doc = new Document();
doc.setRootElement(searchRetrieveResponse);
Format format = Format.getPrettyFormat();
format.setEncoding("utf-8");
XMLOutputter xmlOut = new XMLOutputter(format);
xmlOut.output(doc, response.getOutputStream());
}
示例7: missingArgument
import org.jdom2.output.Format; //导入方法依赖的package包/类
private static void missingArgument(HttpServletResponse response, String parameter) throws IOException {
Element searchRetrieveResponse = new Element("searchRetrieveResponse", SRU_NAMESPACE);
Element version = new Element("version", SRU_NAMESPACE);
version.setText("1.2");
searchRetrieveResponse.addContent(version);
Element diagnostic = new Element("diagnostic", SRU_NAMESPACE);
searchRetrieveResponse.addContent(diagnostic);
Element uri = new Element("uri", DIAG_NAMESPACE);
uri.setText("info:srw/diagnostic/1/7");
diagnostic.addContent(uri);
Element details = new Element("details", DIAG_NAMESPACE);
details.setText("Mandatory parameter not supplied");
diagnostic.addContent(details);
Element message = new Element("message", DIAG_NAMESPACE);
message.setText("Mandatory parameter not supplied / " + parameter);
diagnostic.addContent(message);
Document doc = new Document();
doc.setRootElement(searchRetrieveResponse);
Format format = Format.getPrettyFormat();
format.setEncoding("utf-8");
XMLOutputter xmlOut = new XMLOutputter(format);
xmlOut.output(doc, response.getOutputStream());
}
示例8: unsupportedOperation
import org.jdom2.output.Format; //导入方法依赖的package包/类
private static void unsupportedOperation(HttpServletResponse response, String parameter) throws IOException {
Element searchRetrieveResponse = new Element("searchRetrieveResponse", SRU_NAMESPACE);
Element version = new Element("version", SRU_NAMESPACE);
version.setText("1.2");
searchRetrieveResponse.addContent(version);
Element diagnostic = new Element("diagnostic", SRU_NAMESPACE);
searchRetrieveResponse.addContent(diagnostic);
Element uri = new Element("uri", DIAG_NAMESPACE);
uri.setText("info:srw/diagnostic/1/4");
diagnostic.addContent(uri);
Element details = new Element("details", DIAG_NAMESPACE);
details.setText("Unsupported operation");
diagnostic.addContent(details);
Element message = new Element("message", DIAG_NAMESPACE);
message.setText("Unsupported operation / " + parameter);
diagnostic.addContent(message);
Document doc = new Document();
doc.setRootElement(searchRetrieveResponse);
Format format = Format.getPrettyFormat();
format.setEncoding("utf-8");
XMLOutputter xmlOut = new XMLOutputter(format);
xmlOut.output(doc, response.getOutputStream());
}
示例9: generate
import org.jdom2.output.Format; //导入方法依赖的package包/类
public void generate() throws Exception{
Format format = Format.getPrettyFormat();
format.setEncoding("UTF-8");
XMLOutputter outputter = new XMLOutputter(format);
int base = 0;
File f=new File(dirPath);
if(f.exists()==false)
{
f.mkdir();
}
String filenameBase =dirPath + "\\" + problemType + "_" + nbAgent + "_" + domainSize + "_";
for (String key : extraParameter.keySet()){
filenameBase += key + "_";
filenameBase += extraParameter.get(key) + "_";
}
while (true){
String fileName = filenameBase + base + ".xml";
if (!new File(fileName).exists())
break;
base++;
}
for (int i = 0; i < nbInstance; i++){
FileOutputStream stream = new FileOutputStream(filenameBase+ (base + i) + ".xml");
Element root = new Element("instance");
if (problemType.equals(PROBLEM_SCALE_FREE_NETWORK)) {
AbstractGraph graph = new ScaleFreeNetworkGenerator("instance" + i,nbAgent,domainSize,minCost,maxCost,(Integer)extraParameter.get("m1"),(Integer)extraParameter.get("m2"));
graph.generateConstraint();
root.addContent(graph.getPresentation());
root.addContent(graph.getAgents());
root.addContent(graph.getDomains());
root.addContent(graph.getVariables());
root.addContent(graph.getConstraints());
root.addContent(graph.getRelations());
root.addContent(graph.getGuiPresentation());
}
outputter.output(root,stream);
stream.close();
}
}
示例10: 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".
*
* LineEndings will be CR-LF. Except for comments!?
*/
public static void sloppyPrint( Document doc, Writer writer, String encoding ) throws IOException
{
Format format = Format.getPrettyFormat();
format.setExpandEmptyElements( false );
format.setOmitDeclaration( false );
format.setIndent( "\t" );
format.setLineSeparator( LineSeparator.CRNL );
if ( encoding != null ) format.setEncoding( encoding );
XMLOutputter outputter = new XMLOutputter( format, new SloppyXMLOutputProcessor() );
outputter.output( doc, writer );
}
示例11: writeXML
import org.jdom2.output.Format; //导入方法依赖的package包/类
private static void writeXML( ModsInfo modsInfo, OutputStreamWriter dst, String indent, int depth ) throws IOException {
Format xmlFormat = Format.getPrettyFormat();
xmlFormat.setEncoding( dst.getEncoding() );
XMLOutputter xmlOut = new XMLOutputter( xmlFormat );
writeIndent( dst, indent, depth++ ).append( "<modsinfo>\n" );
writeIndent( dst, indent, depth ); dst.append("<title>").append( xmlOut.escapeElementEntities( modsInfo.getTitle() ) ).append( "</title>\n" );
writeIndent( dst, indent, depth ); dst.append("<author>").append( xmlOut.escapeElementEntities( modsInfo.getAuthor() ) ).append( "</author>\n" );
writeIndent( dst, indent, depth ); dst.append("<threadUrl><![CDATA[ ").append( modsInfo.getThreadURL() ).append( " ]]></threadUrl>\n" );
writeIndent( dst, indent, depth++ ).append( "<versions>\n" );
for ( Map.Entry<String,String> entry : modsInfo.getVersionsMap().entrySet() ) {
String versionFileHash = entry.getKey();
String versionString = entry.getValue();
writeIndent( dst, indent, depth );
dst.append( "<version hash=\"" ).append( xmlOut.escapeAttributeEntities( versionFileHash ) ).append( "\">" );
dst.append( xmlOut.escapeElementEntities( versionString ) );
dst.append( "</version>" ).append( "\n" );
}
writeIndent( dst, indent, --depth ).append( "</versions>\n" );
writeIndent( dst, indent, depth ); dst.append("<threadHash>").append( modsInfo.getThreadHash() ).append( "</threadHash>\n" );
dst.append( "\n" );
writeIndent( dst, indent, depth ); dst.append( "<description>" ).append( "<![CDATA[" );
dst.append( modsInfo.getDescription() );
dst.append( "]]>\n" );
writeIndent( dst, indent, depth ); dst.append( "</description>\n" );
writeIndent( dst, indent, --depth ).append( "</modsinfo>\n" );
}
示例12: write
import org.jdom2.output.Format; //导入方法依赖的package包/类
public void write(ChannelIF channel) throws IOException {
if (writer == null) {
throw new RuntimeException("No writer has been initialized.");
}
// create XML outputter with indent: 2 spaces, print new lines.
Format format = Format.getPrettyFormat();
format.setEncoding(encoding);
XMLOutputter outputter = new XMLOutputter(format);
// ----
Element rootElem = new Element("rss");
rootElem.setAttribute("version", RSS_VERSION);
Element channelElem = new Element("channel");
channelElem.addContent(new Element("title").setText(channel.getTitle()));
channelElem.addContent(new Element("description")
.setText(channel.getDescription()));
if (channel.getSite() != null) {
channelElem.addContent(new Element("link")
.setText(channel.getSite().toString()));
}
if (channel.getLanguage() != null) {
channelElem.addContent(new Element("language")
.setText(channel.getLanguage()));
}
Collection items = channel.getItems();
Iterator it = items.iterator();
while (it.hasNext()) {
channelElem.addContent(getItemElement((ItemIF) it.next()));
}
// export channel image
if (channel.getImage() != null) {
Element imgElem = new Element("image");
imgElem.addContent(new Element("title")
.setText(channel.getImage().getTitle()));
imgElem.addContent(new Element("url")
.setText(channel.getImage().getLocation().toString()));
imgElem.addContent(new Element("link")
.setText(channel.getImage().getLink().toString()));
imgElem.addContent(new Element("height")
.setText("" + channel.getImage().getHeight()));
imgElem.addContent(new Element("width")
.setText("" + channel.getImage().getWidth()));
imgElem.addContent(new Element("description")
.setText(channel.getImage().getDescription()));
channelElem.addContent(imgElem);
}
// TODO: add exporting textinput field
// if (channel.getTextInput() != null) {
// channelElem.addContent(channel.getTextInput().getElement());
// }
if (channel.getCopyright() != null) {
channelElem.addContent(new Element("copyright")
.setText(channel.getCopyright()));
}
// we have all together for the channel definition
rootElem.addContent(channelElem);
// ---
DocType docType = new DocType("rss", PUBLIC_ID, SYSTEM_ID);
Document doc = new Document(rootElem, docType);
outputter.output(doc, writer);
}
示例13: outputString
import org.jdom2.output.Format; //导入方法依赖的package包/类
/**
* Creates a String with the XML representation for the given WireFeed.
* <p>
* If the feed encoding is not NULL, it will be used in the XML prolog encoding attribute. It is
* the responsibility of the developer to ensure that if the String is written to a character
* stream the stream charset is the same as the feed encoding property.
* <p>
* NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
* <p>
*
* @param feed Abstract feed to create XML representation from. The type of the WireFeed must
* match the type given to the FeedOuptut constructor.
* @param prettyPrint pretty-print XML (true) oder collapsed
* @return a String with the XML representation for the given WireFeed.
* @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed
* don't match.
* @throws FeedException thrown if the XML representation for the feed could not be created.
*
*/
public String outputString(final WireFeed feed, final boolean prettyPrint) throws IllegalArgumentException, FeedException {
final Document doc = outputJDom(feed);
final String encoding = feed.getEncoding();
Format format;
if (prettyPrint) {
format = Format.getPrettyFormat();
} else {
format = Format.getCompactFormat();
}
if (encoding != null) {
format.setEncoding(encoding);
}
final XMLOutputter outputter = new XMLOutputter(format);
return outputter.outputString(doc);
}
示例14: output
import org.jdom2.output.Format; //导入方法依赖的package包/类
/**
* Writes to an Writer the XML representation for the given WireFeed.
* <p>
* If the feed encoding is not NULL, it will be used in the XML prolog encoding attribute. It is
* the responsibility of the developer to ensure the Writer instance is using the same charset
* encoding.
* <p>
* NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
* <p>
*
* @param feed Abstract feed to create XML representation from. The type of the WireFeed must
* match the type given to the FeedOuptut constructor.
* @param writer Writer to write the XML representation for the given WireFeed.
* @param prettyPrint pretty-print XML (true) oder collapsed
* @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed
* don't match.
* @throws IOException thrown if there was some problem writing to the Writer.
* @throws FeedException thrown if the XML representation for the feed could not be created.
*
*/
public void output(final WireFeed feed, final Writer writer, final boolean prettyPrint) throws IllegalArgumentException, IOException, FeedException {
final Document doc = outputJDom(feed);
final String encoding = feed.getEncoding();
Format format;
if (prettyPrint) {
format = Format.getPrettyFormat();
} else {
format = Format.getCompactFormat();
}
if (encoding != null) {
format.setEncoding(encoding);
}
final XMLOutputter outputter = new XMLOutputter(format);
outputter.output(doc, writer);
}
示例15: outputString
import org.jdom2.output.Format; //导入方法依赖的package包/类
/**
* Creates a String with the XML representation for the given WireFeed.
* <p>
* If the feed encoding is not NULL, it will be used in the XML prolog encoding attribute. It is the responsibility
* of the developer to ensure that if the String is written to a character stream the stream charset is the same as
* the feed encoding property.
* <p>
* NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
* <p>
* @param feed Abstract feed to create XML representation from. The type of the WireFeed must match
* the type given to the FeedOuptut constructor.
* @param prettyPrint pretty-print XML (true) oder collapsed
* @return a String with the XML representation for the given WireFeed.
* @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed don't match.
* @throws FeedException thrown if the XML representation for the feed could not be created.
*
*/
public String outputString(WireFeed feed, boolean prettyPrint) throws IllegalArgumentException,FeedException {
Document doc = outputJDom(feed);
String encoding = feed.getEncoding();
Format format = prettyPrint ? Format.getPrettyFormat() : Format.getCompactFormat();
if (encoding!=null) {
format.setEncoding(encoding);
}
XMLOutputter outputter = new XMLOutputter(format);
return outputter.outputString(doc);
}