Giter VIP home page Giter VIP logo

xsd2cocoa's Introduction

XSDConverter

Parses XSD files and generates Swift (or Objective-C) classes for IOS (or OSX) -- uses libxml only

For every Complex Type in your schema file, a corresponding Swift (or Objective-C) class is generated with its attributes and elements as Swift (or Objective-C) properties and an init method taking an libxml2 TextReader is generated.

The code is generated according to a template that you can easily customize if you need to generate specific/exotic code. For most folks the standard template included should work just fine.

The generator is a framework that is completely seperate from the GUI. And thus it can be embedded in any cocoa app.

The generator is checked with unit tests that read specific xsds, generate code for it, compile it using clang and then see if they can parse an according xml
(so IF you find bugs / missing features - please provide a xsd & a xml file so I can fix it / add it to the generator)

What works already:

(the key points I remember, a changelog is attached)
  • element default type
  • objc & swift 2.0 support (generates FORMATTED code) ** 1.5 **
  • handles namespacesd xsd ** 1.5 **
  • Complex type elements
  • Simple type elements/attributes (standard and custom)
    • 42/44 types defined by the w3c work
      outstanding: base64Binary, hexBinary
  • Inheritance by extension
  • restrictions with enumeration support (thanks to Alex Smith for the initial trigger) ** 1.4 **
  • Static parsing methods for global elements (a category is generated for a complex type that is used as the root element)
  • Mapping xml namespaces to class name prefixes via a specific tag in a template. (without it, namespaces are mapped 1 : 1 to Class prefixes)
  • referencing external files to copy to the destination folder when generating code ** 1.2 **
  • nested sequences & choices
  • includes and imports of other XSD files ** 1.3 **
  • annotations of elements that are converted to comments ** 1.3 **
  • anonymous 'inner' types (complex and simple)

the generated parser only requires libxml and I have tested it on IOS as well as OSX

  • (Remember: to use the classes, link your target against libxml2 (!))

The generator itself uses the NSXML* tree based API and is for OSX only.

Biggest pain points

  1. So far the generator does NOT handle references to elements/attributes via the ref= attribute.
  2. The min & maxOccurances of elements inside a sequence/choice must be specified on element itself as opposed to the sequence itself

The Project is still in alpha phase, BUT real world usage is already be possible. and practiced
A demo project is included and I used it to generate XML Parsers for two commercial projects already (Ill merge back fixes as I find the time).

how-to (based on 1.0)

(very brief ;))
  1. download the sourcecode and use XCode 7 to build the xsd2cocoa mac app. (At this point, Im not providing a binary)

  2. Upon starting the app you see a window 1

  3. here you specify:

    • the XSD to process
    • the output folder for the generated .m/.h files
    • the template to use (built-in vs. custom path) 2
  4. After specifying the in- and output paths you hit 'write code' and you get ready to use .h/.m files. (Remember: to use the classes, link against libxml2 (!)) 3

credits

The basic code was on google projects. The original xsd2cocoa was written by Stefan Winter in 2011 and even if not really usable, it was already quite awesome

I made numerous fixes and improvements to the generator.

  • additions to properly deal with lots of more XSD features (too many to list here)
  • new features to the template to make things more customizable
  • a lot of refactoring. Modernizing, getting rid of code... I would say I removed 1/2 the code ;)
  • addded a UI for the app as well as a Demo project to make it easier to use
  • modernized and completed the templates used to generate the code

The code uses the MGTemplateEngine by Matt Gemmel

changelog

1.6

  • fixed a memory leak that parsing a URL
  • made the reader not break if there are comments in global level
  • fixed the xsd types gDay, gMonth, gMonthDay, ...
  • fixed unit tests
  • improved date parsing
  • fixed some swift issues where parsers crashed
  • setting enUSPOSIXLocale for all formatters so XML types are parsed ok

xsd2cocoa's People

Contributors

allyjweir avatar daij-djan avatar smittysmee avatar valeriyvan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

xsd2cocoa's Issues

NSNumberFormatter and german local

NSNumberFormatter* decFormatter = [[NSNumberFormatter alloc] init]; 
decFormatter.decimalSeparator = @".";
decFormatter.numberStyle = NSNumberFormatterDecimalStyle;

