1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
/**
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tuscany.samples.sdo.basic;
import org.apache.tuscany.samples.sdo.SampleBase;
import commonj.sdo.DataObject;
import commonj.sdo.Property;
import commonj.sdo.Sequence;
import commonj.sdo.helper.HelperContext;
/**
* Demonstrates accessing the sequence from a DataObject containing mixed content.
* <p>
* <h3>Running this Sample</h3> See <A HREF="../../../../../../index.html"
* target="_top">the main overview</A> for instructions on how to run this
* sample.
*/
public class AccessingTheContentsOfASequence extends SampleBase {
HelperContext scope;
public AccessingTheContentsOfASequence(Integer userLevel) {
super(userLevel, SAMPLE_LEVEL_BASIC);
}
/**
* previously created XSD file used
*/
public static final String LETTER_XSD = "letter.xsd";
/**
* previously created XML file used
*/
public static final String LETTER_XML = "letter.xml";
/**
* Execute this method in order to run the sample.
*
* @param args
*/
public static void main(String[] args) {
AccessingTheContentsOfASequence sample =
new AccessingTheContentsOfASequence(COMMENTARY_FOR_NOVICE);
sample.run();
}
/*
* metadata for the sample documenting the areas of SDO that are explored
*/
public static int [] CORE_FUNCTION = {
SDOFacets.ACCESSING_VALUES_IN_A_SEQUENCE
};
public void runSample () throws Exception {
commentary("Demonstrates accessing the sequence from a DataObject containing mixed content.");
scope = createScopeForTypes();
loadTypesFromXMLSchemaFile(scope, LETTER_XSD);
DataObject letter = getDataObjectFromFile(scope, LETTER_XML);
// print letter sequence
commentary("We've loaded a document from an XML file that contains mixed content.\n" +
"Here's how the XML looks ...\n");
System.out.println(scope.getXMLHelper().save(letter, "letter.xsd", "letter"));
commentary("We can iterate over the sequence, getting the Property / Value pairs\n" +
"using the Sequence.getProperty(int) and Sequence.getValue(int) methods.\n" +
"The model for this document is \"mixed\", i.e.\n" +
"letter.getType().isMixed() returns \"true\".\n" +
"Let's take a look at the Properties in this sequence.");
Sequence letterSequence = letter.getSequence();
for (int i = 0; i < letterSequence.size(); i++) {
Property prop = letterSequence.getProperty(i);
if (prop == null) {
String text = (String) letterSequence.getValue(i);
System.out.println("Unstructured text (" + text + ")");
} else {
System.out.println("Property: " + prop.getName() + " Value : " + letterSequence.getValue(i));
}
}
commentary("The values of the modeled Properties are still accessible through the DataObject\n" +
"getter and setter methods, but only through the Sequence API can we get to the unstructured\n" +
"text and see the ordering of the instance document");
}
}
|