i'm doing a java project about digital signature on microsoft office files (docx, xlsx, pptx).
i can create an object which will be added in the xml signature using:
XMLSignatureFactory xml_fac = XMLSignatureFactory.newInstance("DOM");
XMLObject xml_object = XMLSignatureFactory.newXMLObject(...
ArrayList<XMLObject> obj_list = new ArrayList<XMLObject>();
obj_list.add(xml_object);
XMLSignature xml_sig = xml_fac.newXMLSignature(...,...,obj_list,...
however, this xml signature generated by java doesn't
contain xmlns for the type XMLObject
for microsoft office to recognise this xml object:
<Object>
<SignatureProperties>
<SignatureProperty Id="idSignatureTime" Target="#idPackageSignature">
<mdssi:SignatureTime>
<mdssi:Format>YYYY-MM-DDThh:mm:ssTZD</mdssi:Format>
<mdssi:Value>2011-04-02T10:10:10Z</mdssi:Value>
</mdssi:SignatureTime>
</SignatureProperty>
</SignatureProperties>
</Object>
i have to add:
xmlns:mdssi="http://schemas.openxmlformats.org/package/2006/digital-signature"
into the opening <Object> tag
but XMLObject in java has no way to add this, it has only Id, MimeType, Encoding, and Content
any suggestions?
SignatureProperty property = XML_SIGNATURE_FACTORY.newSignatureProperty(
...);
SignatureProperties properties = XML_SIGNATURE_FACTORY.newSignatureProperties(
Collections.singletonList(property), ...);
XMLObject object = XML_SIGNATURE_FACTORY.newXMLObject(
Collections.singletonList(properties), ...);
Version 2:
Here comes story. I uses Java. (JSR-105)
I'm digging XML Digital Signature for W3C Widgets.
for Signature/Object
-- It seems easy first time I saw the APIs. I just created XMLObject like above.
But, as spec says, I have to insert a Reference referring the XMLObject created by 1.
-- What? How can I make a reference referring an output node?
-- How can I return a Data for Nodes not even created yet?
I realized that I should prepare a org.w3c.dom.Document containing the <Object> element (and its children as purposed) and use it for the Reference.
--> With your URIDereferencer, you can return a NodeSetData from this document's getDocumentElement() result.
So here comes the solution.
Make a org.w3c.dom.Document instance containing the <Object> element as its root element. You can add any attributes and child elements as you wish.
And you can generate an XML Signature as detached or enveloping scheme.
Check this out. This is where I found the solution. And I think your problem will be solved.
http://today.java.net/pub/a/today/2006/11/21/xml-signature-with-jsr-105.html#generate-an-enveloping-signature
Sorry for my poor English. I'm Korean. :)
My best regards.
Jin Kwon
Related
I have the following problem. Into a Java application I have to create a new XML content using XPath (I always used it to parse XML files and obtain values inside its tag, can I use it also for build a new XML content?).
So my final result (that have to be saved on a database CLOB field, not on an .xml file, but I think that this is not important) have to be something like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Messaggio>
<Intestazione>
<Da>06655971007</Da>
<A>01392380547</A>
<id>69934</id>
<idEnel/>
<DataInvio>2015-05-06</DataInvio>
<DataRicezione/>
<InRisposta/>
<TipoDoc>Ricevuta</TipoDoc>
</Intestazione>
<Documenti>
<Ricevuta>
<Testata>
<Documento>
<Tipo>380</Tipo>
<NumeroDocumento>ff</NumeroDocumento>
<Stato>KO</Stato>
<Data>2014-03-10</Data>
</Documento>
</Testata>
<Dettaglio>
<Messaggio>
<Codice>000</Codice>
<Descrizione>Documento NON Conforme / NON dovuto</Descrizione>
</Messaggio>
</Dettaglio>
</Ricevuta>
</Documenti>
</Messaggio>
So what I need to do is to programmatically add the nodes and the content of these nodes (the content is obtained from a model object).
Can I do it using XPath? How?
Tnx
XPath is an API to locate nodes in a XML document. It can't create new nodes or manipulate existing nodes. So what you need is to locate the nodes to modify using XPath and then use the API of the found nodes to make the changes.
But in your case, you're starting with an empty document. Have a look at frameworks like JDOM 2 to build XML documents from scratch. This tutorial should get you started: http://www.studytrails.com/java/xml/jdom2/java-xml-jdom2-example-usage.jsp
You can't. XPath is a matching technology, not a content creation technology. Possibly you are looking for XSLT?
Good Evening everyone!
I'm trying to parse a HTML-page in Java with JDOM2, to access some information from it.
My code looks like this: (Just added the packages for this codeblock, don't have them in my real source)
//Here goes the reading of the site into my String "string" (using NekoHTML)
org.xml.sax.InputSource is = new InputSource();
is.setCharacterStream(new StringReader(string));
org.cyberneko.html.parsers.DOMParser parser = new DOMParser();
parser.parse(is);
org.jdom2.input.DOMBuilder builder = new DOMBuilder();
org.jdom2.Document doc = builder.build(parser.getDocument());
This works fine for everything except some special case: When the site contains quotation-Marks within an element. Here is an example of what I mean:
Der "realismo mágico" und die Phantastische...
So, after that wonderful Tag I get the following error-trace:
SEVERE: org.jdom2.IllegalNameException: The name "literatur"" is not legal for JDOM/XML attributes: XML name 'literatur"' cannot contain the character """.
So, now my question is: What are my options to take care of this error? Is there maybe a feature in NekoHTML I can use for this (using the "setFeature()"), or something within JDOM I could use?
If no: Are there other libaries that are suitable for scraping websites that can take such a thing as the quotation mark within the tag?
Thanks for your time!
Okay, so I solved the problem like following:
Since there wasn't any dependency on NekoHTML I switched to jTidy as parser which does the job in this case.
Question answered.
Is this possible? For example, if I run the jSoup Code:
Document source = Jsoup.connect("http://www.xmlFile.com/file.xml").get();
// Convert the Jsoup object to a w3c Document object with Apache.
return DOMBuilder.jsoup2DOM(source);
I now have a w3c Document object. Is there any way of getting file.xml from that object? I've not found anything online that informs me eitherway.
There's a method Document called getDocumentUri() that might do what you want. It seems to depend on what way the Document was created whether the Uri is set to something or null. I don't know how JSoup creates the documents, so your mileage may vary.
http://docs.oracle.com/javase/6/docs/api/org/w3c/dom/Document.html#getDocumentURI()
If JSoup doesn't set it, you could call setDocumentUri() manually in your method, and then use getDocumentUri() where you needed it?
I have a XML document.I am signing a part of document using xmlsignature. Before finding digest, I want to apply XSLT transform. According to what I read, XSLT converts an XML document to another format(can be XML also). Now I am confused that, where will be the transformed new document is avilable?How to retrieve the value from this newly created document if I want to show it to user?
My XML Document
<r1>
<user>asd</user>
<person>ghi</person>
</r1>
Code for Transformation
Transform t=fac.newTransform(Transform.XPATH,new XPathFilterParameterSpec("/r1/user"));
According to xpath transformation,Whenever value of user element changes the xmlsignature should not be validated. And if person element's value changes then Signature should be validated. But when I change person element's value the signature is not validated. WHY?
The xslt transform used when signing a document relates to how nodes in your source XML are selected when the signature is calculated.
This question/answer by Dave relates to signing parts of an XML document using xpath2. The link to Sean Mullans' post in this answer suggests xpath2 is more appropriate for signing parts of a document because the evaluation of an xpath expression is done per node.
So based on the sun dsig example you can replace the Reference creation using:
List<XPathType> xpaths = new ArrayList<XPathType>();
xpaths.add(new XPathType("//r1/user", XPathType.Filter.INTERSECT));
Reference ref = fac.newReference
("", fac.newDigestMethod(DigestMethod.SHA1, null),
Collections.singletonList
(fac.newTransform(Transform.XPATH2,
new XPathFilter2ParameterSpec(xpaths))),
null, null);
This allows //r1/user to be protected with a signature while the rest of the document can be altered.
The problem with the xpath/xpath2 selection is that a signature can be generated for /some/node/that/does/not/exist. You are right to modify a test document and make sure the signature is working the way you expect.
You might test the document in a test program by generating a signature then tampering with the xml node before verification:
NodeList nlt = doc.getElementsByTagName("user");
nlt.item(0).getFirstChild().setTextContent("Something else");
A more reliable alternative to an xpath selector might be to put an ID on the xml document elements you hope to sign like:
<r1>
<user id="sign1">asd</user>
<person>ghi</person>
</r1>
then reference this ID as the URI in the first parameter of an enveloped transfer:
Reference ref = fac.newReference
("#sign1", fac.newDigestMethod(DigestMethod.SHA1, null),
Collections.singletonList
(fac.newTransform(Transform.ENVELOPED,(TransformParameterSpec) null)),
null, null);
For the output, a signature operation adds a new Signature element to the DOM you have loaded in memory. You can stream the output by transforming it like this:
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.transform(new DOMSource(doc), new StreamResult(System.out));
The XSLT specification doesn't define what happens to the result document; that's defined by the API specifications of your chosen XSLT processor. For example, if you invoke XSLT from Java using the JAXP interface, you can ask for the result as a DOM tree in memory or for it to be serialized to a specified file on disk.
You have tagged your question "Java" which is the only clue you give to your processing environment. My guess is you want to transform to a DOM and then use DOM interfaces to get the value from the new document. Though if you're using XSLT 2.0 and Saxon, the s9api interface is much more usable than the native JAXP interface.
The xslt part defines only the transformation definition, nothing else.
Have a look at this:
java xslt tutorial
in Francois Gravel answer the input.xml file is the file that will be transformed, the transform.xslt is the xslt definition which describes how to transform the xml file. output.out are the results, this may be xml, but it can also be html, flat file...
This is where I started with when i was using xslt:
http://www.w3schools.com/xsl/default.asp
Have a look at this also:
http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog
I'm working on a project under which i have to take a raw file from the server and convert it into XML file.
Is there any tool available in java which can help me to accomplish this task like JAXP can be used to parse the XML document ?
I guess you will need your objects for later use ,so create MyObject that will be some bean that you will load the values form your Raw File and you can write this to someFile.xml
FileOutputStream os = new FileOutputStream("someFile.xml");
XMLEncoder encoder = new XMLEncoder(os);
MyObject p = new MyObject();
p.setFirstName("Mite");
encoder.writeObject(p);
encoder.close();
Or you con go with TransformerFactory if you don't need the objects for latter use.
Yes. This assumes that the text in the raw file is already XML.
You start with the DocumentBuilderFactory to get a DocumentBuilder, and then you can use its parse() method to turn an input stream into a Document, which is an internal XML representation.
If the raw file contains something other than XML, you'll want to scan it somehow (your own code here) and use the stuff you find to build up from an empty Document.
I then usually use a Transformer from a TransformerFactory to convert the Document into XML text in a file, but there may be a simpler way.
JAXP can also be used to create a new, empty document:
Document dom = DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.newDocument();
Then you can use that Document to create elements, and append them as needed:
Element root = dom.createElement("root");
dom.appendChild(root);
But, as Jørn noted in a comment to your question, it all depends on what you want to do with this "raw" file: how should it be turned into XML. And only you know that.
I think if you try to load it in an XmlDocument this will be fine