Producing a Windows Path from an XML URI - html

What is the proper way to convert an XML URI into a Windows file path?
As a starting point, it's possible to turn:
file:///C:/DirA/DirB/File.txt
into:
C:\DirA\DirB\File.txt
... by first dropping the file:/// substring (having used it to determine we're dealing with a local file) and then placing a backslash wherever a slash appears in the original string. That seems like a good start, but it's not enough. For instance, the URI might look like this:
file:///C:/DirA/DirB/With%20Spaces.txt
... which becomes:
C:\DirA\DirB\With Spaces.txt
... after replacing %20s with spaces. Even that, however, would not be enough, as it may likewise be necessary to deal with other such encodings. Furthermore, some of those characters will not be legal Windows filename charcters, so it's necessary to identify which of those encodings are valid in Windows filenames and flag an error if anything else is encountered.
Is there anything else I'm forgetting? Anybody care to expand on the above?

Use the Uri.LocalPath property.
string path = new Uri("file:///C:/folder/file.txt").LocalPath;
This is platform-senstive, so path is "C:\folder\file.txt" on my Windows machine.
Note that you can also go the other way (from a local file system path to a file URI) using the constructor:
var uri = new Uri(#"C:\folder\file.txt");

You should use PathCreateFromUrl() on Windows.
See also The Bizarre and Unhappy Story of File: URLs.

Related

What is the difference between a .JSON file and .JL file?

I have both JSON file and JL file on my computer but when I open them in Notepad their structure looks like the same. What is the difference between them? where shall I use each one?
Actually, the time that I was asking this question I didn't know that "the file type is no guarantee of what is inside it". in other words I thought that for every file name there is a separate manifesto and if a files name is ".something", there is a unique manifesto for it. But now I know that I can create a file, write anything that I want into it and name it ".peyman" and yes there is nothing special with it!
What was that file? the file was JSON Lines file format.
Where did I find it? in the Scrapy except writing scrapy crawl name -o file.json I saw that somebody wrote scrapy crawl name -o file.jl. I tried that and the file was 99% like JSON file so I wondered and asked this question here.
So:
What is the difference between a .JSON file and .JL file? Now I know that the better question is "What is the difference between a .JSON file and .JL file in the Scrapy?"
The JSON Line is like JSON but without the "[" and "]" at the
beginning and the end. it is used in the Scrapy because of this
There's quite a few things that a jl file extension could be referring to. If I remember correctly, it originally had something to do with the window manager Sawfish.
Sawfish was developed in Lisp, and the jl file was a Lisp source file for Sawfish. However, I'm guessing (because you said that inside was JSON-like sauce) that's not what you're asking about.
In that case, I do recall a few projects on GitHub... JSON lambda and Julia.
Both of those may be the reason why you're seeing JSON in a jl file. Without more information on where you got that file, or what it was part of, though, we won't be able to help you much.
That said, file extensions rarely matter in terms of Linux. In Windows, they're far more important, but in Linux you could literally append anything to a file as an "extension" (ie. thisfile.whatever) and you could still open it up in an editor. The same is true for most editors in Windows.
Likely, the packager of that file decided on jl for their own reasons, rather than following convention of using .json.
I guess JL extension is used for many purposes, but JL is also one of the few extensions used for JSON-lines (also known as NDJSON or JSONL).
This format can contain multiple JSON values, one JSON value (with "compact" formatting) per line and is useful for e.g. streaming or logging.

MediaWiki filepath Magic Word doesn't work for some files types

I'm trying to use the MediaWiki filepath magic word` so that I can create some template links that pass a specific MediaWiki file. Unfortunately with certain file types, filepath just returns nothing.
The file I'm trying to get the path for that's failing is a text file in this case. I have confirmed that I am using the correct filename as I can create a regular file link using [[File:Name.txt]], and {{filepath:Image.png}} works properly.
Example of what I'm trying to accomplish:
[http://server/processfile.php?path={{filepath:<filename>}} Process A File]
Is this a known issue? Is there an easy way that I can debug what's happening here?
After digging around a bunch more I was able to resolve the issue. It turns out that even though the MediaWiki would accept the file, it was being assigned a random mime type because it was a .yaml file.
After updating mime.types and mime.info in MediaWiki and adding the mime type (text/yaml) to my IIS configuration, I was able to get the downloads working and the file links showing up.
Full disclosure: I may have been using an incorrectly cased file name even though I said that I was using the correct file name. :P

Change Data Type of existing Tiff tag using LibTiff.Net

I'm using the latest version of LibTiff.Net to fix some tiffs to be included in X9.37 files. I have got all the correct tags down, however, there are a couple of them that are DataType 'short' when I need them to be 'long'. Is there a best way to change just the datatype? Or will I have to create a custom tag?
I think you will need to at least rewrite each directory in your TIFFs. But please make sure the library writes relevant tags with the data type you need (or change the source code of the library if it doesn't).
In case X9.37 files (I don't know what it is) require TIFF directory to be placed in certain places of the file, you might be forced to recreate the file completely (by copying tags and raw raster).

Ruby encoding question

I'm saving scraped data to a web app, and here's a sample param:
400\xB0F.
This is the 'degree' character from a website, but when I put that into my model I get the dreaded invalid byte sequence in UTF-8 error.
Since it's coming from the web I thought I might try some client side encoding, so javascript turns that into: 400%B0F. This can at least get saved by ActiveRecord with no issue, but Rails seems to be escaping it again on the way out so those entities aren't decoded by the browser, so my show method shows the entire encoded string.
Where should I be cleaning up my input data, and what methods might be the best to use for unpredictable input?
Thanks!
Years ago I had, and solved, this very same problem in builder. Take a look at the to_xs method: http://builder.rubyforge.org/classes/String.html#M000007
You can require builder, and use it directly (you might want to pass false to escaping or you will get entity escaped output). Either that, or simply steal and adapt the source.
Update: here is the original, standalone, library:
http://intertwingly.net/stories/2005/09/28/xchar.rb
Perhaps you can use a binary form (like for upload file) with enctype="multipart/form-data" in form tag. Like this, you can use this data as a binary data ?
It's depends perhaps of waht you do with this data.
URI.unescape was the trick, after I encoded it client-side

What is the correct term for referring to a path and filename?

I want to use the correct term to make my API as intuitive as possible, so when a parameter is expected to be a full path and filename (ex "C:\Users\Max\Documents\MyDocument.txt") what is the correct term?
filename - that would just be MyDocument.txt, right?
path - that would just be C:\Users\Max\Documents, right?
What should I use for the parameter name? Would "file" be the appropriate name?
I feel kind of dumb asking this, but I want to make sure it's right.
My suggestion would be "Absolute file path" for some path pointing to a file, where as i would use "Absolute directory path" for a path pointing to a directory
In case of relative paths the change should be obvious.
If nothing else, you can always make a section in your documentation where you describe the meaning of certain terms you use.
The correct term is "Fully Qualified Name" (sometimes "FQN").
The term you should use in your API is "QualifiedName" or "QualifiedFilename"
Wikipedia has an entry for "Fully qualified file name" and defines it as "a file on a computer whose exact name is completely specified such that it is unambiguous and cannot be mistaken for any other file on that system."
A term often overused for this is URI, though your example isn't really one of those. I think you'll be perfectly clear if you just use "filepath" or "pathname."
For example, Java's file object uses "pathname" as the parameter name for the constructor on their File object.
Usually path is the full C:\Users\Max\Documents\MyDocument.txt while the C:\Users\Max\Documents\ part is usually known as the base directory or just directory.
You'll see in a lot of example code people write: C:\path\to\the\document.txt
I'd go with fullPath like you said path would be C:\Users\Max\Documents but reading fullPath would suggest path + filename.
I don't think there is One True Answer, maybe some consensus, but that's all we can hope for. I usually try to follow the conventions of the library I'm using (e.g., Cocoa, Java, or PHP). If I've got nothing to go by, I'd say:
File: the abstract thing being referred to by a name: the file handle
Path: a string indicating the location of a file or directory, either absolute or relative: /Users/Max/Documents/FooBar, ../Sibling/Zardoz
Name: the name of the file or directory, without location: FooBar, Zardoz, Documents
One easier solution you may have considered already is telling the consumer of the API what is expected in the XML documentation, which will also appear in Visual Studio intellisense if your compile the assembly with documentation, and distribute the .xml file.
/// <summary>
/// Saves the provided file to disk.
/// </summary>
/// <param name="filePath">The full path, including the filename of the file.</param>
public void SaveFile(string filePath)
{
}