What is the best way to represent a byte array as an XSD schema? I have a byte input which I need to parse and to feed to Java objects generated by JAXB out of an XSD schema for future validation. Every piece of information in my input is defined by offset and the length. I would like to attach this information to my elements so I could then read it from generated classes and use for parsing. The only way to attach such additional information to an Element I can think of is representing every Element as a Complex Type and then adding attributes to the complex Element and restrictions – to the simple Element wrapped by this complex Element. The problem with this approach (in addition to the Schema looking messy) is that JAXB will create separate class for every Element (Complex Type). And I will have a lot of fields in my input. :) The second issue is that I will in fact attach attributes and restrictions (which I need for validation) to different elements (wrapped simple element and the complex wrapper). So I will need to find a way to figure out that they in fact represent the same piece of information. I hope all this make sense. :) Basically my question is: can you think of a more elegant way to attach position and length information to an XSD element that represents the corresponding piece of a byte array? Thanks!
What is the best way to represent a
byte array as an XSD schema? I have a
byte input which I need to parse and
to feed to Java objects generated by
JAXB out of an XSD schema for future
validation.
JAXB will convert a byte[] to/from elements and attributes of type xsd:base64Binary.
For information on starting from XML schema see:
http://bdoughan.blogspot.com/2011/05/schema-to-java-xmlmimetype.html
For information on starting from Java classes see:
http://bdoughan.blogspot.com/2011/03/jaxb-web-services-and-binary-data.html
Related
How do I get numeric values from the RepresentationModel?
Say, after traversing a document, I have a YamlScalarNode. It has a string Value, which I can, of course, try to convert to a number, but I'd expect YAML to detect the type and present it as int or double etc. (perhaps via descendants from YamlScalarNode, whose type I could detect).
Is there an official way to do it that I'm missing?
Note that I can't use Serialization: the document structure does not directly map to a class; it can be a recursive definition of arbitrary depth, and the end values are either scalar numbers or sequences of numbers (vectors).
Also, can YamlDotNet handle numerical keys in mappings? This means that keys 1 and 01 should be considered duplicates. I believe YAML specification requires that, but I'm not certain...
The YAML schemas specify how scalars are to be interpreted. Ideally, you would look at the tag of a scalar to establish its type according to the selected schema. However, YamlDotNet does not yet implement them. For now you will have to do that yourself.
Intro
When designing UML activity diagrams I often encounter a rather simple problem for which I have to draw a rather complicated solution. I'm looking for an UML conform shortcut or more simple solution for the following problem.
Problem
Lets assume we have a class Parent with associations to different Children:
And we have an analog Constalation with the class Result and three chidren Result Part A, etc.
Now I want to refine an activity, which accepts a Parent object as input and produces an Result as output:
In the desired refinement, I want to I want to access the children or create the result from the result parts.
Current Solution
If I want to access the children or create the result from the result parts, I always have to introduce extra activities for those rather simple tasks:
Question
Are there any shortcut or simplification here, to access, extract or merge the children of an object? The desired Solution should be legal standard UML.
Something as simple like this would be nice:
UML does not define complex object creation element. If you need to construct result object which is composition of child objects, you have to present action. You should define action with resulting pin of composed type and input pins of child object types for each one. Action can start execution only if all input pins contain expected object.
For separation of child object from composed object use transformation as it is described in Waog's answer.
In your current solution example remove join element before merge result Children action, and connect all object nodes to this action. Remove extract input Children action and use transformation.
Answer on how to split objects
I found an answer on how to split a composed object in Martin Fowlers UML distilled myself, after getting a hint from #xmojmr
The book states:
Source: UML Distilled: A Brief Guide to the Standard Object Modeling Language
By Martin Fowler - on Google Books
I still don't know, if it's allowed to omit thos transformation-notes and just draw the pins and transitions!?
Missing Answer on how to merge objects
I'm still missing the answer on how to merge objects to a composed objects without introducting a merge-activity.
I need to know if we can unmarshal the object to the order we want.
ex: we have three classes A, B and C. order of unmarshalling is also in the same order.
As my concepts are not that clear but as far as I know the unmarshalling happens in the order in which it was marshaled.
As there is a development constrains I cannot change the order of marshalling objects. so is there a way to change the order to unmarshal B's objects first before A's objects.
I am using JAXB api's.
Thanks,
Suvidh
JAXB (JSR-222) implementations do not require that the elements in the XML document strictly match the order defined in the XML schema. If you wish to enforce an order then you can set an instance of javax.xml.validation.Schema on the Unmarshaller.
I have a string attribute in a Core Data entity whose Max Length value is 40. I'd like to use this value in code and not have to re-type the value "40." Is this possible?
As #K.Steff says in the comments above, you are better off validating in your code and not setting a max length in your core data model. To add on to that comment, I would also advise you to look at using a custom NSManagedObject subclass for this entity type, and within that subclass overriding the validateValue:forKey:error: or implementing a key-specific validation method for this property.
The value of this approach is that you can do things like "coerce" the validation by truncating strings at validation time. From the NSManagedObject documentation:
This method is responsible for two things: coercing the value into an appropriate
type for the object, and validating it according to the object’s rules.
The default implementation provided by NSManagedObject consults the object’s entity
description to coerce the value and to check for basic errors, such as a null value when
that isn’t allowed and the length of strings when a field width is specified for the
attribute. It then searches for a method of the form validate< Key >:error: and invokes it
if it exists.
You can implement methods of the form validate< Key >:error: to perform validation that is
not possible using the constraints available in the property description. If it finds an
unacceptable value, your validation method should return NO and in error an NSError object
that describes the problem. For more details, see “Model Object Validation”. For
inter-property validation (to check for combinations of values that are invalid), see
validateForUpdate: and related methods.
So you can implement this method to both validate that the string is not too long and, if necessary, truncate it when it is too long.
From NSManagedObject you can access the NSEntityDescription via entity. In there you can grab the array properties and a dictionary propertiesByName, either of which will get you to NSPropertyDescriptions. Each property description has a property, validationPredicates that will return an array of NSPredicates. One of those will be the condition that your string length must be at most 40.
Sadly predicates are a lot of hassle to reverse engineer — and doing so can even be impossible, given that you can create one by supplying a block. Hopefully though you'll just have an NSComparisonPredicate or be able to get to one by tree walking downward from an NSCompoundPredicate or an NSExpression.
From the comparison predicate you'll be able to spot from the left and right expressions that one is string length and the other is a constant value.
So, in summary:
Core Data exposes validation criteria only via the very general means of predicates;
you can usually, but not always, rebuild an expression (in the natural language sense rather than the NSExpression sense) from a predicate; and
if you know specifically you're just looking for a length comparison somewhere then you can simplify that further into a tree walk for comparison predicates that involve the length.
It's definitely not going to be pretty because of the mismatch of the specific and the general but it is possible.
I have a large XElement property, and want to know the byte size of it for logging purposes. I don't want to just ToString() it because I have concerns about potentially big strings (not) getting GC'd.
What is a smart/compact way to calculate the XML content of an XElement (.NET 4.0).
Thanks.
In C#, there is no easy way to know an object's size. You can recursively compute it knowing the size of the primitive types and using reflection, but it's not an easy job.
I assume you don't really care about the XElement size, but rather about the serialized XML content (they are quite different sizes naturally). I think to get that you need to serialize it (i.e. call ToString()) - there is no way around it.