Ant task for generating XSD from POJOs - ant

Is there an ant task for generating an XSD from POJOs?

I'm presuming that you want to serialize a Java POJO object and then use XSD to validate the serialised XML data, or author new object instances which can instantiated via your de-serialization process.
Thing is Java XML serialization comes in two favours (Examples follow):
Xstream XML is dynamically generated from fixed object class description
XMLBeans Java classes dynamically generated from fixed XML Schema
Now perhaps you're using something else that combines both approaches?
What I'd recommend is create (or generate) an XSD based on the XML your object creates when serialized. Relatively speaking Java objects don't change that often and when they do a far great challenge is supporting multiple versions (Reading data encoded for the older version of your object). To address this challenge I'd recommend reading the following article for one possible solution:
http://java.dzone.com/articles/migrate-serialized-java

Related

Xtext: How to persist a DSL AST using XMI (or JSON)?

I'm currently trying to figure out how to "connect" an Xtext Language Server with an EMFCloud.ModelServer instance so each time a client (VS Code extension in my case) saves a custom DSL text file, the Language Server saves the AST as XMI (or JSON). So then later the model can be included in the Model Server workspace and other editors can react to changes on my model (XMI, JSON)
So far I've seen that the current Xtext LS version does nothing with the "textDocument/didSave" notifications:
#Override
public void didSave(DidSaveTextDocumentParams params) {
// nothing to do
}
I'd like to enhance my LS instance to provide logic to that method and persist the current AST to XMI/JSON.
So far I was able to see that there is a generator class which the doGenerate method is called when the save is triggered on client side. However, taking a look to the call hierarchy of that method, it seems that is called within a "code generation" process. The documentation found for this is always related to other language generation (i.e. Java or c++) and I'm not sure if this would be the right place also because it seems that the file URI is not accessible (as it is in the didSave method of the LS part)
As summary, is there a way to access the semantic model (AST) from the Language Server "didSave" operation?
Following the hints provided by Christian Dietrich, the Language Server uses a ProjectManager which is able to retrieve the XtextResource holding the semantic model for a specific model URI (i.e. the URI passed to the server from the editor). Basically:
XtextResource resource = (XtextResource) getWorkspaceManager().getProjectManager(uri).getResource(uri);
In order to get our model from the resource the following method is provided:
EList<EObject> modelObjects = resource.getContents();
At this point we can persist the semantic model via the EMF Model Server (i.e. by creating or modifying an existing model in the Model Server).

How to feed a Model with a DOM tree

The XML data to be used with the Model is available as an org.w3c.dom.Element, due to unmarshalling an XML wrapper containing up to 100 individual rdf:RDF nodes.
Currently I'm converting the DOM tree to a string, and an InputStream fed with the String can be passed to the Model read method.
Isn't there a more direct method for feeding a Model?
There is class DOM2Model.create(...).load(...) which might work - very old code.
The main RDF/XML parsing is SAX-based and streaming, not XML-tree based.

Do I have to deserialize the whole complex json file (with many nested arrays and objects) when I need only one object? Flutter, Dart

https://github.com/enter link description herePoojaB26/ParsingJSON-Flutter/blob/master/assets/page.json
That's a tiny JSON file and there is no reason for premature optimizations.
Just deserialize it and then access the items of the generated Dart data structures to get what you need. Everything else is probably equally expensive and won't buy you anything.

How to prohibit DTD entities

I'm using the XML Tree API and the XML Parser API in c++, and I would like to prohibit entities creation in my XML documents.
What is the best way to do that when using these API's?
I've only seen examples of how to prohibit DTD entities when using XML Reader, and none when using the XML Tree or Parser API.
Thanks!
When using the tree API, you can call xmlGetIntSubset and inspect the xmlDtd structure to check whether a document contains entity declarations. When using the SAX parser, you can register an entityDeclSAXFunc callback.

How to write object in file in java without using object serialization?

I want to write object to file. But as per my study I found that it is not possible without object serialization. Other way is to convert object to array of bytes and then to write to file. But for this also object serialization is required. Is there any other way to write object to file? Because I want to use the same code in android and blackberry also. Please help me, I want to solve this problem as early as possible. Thanks in advance.
If you want to write an object to a file, then you by definition want to serialize it - that's what serialization means. If you're just looking for a way to save data to a file that doesn't rely on device-specific storage mechanisms, then you will need to write some custom serialization code. For every class that you want to store in a file, you'll need to do the following:
Write a method that stores the current state of the object in some writable structure, such as a string or byte array
Write a method that converts the string or byte array back into an object (it will probably be easiest to have this method take a stream as a parameter, and have it create a new object based on the data in that stream)
For example, you could save objects as XML or JSON strings, or in more efficient means - the best way to store it mainly depends on what sort of data you are storing and what you need to do with it.
It is good practice when doing this to include some version number that defines what version of the class you are using, and include this first when serializing the object. That way, when you deserialize, you can check that version number and know how it was serialized. This makes it easy to change your serialization scheme later on while still maintaining backwards compatibility with older files.
If you are just looking for a general purpose storage mechanism that is device-independent, then you could also look into using a SQLite database - they are supported on most if not all modern mobile operating systems. This will be easier than hand-rolling your own serialization, and will also generally have better performance.

Resources