The initializiation of the NSNumberFormatter is missing the groupingSeperator.
This leads to the same grouping and decimalSeperator for german (DE) local. (Both are ".")

This leads to strange behavior when interpreting numbers.
@"1.00" will be interpreted as 1
@"1.000" will be interpreted as 1000

Schema/template distinction doesn't work if first xsd element is comment

If XSD document happens to have comment as first element, like in snippet below, used approach of distinction of schema/template isn't working.

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
	Bundeseinheitlicher Medikationsplan (BMP) (bmp023.xsd)
	2016-07-15 Version 2.3

	verantwortliche Organisationen KBV, DAV und BÄK

   basierend auf Medikationsplan Ultrakurzformat (UKF)
  
   In Zusammenarbeit mit:    
   Bundesverband Deutscher Apothekensoftwarehäuser (ADAS e.V.)
   Bundesverband Gesundheits-IT e.V. (bvitg e.V.)
   HL7 Deutschland e.V.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:element name="MP">
		<xs:annotation>
			<xs:documentation>Bundesmedikationsplan</xs:documentation>
		</xs:annotation>
		<xs:complexType>

Tests fail & App not working on El Capitan / Xcode 7

Hello,

I was pretty glad to find a framework helping me in working with XSDs. Unfortunately, I can build the App and run, but the generation fails. I then started the tests, and many of them fail.
I noticed that several error message are talking of "namespace", so it may be that this relates to the other open issue here.
As I'm not using ObjC, but Swift, I'm pretty helpless in fixing anything myself... ;-)
I attached the test results for your information.

Best regards

Immediately fails

Any plans to update this to current OS and Xcode? It builds OK but when I run it immediately fails.

2017-07-14 19:17:25.444821-0700 XSDConverter[4863:2210945] [General] -[__NSCFConstantString characterAtIndex:]: Range or index out of bounds
2017-07-14 19:17:25.448390-0700 XSDConverter[4863:2210945] [General] (
0 CoreFoundation 0x00007fffa0f8457b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fffb61c81da objc_exception_throw + 48
2 CoreFoundation 0x00007fffa1001c55 +[NSException raise:format:] + 197
3 CoreFoundation 0x00007fffa0efd396 -[__NSCFString characterAtIndex:] + 102
4 XSDConverterCore 0x00000001000e4d9f -[XSDschema setTargetNamespacePrefixOverride:] + 991
5 XSDConverterCore 0x00000001000e2153 -[XSDschema initWithNode:targetNamespacePrefix:error:] + 531
6 XSDConverterCore 0x00000001000e3966 -[XSDschema initWithUrl:targetNamespacePrefix:error:] + 726
7 XSDConverter 0x0000000100002e3d -[AppDelegate writeCode:] + 1133
8 libsystem_trace.dylib 0x00007fffb6cdb3a7 _os_activity_initiate_impl + 53
9 AppKit 0x00007fff9f172721 -[NSApplication(NSResponder) sendAction:to:from:] + 456
10 AppKit 0x00007fff9ec56cc4 -[NSControl sendAction:to:] + 86
11 AppKit 0x00007fff9ec56bec __26-[NSCell _sendActionFrom:]_block_invoke + 136
12 libsystem_trace.dylib 0x00007fffb6cdb3a7 _os_activity_initiate_impl + 53
13 AppKit 0x00007fff9ec56b44 -[NSCell _sendActionFrom:] + 128
14 AppKit 0x00007fff9ec99539 -[NSButtonCell _sendActionFrom:] + 98
15 libsystem_trace.dylib 0x00007fffb6cdb3a7 _os_activity_initiate_impl + 53
16 AppKit 0x00007fff9ec55426 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2481
17 AppKit 0x00007fff9ec99272 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 798
18 AppKit 0x00007fff9ec53ddb -[NSControl mouseDown:] + 832
19 AppKit 0x00007fff9f2ee24f -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 6341
20 AppKit 0x00007fff9f2eaa6c -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 1942
21 AppKit 0x00007fff9f2e9f0a -[NSWindow(NSEventRouting) sendEvent:] + 541
22 AppKit 0x00007fff9f16e681 -[NSApplication(NSEvent) sendEvent:] + 1145
23 AppKit 0x00007fff9e9e9427 -[NSApplication run] + 1002
24 AppKit 0x00007fff9e9b3e0e NSApplicationMain + 1237
25 XSDConverter 0x0000000100001552 main + 34
26 libdyld.dylib 0x00007fffb6aa9235 start + 1
27 ??? 0x0000000000000003 0x0 + 3
)

