GVAttributeSchema.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at http://www.graphviz.org/
  9. *************************************************************************/
  10. #import "GVAttributeSchema.h"
  11. static NSXMLDocument *_attributes = nil;
  12. static NSTextFieldCell *_stringCell = nil;
  13. static NSComboBoxCell *_enumCell = nil;
  14. @implementation GVAttributeSchema
  15. + (void)initialize
  16. {
  17. _attributes = [[NSXMLDocument alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"attributes" ofType:@"xml"]] options:0 error:nil];
  18. NSFont* smallFont = [NSFont labelFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]];
  19. _stringCell = [[NSTextFieldCell alloc] init];
  20. [_stringCell setControlSize:NSSmallControlSize];
  21. [_stringCell setDrawsBackground:NO];
  22. [_stringCell setEditable:YES];
  23. [_stringCell setEnabled:YES];
  24. [_stringCell setFont:smallFont];
  25. _enumCell = [[NSComboBoxCell alloc] init];
  26. [_enumCell setBezeled:NO];
  27. [_enumCell setButtonBordered:NO];
  28. [_enumCell setCompletes:YES];
  29. [_enumCell setControlSize:NSSmallControlSize];
  30. [_enumCell setDrawsBackground:NO];
  31. [_enumCell setEditable:YES];
  32. [_enumCell setEnabled:YES];
  33. [_enumCell setFont:smallFont];
  34. [_enumCell setHasVerticalScroller:NO];
  35. }
  36. + (NSArray*)attributeSchemasWithComponent:(NSString *)component
  37. {
  38. NSMutableArray* attributeSchemas = [NSMutableArray array];
  39. for (NSXMLElement* element in [_attributes nodesForXPath:[NSString stringWithFormat:@"/xsd:schema/xsd:complexType[@name='%@']/xsd:attribute", component] error:nil])
  40. [attributeSchemas addObject:[[[GVAttributeSchema alloc] initWithXMLElement:element] autorelease]];
  41. return attributeSchemas;
  42. }
  43. - (id)initWithXMLElement:(NSXMLElement *)element
  44. {
  45. if (self = [super init])
  46. _element = [element retain];
  47. return self;
  48. }
  49. - (NSString*)name
  50. {
  51. return [[_element attributeForName:@"ref"] stringValue];
  52. }
  53. - (NSCell*)cell
  54. {
  55. NSCell *typeCell = _stringCell;
  56. /* determine which cell to return */
  57. NSString *type = [[[_attributes nodesForXPath:[NSString stringWithFormat:@"/xsd:schema/xsd:attribute[@name='%@']/@type[1]", [self name]] error:nil] lastObject] stringValue];
  58. if (type) {
  59. if (![type hasPrefix:@"xsd:"]) {
  60. NSXMLElement *simpleType = [[_attributes nodesForXPath:[NSString stringWithFormat:@"/xsd:schema/xsd:simpleType[@name='%@'][1]", type] error:nil] lastObject];
  61. NSArray *enumerations = [simpleType nodesForXPath:@"xsd:restriction/xsd:enumeration" error:nil];
  62. if ([enumerations count]) {
  63. [_enumCell removeAllItems];
  64. for (NSXMLElement *enumeration in enumerations)
  65. [_enumCell addItemWithObjectValue: [[enumeration attributeForName: @"value"] stringValue]];
  66. typeCell = _enumCell;
  67. }
  68. }
  69. }
  70. /* determine the default value */
  71. NSString* defaultValue = [[_element attributeForName:@"default"] stringValue];
  72. if ([typeCell respondsToSelector:@selector(setPlaceholderString:)])
  73. [typeCell performSelector:@selector(setPlaceholderString:) withObject:defaultValue];
  74. return typeCell;
  75. }
  76. - (NSString*)documentation
  77. {
  78. return [[[_attributes nodesForXPath:[NSString stringWithFormat:@"/xsd:schema/xsd:attribute[@name='%@']/xsd:annotation/xsd:documentation[1]", [self name]] error:nil] lastObject] XMLString];
  79. }
  80. - (void)dealloc
  81. {
  82. [_element release];
  83. [super dealloc];
  84. }
  85. @end