below is the current codes i have.
what it does is basically loop thru project solution project file and detect if it is a C# file. however it can't detect files that are put in a folder , how can i modify it to read a C# file in a solution folder.
Regards , Andy
foreach (var projectItem in
_applicationObject.Solution.Projects.Cast<Project>().SelectMany(project => project.ProjectItems.Cast<ProjectItem>()))
{
//for (var i = 0; i < projectItem.FileCount; i++)
//{
if (projectItem.FileCount > 0 && projectItem.Name.EndsWith(".cs")) // check if project is .Cs files
{
string fileName;
try
{
fileName = projectItem.FileNames[0];
}
catch (Exception)
{
continue;
}
//end of find filename
}
}
This will print all items in the solution, I believe.
It works with C++ solution in VS 2012.
// XXX Test
IEnumerator enumerator = m_applicationObject.Solution.GetEnumerator();
string indent = " ";
while (enumerator.MoveNext())
{
Project p = enumerator.Current as Project;
if (p != null)
{
Debug.WriteLine(p.Name);
ProcessProjectItems(p.ProjectItems, indent);
}
}
// XXX Test
void ProcessProjectItems(ProjectItems pis, string indent)
{
if (pis == null)
return;
IEnumerator items = pis.GetEnumerator();
while (items.MoveNext())
{
ProjectItem pi = items.Current as ProjectItem;
if (pi != null)
{
Debug.WriteLine(indent + pi.Name);
if (pi.ProjectItems != null)
{
ProcessProjectItems(pi.ProjectItems, indent + " ");
}
else
{
Project p = pi.Object as Project;
if (p != null && p.ProjectItems != null)
ProcessProjectItems(p.ProjectItems, indent + " ");
}
}
}
}
Related
I am writing a program to count words in a file. I am facing problems while parsing .tex files.
This code need to go on a website where it has to count words from the file that is being uploaded. I have managed to do it but I am looing for some better solutions
case "application/x-tex": // Avoid words with '\' and count
Scanner sc1;
try {
sc1 = new Scanner(new URL(URLPath).openStream());
while (sc1.hasNext()) {
String str = sc1.next();
if (!str.contains("\\")) {
System.out.print(str + " ");
wordCount++;
}
}
sc1.close();
} catch (IOException e) {
System.out.println("There was a problem while reading File on the URL");
break;
// e.printStackTrace();
}
if (wordCount <= 0) {
System.out.println("Total count is " + wordCount
+ ". The uploaded File is either empty or it consists of Images only");
} else {
System.out.println("");
System.out.println("**********");
System.out.println("Word Count: " + wordCount);
System.out.println("**********");
System.out.println("");
}
break;
I am expecting a String output which I could further use to count words.
// Trigger perl script
URL website = new URL(URLPath);
Path path = Paths.get("myfile.tex");
bufferFiles.add(new File("myFile.tex"));
try (InputStream in = website.openStream()) {
Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);
}
URL texcount = new URL("https://papertrue.s3.us-west-1.amazonaws.com/draft/77e3c992-b70f-4711-8b9b-eaf390617bb8");
Path path1 = Paths.get("texcount.pl");
bufferFiles.add(new File("texcount.pl"));
try (InputStream in = texcount.openStream()) {
Files.copy(in, path1, StandardCopyOption.REPLACE_EXISTING);
}
wordCount = 0;
Process process;
try {
process = Runtime.getRuntime().exec("/etc/papertrue/texcount.pl myfile.tex");
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
String tem[] = line.split(":\\s");
log.debug(tem[tem.length - 1]);
try {
wordCount += Integer.parseInt(tem[tem.length - 1]);
} catch (Exception e) {
}
}
process.waitFor();
if (process.exitValue() == 0) {
log.debug("Command Successful");
} else {
log.debug("Command Failure");
}
log.debug(wordCount);
} catch (IOException e) {
log.debug("There was a problem while reading File on the URL");
e.printStackTrace();
break;
}
I am using following code for getting contents of a web page
String url = "http://abc.com/qrticket.asp?qrcode="
+ "2554";
try {
url += ";deviceside=true;interface=wifi;ConnectionTimeout=" + 50000;
HttpConnection connection = (HttpConnection) Connector.open(url,
Connector.READ_WRITE);
connection.setRequestMethod(HttpConnection.GET);
// connection.openDataOutputStream();
InputStream is = connection.openDataInputStream();
String res = "";
int chr;
while ((chr = is.read()) != -1) {
res += (char) chr;
}
is.close();
connection.close();
showDialog(parseData(res));
} catch (IOException ex) {
ex.printStackTrace();
showDialog("http: " + ex.getMessage());
} catch (Exception ex) {
ex.printStackTrace();
showDialog("unknown: " + ex.getMessage());
}
public void showDialog(final String text) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.alert(text);
}
});
}
public String parseData(String str) {
String[] data = split(str, "//");
StringBuffer builder = new StringBuffer();
for (int i = 0; i < data.length; i++) {
System.out.println("data:" + data[i]);
String[] vals = split(data[i], ">>");
if (vals.length > 1) {
System.out.println(vals[0]);
builder.append(vals[0].trim()).append(": ")
.append(vals[1].trim()).append("\n");
} else {
builder.delete(0, builder.toString().length()).append(
vals[0].trim());
break;
}
}
return builder.toString();
}
public String[] split(String splitStr, String delimiter) {
// some input validation
if (delimiter == null || delimiter.length() == 0) {
return new String[] { splitStr };
} else if (splitStr == null) {
return new String[0];
}
StringBuffer token = new StringBuffer();
Vector tokens = new Vector();
int delimLength = delimiter.length();
int index = 0;
for (int i = 0; i < splitStr.length();) {
String temp = "";
if (splitStr.length() > index + delimLength) {
temp = splitStr.substring(index, index + delimLength);
} else {
temp = splitStr.substring(index);
}
if (temp.equals(delimiter)) {
index += delimLength;
i += delimLength;
if (token.length() > 0) {
tokens.addElement(token.toString());
}
token.setLength(0);
continue;
} else {
token.append(splitStr.charAt(i));
}
i++;
index++;
}
// don't forget the "tail"...
if (token.length() > 0) {
tokens.addElement(token.toString());
}
// convert the vector into an array
String[] splitArray = new String[tokens.size()];
for (int i = 0; i > splitArray.length; i++) {
splitArray[i] = (String) tokens.elementAt(i);
}
return splitArray;
}
This is working absolutely fine in simulator but giving 'http:null' (IOException) on device, I dont know why??
How to solve this problem?
Thanks in advance
I think the problem might be the extra connection suffixes you're trying to add to your URL.
http://abc.com/qrticket.asp?qrcode=2554;deviceside=true;interface=wifi;ConnectionTimeout=50000
According to this BlackBerry document, the ConnectionTimeout parameter isn't available for Wifi connections.
Also, I think that if you're using Wifi, your suffix should simply be ";interface=wifi".
Take a look at this blog post on making connections on BlackBerry Java, pre OS 5.0. If you only have to support OS 5.0+, I would recommend using the ConnectionFactory class.
So, I would try this with the url:
http://abc.com/qrticket.asp?qrcode=2554;interface=wifi
Note: it's not clear to me whether your extra connection parameters are just ignored, or are actually a problem. But, since you did get an IOException on that line, I would try removing them.
The problem was that no activation of blackberry internet service. After subscription problem is solved.
Thanks alto all of you especially #Nate
I am useing kxml parser for my j2me application. I am reading the file from phone memory and parsing the xml file to display the data(have various level of filter). On each filter i need to read the data from this file. For first time i created the parser and every time i re-assign this parser1(reference-original) to the paerser2(used to parse data). For first time i got the correct answer, but second time i haven't got the file content it shows null as data.
Here is my code:
FileConnection fc = (FileConnection)Connector.open(rmsObj.rmsData.elementAt(0).toString());
InputStream in = fc.openInputStream();
InputStreamReader is = new InputStreamReader(in);
commonAppObj.externParser = new XmlParser(is);
commonAppObj class file.
protected void fileread() {
try {
if(externParser != null){
parser = externParser;
fileparser(parser);
}else{
InputStream in = this.getClass().getResourceAsStream(this.dataBase);
InputStreamReader is = new InputStreamReader(in);
parser = new XmlParser(is);
fileparser(parser);
}
} catch (IOException ioe) {
} finally {
parser = null;
}
}
private void fileparser(XmlParser parser){
try {
ParseEvent event = null;
dataflag = 0;
dataflagS = 0;
System.out.println("findtags = " + findtags);
while (((event = parser.read()).getType() != Xml.END_DOCUMENT) && (dataflag != 1)) {
if (event.getType() == Xml.START_TAG) {
String name = event.getName();
if (name != null && name.equals(findtags)) {
dataflag = 0;
parseAddressTag(parser);
}
name = null;
}
event = null;
}
} catch (IOException ioe) {
} finally {
parser = null;
}
}
}
If your InputStream returns true in a call to markSupported you may reset it at the end of fileparser method, but first you need to call mark right after creating it.
if (in.markSupported()) {
in.mark(in.available());
}
I'm trying to combine all scripts into one.. I have two folders, the main folder 'scripts' and the other 'scripts/other'.
When I try:
BundleTable.Bundles.Add(new ScriptBundle("~/scripts/all").Include("~/Scripts/*.js", "~/Scripts/other/*.js"));
scripts from 'scripts/other' are not included.
but when I invert the order:
BundleTable.Bundles.Add(new ScriptBundle("~/scripts/all").Include("~/Scripts/other/*.js", "~/Scripts/*.js"));
it works!!
Someone can tell me why?
Can you try calling the IncludeDirectory methods directly and seeing if you see the same issue?
ScriptBundle("~/scripts/all").IncludeDirectory("~/Scripts", "*.js").IncludeDirectory("~/Scripts/other", "*.js"));
If this works, then it's possible we have a bug here.
I don't know what is happening, but this is the code inside the System.Web.Optimization.Bundle:
// System.Web.Optimization.Bundle
public Bundle Include(params string[] virtualPaths)
{
for (int i = 0; i < virtualPaths.Length; i++)
{
string text = virtualPaths[i];
Exception ex = Bundle.ValidateVirtualPath(text, "virtualPaths");
if (ex != null)
{
throw ex;
}
if (text.Contains('*'))
{
int num = text.LastIndexOf('/');
string text2 = text.Substring(0, num);
if (text2.Contains('*'))
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, OptimizationResources.InvalidPattern, new object[]
{
text
}), "virtualPaths");
}
string text3 = "";
if (num < text.Length - 1)
{
text3 = text.Substring(num + 1);
}
PatternType patternType = PatternHelper.GetPatternType(text3);
ex = PatternHelper.ValidatePattern(patternType, text3, "virtualPaths");
if (ex != null)
{
throw ex;
}
this.IncludeDirectory(text2, text3);
}
else
{
this.IncludeFile(text);
}
}
return this;
}
I've been trying to make a function that goes through a directory and lists all the files in the directory and any sub-directories:
void get_listing (string dir) {
try {
var directory = File.new_for_path (dir);
var enumerator = directory.enumerate_children (FILE_ATTRIBUTE_STANDARD_NAME, 0);
FileInfo file_info;
while ((file_info = enumerator.next_file ()) != null) {
stdout.printf(file_info.get_file_type().to_string());
if (file_info.get_file_type() == FileType.DIRECTORY) {
get_listing(file_info.get_name());
} else {
stdout.printf ("%s\n", file_info.get_name ());
}
}
} catch (Error e) {
stderr.printf ("Error: %s\n", e.message);
return;
}
}
int main (string[] args) {
get_listing(".");
return 0;
}
When I run this code none of the files in any sub-directories are outputted. All the files/directories types are "G_FILE_TYPE_UNKNOWN". Does anyone know how to fix this or another method I could use.
You are enumerating files by name only; if you wish to access file type later, you should pass appropriate hint to enumerator:
FILE_ATTRIBUTE_STANDARD_NAME + "," + FILE_ATTRIBUTE_STANDARD_TYPE