"date" type kills the processing

A line like this:
<xs:attribute name="VALUE" type="date">/xs:attribute

Would cause an assert failure:

Assertion failed: (retType), function -[XSDschema typeForName:], file /Users/cba-user/Downloads/xsd2cocoa-master/framework/XSDschema.m, line 340.

Adding full XSD sample here: http://pastebin.com/jkdAPuC9

Namespaced schemas don't generate any output

An XSD with a header like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="GSP">
xs:complexType
...

Would generate empty output.

Reason: XSDSchema.m, ln 78 wouldn't pick up any complex types because of the incorrect XPath selector:
NSArray* ctNodes = [node nodesForXPath: @"/schema/complexType" error: error];

Adding full XSD sample here: http://pastebin.com/jkdAPuC9

Retain namespacing on xsd imports

Any easy way to retain the namespacing of imported schema?

For instance I have a shared common entity schema (CMENT namespace) containing Address. When I convert the common entity schema I get what I would expect.
CMENTAddress.h
CMENTAddress.m

When I convert a schema (HOMESVC namespace) that imports the common entity schema I get:
HOMESVCAddress.h
HOMESVCAddress.m
HOMESVCHome.h
HOMESVCHome.m

I would expect:
CMENTAddress.h
CMENTAddress.m
HOMESVCHome.h
HOMESVCHome.m

Empty entry in XML confuses the generated parser (tamplate bug)

Hi,

I discoverer following bug in generated parser:
I modified "tests/xmls/MyPantry.xsd" to add "Comment" entry, and after that following change in "tests/xmls/MyPantry.xml" breaks generated parser for ObjC:

<?xml version="1.0" encoding="UTF-8"?>
<p:Pantry
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.example.org/Pantry"
    xsi:schemaLocation="http://www.example.org/Pantry Pantry.xsd">
    <p:Shelf>
        <p:Food>
            <p:Name>Apple</p:Name>
            <p:FoodGroup>fruit</p:FoodGroup>
            <p:Comment></p:Comment><!-- here parser is confused -->
            <p:Cost>$0.50</p:Cost>
        </p:Food>
    </p:Shelf>
    <p:Shelf>
        <p:Food>
            <p:Name>Chocolate</p:Name>
            <p:FoodGroup>sweetFatOil</p:FoodGroup>
            <p:Cost>$0.50</p:Cost>
        </p:Food>
    </p:Shelf>
</p:Pantry>

Modification done to this xsd and xml are available at my branch: https://github.com/MarekR22/xsd2cocoa/tree/test/emptyEntryBug

I will try to fix it my self, but this template file is not very easy to understand.

Add ability to work with "any" element type

For example:

<!-- TransformExpressionType -->
<xs:complexType name="TransformExpressionType">
	<xs:sequence>
		<xs:any processContents="lax" namespace="##other" minOccurs="0" maxOccurs="1"/>
	</xs:sequence>
</xs:complexType>

Unable create Element with reference.

Add ability to work with references in element.

Example:

<xs:import schemaLocation="TableViewLayout.xsd" namespace="http://schemas.digdes.com/DDM/ExecutiveWorkplace/2010/TableViewLayout"/>

<!-- LayoutType -->
<xs:element name="Layout" type="LayoutType" />
<xs:complexType name="LayoutType">
	<xs:sequence>
		<xs:element ref="layout:TableViewLayout" minOccurs="0" maxOccurs="1"/>
	</xs:sequence>
	<xs:attribute name="Id" type="ms:guid" use="required"/>
</xs:complexType>
<!-- TableViewLayout root element -->
<xs:element name="TableViewLayout" type="TableViewLayoutType"/>

<!-- TableViewLayoutType -->
<xs:complexType name="TableViewLayoutType">
	<xs:sequence>
		<xs:element name="Column" type="TableColumnType" minOccurs="0" maxOccurs="unbounded"/>
	</xs:sequence>
	<xs:attribute name="Version" type="xs:int" use="optional"/>
</xs:complexType>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.