How to collect the list of differnces of file between local changes and Head changes in a Map using JGIT Library? - version-control

I need a list of file differences between a local (worktree) and the Head
eg
public void localDiffAndHead(Git git) throws IOException {
try (DiffFormatter diffFormatter = new DiffFormatter(DisabledOutputStream.INSTANCE)) {
diffFormatter.setRepository(git.getRepository());
String lastCommit = getLastCommitOfCurrentBranch(git).get().getName();
AbstractTreeIterator commitTreeIterator = prepareTreeParser( git.getRepository(), lastCommit );
FileTreeIterator workTreeIterator = new FileTreeIterator( git.getRepository() );
List<DiffEntry> diffEntries = diffFormatter.scan(workTreeIterator, commitTreeIterator);
for(DiffEntry diffEntry: diffEntries) {
if (diffEntry.getOldPath().contains("/ui.xml")) {
FileHeader fileHeader = diffFormatter.toFileHeader(diffEntry);
ListIterator<Edit> edits = fileHeader.toEditList().listIterator();
while(edits.hasNext()) {
Edit edit = edits.next();
System.out.println(edit.getType());
System.out.println(edit.getBeginA());
System.out.println(edit.getEndA());
System.out.println(edit.getBeginB());
System.out.println(edit.getEndB());
}
}
}
System.out.println();
}
}
My above code presenting the below output for my selected file "ui.xml".
Replace
0
1
0
1
but my expected result should be like below
Type | content
Replace |xyz
Delete | abc
Insert | bbc

Related

How to append CSV file with PostgreSQL query export in Datagrip?

I want to append an existing CSV file (Template for Google Ads Offline Conversion Tracking) with the results of a query from PostgreSQL using Datagrip.
Execute to file works just fine to export the query results as a CSV file. But obviously that just creates a new file with only the results of the query.
I was thinking to load the existing template in the groovy script that's there in datagrip, and then add its content to the top of the created file, but I can't make any headway on how to do this.
I found (maybe?) how to load the file, but I have no clue how to actually use this and put it at the top of the file so the results of the query can be added below it.
fh = new File("C:/1.csv")
def csv_content = fh.getText('utf-8')
That's all I got for loading the file with no clue on how to use this going forward.
Alternatively I considered to just manually add the info of the template by hand to the script, so it doesn't have to load another file, but just takes that info and then adds the query results afterwards. If you could give me a way on how to directly add rows like these would be very helpful too.
The groovy script file that is included in Datagrip is as follows:
/*
* Available context bindings:
* COLUMNS List<DataColumn>
* ROWS Iterable<DataRow>
* OUT { append() }
* FORMATTER { format(row, col); formatValue(Object, col); getTypeName(Object, col); isStringLiteral(Object, col); }
* TRANSPOSED Boolean
* plus ALL_COLUMNS, TABLE, DIALECT
*
* where:
* DataRow { rowNumber(); first(); last(); data(): List<Object>; value(column): Object }
* DataColumn { columnNumber(), name() }
*/
SEPARATOR = ","
QUOTE = "\""
NEWLINE = System.getProperty("line.separator")
def printRow = { values, valueToString ->
values.eachWithIndex { value, idx ->
def str = valueToString(value)
def q = str.contains(SEPARATOR) || str.contains(QUOTE) || str.contains(NEWLINE)
OUT.append(q ? QUOTE : "")
.append(str.replace(QUOTE, QUOTE + QUOTE))
.append(q ? QUOTE : "")
.append(idx != values.size() - 1 ? SEPARATOR : NEWLINE)
}
}
if (!TRANSPOSED) {
ROWS.each { row -> printRow(COLUMNS, { FORMATTER.format(row, it) }) }
}
else {
def values = COLUMNS.collect { new ArrayList<String>() }
ROWS.each { row -> COLUMNS.eachWithIndex { col, i -> values[i].add(FORMATTER.format(row, col)) } }
values.each { printRow(it, { it }) }
}
You need to pass content of your file to OUT variable like this:
fh = new File("C:/1.csv")
def csv_content = fh.getText('utf-8')
OUT.append(csv_content).append("\n")
Everything that is passed to OUT.append will be written to new file.
Just add it to the beginning of the script:
/*
* Available context bindings:
* COLUMNS List<DataColumn>
* ROWS Iterable<DataRow>
* OUT { append() }
* FORMATTER { format(row, col); formatValue(Object, col); getTypeName(Object, col); isStringLiteral(Object, col); }
* TRANSPOSED Boolean
* plus ALL_COLUMNS, TABLE, DIALECT
*
* where:
* DataRow { rowNumber(); first(); last(); data(): List<Object>; value(column): Object }
* DataColumn { columnNumber(), name() }
*/
SEPARATOR = ","
QUOTE = "\""
NEWLINE = System.getProperty("line.separator")
fh = new File("C:/1.csv")
def csv_content = fh.getText('utf-8')
OUT.append(csv_content).append("\n")
def printRow = { values, valueToString ->
values.eachWithIndex { value, idx ->
def str = valueToString(value)
def q = str.contains(SEPARATOR) || str.contains(QUOTE) || str.contains(NEWLINE)
OUT.append(q ? QUOTE : "")
.append(str.replace(QUOTE, QUOTE + QUOTE))
.append(q ? QUOTE : "")
.append(idx != values.size() - 1 ? SEPARATOR : NEWLINE)
}
}
if (!TRANSPOSED) {
ROWS.each { row -> printRow(COLUMNS, { FORMATTER.format(row, it) }) }
}
else {
def values = COLUMNS.collect { new ArrayList<String>() }
ROWS.each { row -> COLUMNS.eachWithIndex { col, i -> values[i].add(FORMATTER.format(row, col)) } }
values.each { printRow(it, { it }) }
}
Note that you may copy paste CSV-Groovy.csv.groovy to the same directory (e.g. MY-CSV.csv.groovy) and modify new file. This new extractor will be added to combobox with all extractors

