Skip to content

Commit

Permalink
Add formart jar
Browse files Browse the repository at this point in the history
  • Loading branch information
fingerart committed Aug 3, 2017
1 parent db0d732 commit 0f05c40
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
Binary file added libs/jtidy-r938.jar
Binary file not shown.
49 changes: 37 additions & 12 deletions src/io/chengguo/apidebugger/engine/utils/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Node;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
import org.w3c.tidy.Tidy;
import org.xml.sax.InputSource;

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
Expand All @@ -33,15 +38,35 @@ public static String formatJson(@NotNull String json) throws JSONException {
}

public static String formatXml(@NotNull String xml) throws TransformerException {
Source xmlInput = new StreamSource(new StringReader(xml));
StreamResult xmlOutput = new StreamResult(new StringWriter());
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", 4);
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);
return xmlOutput.getWriter().toString().replaceFirst(">", ">\n");
try {
final InputSource src = new InputSource(new StringReader(xml));
final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml"));

//May need this: System.setProperty(DOMImplementationRegistry.PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");

final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
final LSSerializer writer = impl.createLSSerializer();

writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified.
writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted.

return writer.writeToString(document);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static String formatHtml(String html) {
StringReader stringReader = new StringReader(html);
Tidy tidy = new Tidy();
tidy.setWraplen(0);
tidy.setTidyMark(false);
tidy.setSmartIndent(true);
StringWriter stringWriter = new StringWriter();
tidy.parse(stringReader, stringWriter);
return stringWriter.toString();
}

public static String getStackTraceString(Throwable tr) {
Expand Down
2 changes: 1 addition & 1 deletion src/io/chengguo/apidebugger/presenter/DebuggerSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ private String formatContent(String text, Header[] contentTypes) throws JSONExce
}
Header contentType = contentTypes[0];
if (contentType.getValue().contains("text/html")) {
return StringUtils.formatXml(text);
return StringUtils.formatHtml(text);
} else if (contentType.getValue().contains("application/xml")) {
return StringUtils.formatXml(text);
} else if (contentType.getValue().contains("application/json")) {
Expand Down
13 changes: 0 additions & 13 deletions src/io/chengguo/apidebugger/ui/custom/JBDebuggerTable.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.chengguo.apidebugger.ui.custom;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.editor.event.DocumentAdapter;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.event.DocumentListener;
Expand All @@ -11,21 +10,15 @@
import com.intellij.ui.table.TableView;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.ColumnInfo;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.ListTableModel;
import com.intellij.util.ui.UIUtil;
import io.chengguo.apidebugger.ui.action.CloseTabAction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -85,12 +78,6 @@ public void actionPerformed(ActionEvent actionEvent) {
return popupMenu;
}

@Override
public void paint(@NotNull Graphics g) {
super.paint(g);
UIUtil.fixOSXEditorBackground(this);
}

public Map<String, String> getKeyValue() {
List<ItemInfo> items = getListTableModel().getItems();
Map<String, String> result = new LinkedHashMap<>();
Expand Down

0 comments on commit 0f05c40

Please sign in to comment.