Take all text files in a folder and combine then into 1

I'm trying to merge all my text files into one file.
The problem I am having is that the file names are based on data previously captured in my app. I don't know how to define my path to where the text files are, maybe. I keep getting a error, but the path to the files are correct.
What am I missing?
string filesread = System.AppDomain.CurrentDomain.BaseDirectory + #"\data\Customers\" + CustComboB.SelectedItem + #"\";
Directory.GetFiles(filesread);
using (var output = File.Create("allfiles.txt"))
{
foreach (var file in new[] { filesread })
{
using (var input = File.OpenRead(file))
{
input.CopyTo(output);
}
}
}
System.Diagnostics.Process.Start("allfiles.txt");
my error:
System.IO.DirectoryNotFoundException
HResult=0x80070003
Message=Could not find a part of the path 'C:\Users\simeo\source\repos\UpMarker\UpMarker\bin\Debug\data\Customers\13Dec2018\'.
I cant post a pic, but let me try and give some more details on my form.
I select a combobox item, this item is a directory. then I have a listbox that displays the files in my directory. I then have a button that executes my desires of combining the files. thanks
I finally got it working.
string path = #"data\Customers\" + CustComboB.SelectedItem;
string topath = #"data\Customers\";
string files = "*.txt";
string[] txtFiles;
txtFiles = Directory.GetFiles(path, files);
using (StreamWriter writer = new StreamWriter(topath + #"\allfiles.txt"))
{
for (int i = 0; i < txtFiles.Length; i++)
{
using (StreamReader reader = File.OpenText(txtFiles[i]))
{
writer.Write(reader.ReadToEnd());
}
}
System.Diagnostics.Process.Start(topath + #"\allfiles.txt");
}

Why ASTRewrite works only when the source files are open in the editor

I'm trying to build a plugin which parses all the files of a project and makes some changes to it, I'm able to make the changes, but my problem is ASTRewrite makes changes only to the files opened in the editor. Is there a way to make the changes to all the files even if they are not opened in the editor?
Any help is highly appreciated, thanks in advance!
Here is the sample code which I'm using:
//create a ASTRewrite
AST ast = cu.getAST();
ASTRewrite rewriter = ASTRewrite.create( ast );
//for getting insertion position
TypeDeclaration typeDecl = (TypeDeclaration) cu.types().get( 0 );
MethodDeclaration methodDecl = typeDecl.getMethods()[ 0 ];
Block block = methodDecl.getBody();
block.accept( new ASTVisitor(){
#Override
public boolean visit( InfixExpression node ){
//filter only those compound statements which start with logger
if( node.getParent().toString().startsWith( varLogger ) ){
loggerStmtList.add( node );
//checking if left operand is present in the logger infix statement
if(node.getLeftOperand() != null) {
//adding '[' to the leftOperand before "
Expression leftOperand = node.getLeftOperand();
StringBuilder leftOp = new StringBuilder(leftOperand.toString());
char[] c = {' ', '['};
leftOp.setCharAt(leftOp.toString().length() - 2, '[');
//leftOp.insert(leftOp.toString().length()-1, c);
//adding '[' the left operand source code
InfixExpression placeHolder2 = (InfixExpression) rewriter.createStringPlaceholder( leftOp.toString() , ASTNode.INFIX_EXPRESSION );
rewriter.replace( node.getLeftOperand(), placeHolder2, null );
}
//checking if right operand(extended operand) is present in the logger infix statement
if(! node.extendedOperands().isEmpty()){
//adding ']' to the rightOperand after "
List<String> rightOperand = new ArrayList<String>();
rightOperand.add(node.extendedOperands().get(0).toString());
StringBuilder rightOp = new StringBuilder(rightOperand.get(0).toString());
rightOp.setCharAt( 1, ']');
//adding ']' in the Extended operands(right side) source code
ListRewrite listRewrite = rewriter.getListRewrite(node, InfixExpression.EXTENDED_OPERANDS_PROPERTY);
InfixExpression placeHolder = (InfixExpression) rewriter.createStringPlaceholder( rightOp.toString() , ASTNode.INFIX_EXPRESSION );
listRewrite.replace( (ASTNode) node.extendedOperands().get( 0 ) , placeHolder , null );
rightOperand.clear();
}
TextEdit edits;
try{
edits = rewriter.rewriteAST();
// apply the text edits to the compilation unit
Document document = new Document(unit.getSource());
edits.apply(document);
// this is the code for adding statements
unit.getBuffer().setContents(document.get());
System.out.println("rewriting done");
}
catch( JavaModelException | IllegalArgumentException | MalformedTreeException | BadLocationException e ){
e.printStackTrace();
}
}
return false;
}
} );

Eclipse Plugin: Jump to a Line by adding Hyperlink to a row in SWT Table view

The table looks like:
+-----------------+-----------------------+
|File Path | Line Number |
|-----------------|-----------------------|
| File1.java | 10 |
|-----------------|-----------------------|
| File2.java | 19 |
+-----------------+-----------------------+
On double-clicking any row in the table, I want to jump to the the specified line number in the file of its row of the table.
I am using the following function to go to a specified line of an Active Editor.
private static void goToLine(IEditorPart editorPart, int LineNumber)
{
if (!(editorPart instanceof ITextEditor) || LineNumber <= 0)
{
return;
}
ITextEditor editor = (ITextEditor) editorPart;
IDocument document = editor.getDocumentProvider().getDocument(
editor.getEditorInput());
if (document != null)
{
IRegion lineInfo = null;
// line count internally starts with 0, and not with 1 like in GUI
try
{
lineInfo = document.getLineInformation(LineNumber - 1);
}
catch (org.eclipse.jface.text.BadLocationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if (lineInfo != null)
{
editor.selectAndReveal(lineInfo.getOffset(), lineInfo.getLength());
}
}
}
}
How to add hyperlinks to the rows of the table to jump to the line of the corresponding file?
Adding a hyperlink to a table requires using OwnerDrawLabelProvider to draw the table yourself so that you can add the Hyperlink controls.
If you just want to support double click on a row you can use TableViewer.addDoubleClickListener (or TableViewer.addOpenListener) to add an IDoubleClickListener (or IOpenListener).

' ', hexadecimal value 0x1F, is an invalid character. Line 1, position 1

I am trying to read a xml file from the web and parse it out using XDocument. It normally works fine but sometimes it gives me this error for day:
**' ', hexadecimal value 0x1F, is an invalid character. Line 1, position 1**
I have tried some solutions from Google but they aren't working for VS 2010 Express Windows Phone 7.
There is a solution which replace the 0x1F character to string.empty but my code return a stream which doesn't have replace method.
s = s.Replace(Convert.ToString((byte)0x1F), string.Empty);
Here is my code:
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
using (var reader = new StreamReader(e.Result))
{
int[] counter = { 1 };
string s = reader.ReadToEnd();
Stream str = e.Result;
// s = s.Replace(Convert.ToString((byte)0x1F), string.Empty);
// byte[] str = Convert.FromBase64String(s);
// Stream memStream = new MemoryStream(str);
str.Position = 0;
XDocument xdoc = XDocument.Load(str);
var data = from query in xdoc.Descendants("user")
select new mobion
{
index = counter[0]++,
avlink = (string)query.Element("user_info").Element("avlink"),
nickname = (string)query.Element("user_info").Element("nickname"),
track = (string)query.Element("track"),
artist = (string)query.Element("artist"),
};
listBox.ItemsSource = data;
}
}
XML file:
http://music.mobion.vn/api/v1/music/userstop?devid=
0x1f is a Windows control character. It is not valid XML. Your best bet is to replace it.
Instead of using reader.ReadToEnd() (which by the way - for a large file - can use up a lot of memory.. though you can definitely use it) why not try something like:
string input;
while ((input = sr.ReadLine()) != null)
{
string = string + input.Replace((char)(0x1F), ' ');
}
you can re-convert into a stream if you'd like, to then use as you please.
byte[] byteArray = Encoding.ASCII.GetBytes( input );
MemoryStream stream = new MemoryStream( byteArray );
Or else you could keep doing readToEnd() and then clean that string of illegal characters, and convert back to a stream.
Here's a good resource for cleaning illegal characters in your xml - chances are, youll have others as well...
https://seattlesoftware.wordpress.com/tag/hexadecimal-value-0x-is-an-invalid-character/
What could be happening is that the content is compressed in which case you need to decompress it.
With HttpHandler you can do this the following way:
var client = new HttpClient(new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip
| DecompressionMethods.Deflate
});
With the "old" WebClient you have to derive your own class to achieve the similar effect:
class MyWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest;
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
return request;
}
}
Above taken from here
To use the two you would do something like this:
HttpClient
using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }))
{
using (var stream = client.GetStreamAsync(url))
{
using (var sr = new StreamReader(stream.Result))
{
using (var reader = XmlReader.Create(sr))
{
var feed = System.ServiceModel.Syndication.SyndicationFeed.Load(reader);
foreach (var item in feed.Items)
{
Console.WriteLine(item.Title.Text);
}
}
}
}
}
WebClient
using (var stream = new MyWebClient().OpenRead("http://myrss.url"))
{
using (var sr = new StreamReader(stream))
{
using (var reader = XmlReader.Create(sr))
{
var feed = System.ServiceModel.Syndication.SyndicationFeed.Load(reader);
foreach (var item in feed.Items)
{
Console.WriteLine(item.Title.Text);
}
}
}
}
This way you also recieve the benefit of not having to .ReadToEnd() since you are working with the stream instead.
Consider using System.Web.HttpUtility.HtmlDecode if you're decoding content read from the web.
If you are having issues replacing the character
For me there were some issues if you try to replace using the string instead of the char. I suggest trying some testing values using both to see what they turn up. Also how you reference it has some effect.
var a = x.IndexOf('\u001f'); // 513
var b = x.IndexOf(Convert.ToString((byte)0x1F)); // -1
x = x.Replace(Convert.ToChar((byte)0x1F), ' '); // Works
x = x.Replace(Convert.ToString((byte)0x1F), " "); // Fails
I blagged this
I had the same issue and found that the problem was a  embedded in the xml.
The solution was:
s = s.Replace("", " ")
I'd guess it's probably an encoding issue but without seeing the XML I can't say for sure.
In terms of your plan to simply replace the character but not being able to, because you have a stream rather than a text, simply read the stream into a string and then remove the characters you don't want.
Works for me.........
string.Replace(Chr(31), "")
I used XmlSerializer to parse XML and faced the same exception.
The problem is that the XML string contains HTML codes of invalid characters
This method removes all invalid HTML codes from string (based on this thread - https://forums.asp.net/t/1483793.aspx?Need+a+method+that+removes+illegal+XML+characters+from+a+String):
public static string RemoveInvalidXmlSubstrs(string xmlStr)
{
string pattern = "&#((\\d+)|(x\\S+));";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
if (regex.IsMatch(xmlStr))
{
xmlStr = regex.Replace(xmlStr, new MatchEvaluator(m =>
{
string s = m.Value;
string unicodeNumStr = s.Substring(2, s.Length - 3);
int unicodeNum = unicodeNumStr.StartsWith("x") ?
Convert.ToInt32(unicodeNumStr.Substring(1), 16)
: Convert.ToInt32(unicodeNumStr);
//according to https://www.w3.org/TR/xml/#charsets
if ((unicodeNum == 0x9 || unicodeNum == 0xA || unicodeNum == 0xD) ||
((unicodeNum >= 0x20) && (unicodeNum <= 0xD7FF)) ||
((unicodeNum >= 0xE000) && (unicodeNum <= 0xFFFD)) ||
((unicodeNum >= 0x10000) && (unicodeNum <= 0x10FFFF)))
{
return s;
}
else
{
return String.Empty;
}
})
);
}
return xmlStr;
}
Nobody can answer if you don't show relevant info - I mean the Xml content.
As a general advice I would put a breakpoint after ReadToEnd() call. Now you can do a couple of things:
Reveal Xml content to this forum.
Test it using VS Xml visualizer.
Copy-paste the string into a txt file and investigate it offline.