1414import org .w3c .dom .Element ;
1515
1616class CategoricalMatrixImpl extends
17- MatrixImpl <CharacterState > implements CategoricalMatrix {
18-
17+ MatrixImpl <CharacterState > implements CategoricalMatrix {
18+
1919 private Set <CharacterStateSet > mCharacterStateSets = new HashSet <CharacterStateSet >();
2020 private MolecularCharacterStateSetImpl mMolecularCharacterStates = null ;
21-
22- /**
23- * Protected constructors that take a DOM document object but not
24- * an element object are used for generating new element nodes in
25- * a NeXML document. On calling such constructors, a new element
26- * is created, which can be retrieved using getElement(). After this
27- * step, the Impl class that called this constructor would still
28- * need to attach the element in the proper location (typically
29- * as a child element of the class that called the constructor).
30- * @param document a DOM document object
31- * @author rvosa
32- */
21+
22+ /**
23+ * Protected constructors that take a DOM document object but not
24+ * an element object are used for generating new element nodes in
25+ * a NeXML document. On calling such constructors, a new element
26+ * is created, which can be retrieved using getElement(). After this
27+ * step, the Impl class that called this constructor would still
28+ * need to attach the element in the proper location (typically
29+ * as a child element of the class that called the constructor).
30+ * @param document a DOM document object
31+ * @author rvosa
32+ */
3333 protected CategoricalMatrixImpl (Document document ) {
3434 super (document ,"Standard" );
3535 }
36-
37- /**
38- * Protected constructors are intended for recursive parsing, i.e.
39- * starting from the root element (which maps onto DocumentImpl) we
40- * traverse the element tree such that for every child element that maps
41- * onto an Impl class the containing class calls that child's protected
42- * constructor, passes in the element of the child. From there the
43- * child takes over, populates itself and calls the protected
44- * constructors of its children. These should probably be protected
45- * because there is all sorts of opportunity for outsiders to call
46- * these in the wrong context, passing in the wrong elements etc.
47- * @param document the containing DOM document object. Every Impl
48- * class needs a reference to this so that it can create DOM element
49- * objects
50- * @param element the equivalent NeXML element (e.g. for OTUsImpl, it's
51- * the <otus/> element)
52- * @author rvosa
53- */
36+
37+ /**
38+ * Protected constructors are intended for recursive parsing, i.e.
39+ * starting from the root element (which maps onto DocumentImpl) we
40+ * traverse the element tree such that for every child element that maps
41+ * onto an Impl class the containing class calls that child's protected
42+ * constructor, passes in the element of the child. From there the
43+ * child takes over, populates itself and calls the protected
44+ * constructors of its children. These should probably be protected
45+ * because there is all sorts of opportunity for outsiders to call
46+ * these in the wrong context, passing in the wrong elements etc.
47+ * @param document the containing DOM document object. Every Impl
48+ * class needs a reference to this so that it can create DOM element
49+ * objects
50+ * @param element the equivalent NeXML element (e.g. for OTUsImpl, it's
51+ * the <otus/> element)
52+ * @author rvosa
53+ */
5454 protected CategoricalMatrixImpl (Document document , Element element , OTUsImpl otus ) {
5555 super (document , element );
5656 for ( Element stateSetElement : getChildrenByTagName ( getFormatElement (), CharacterStateSetImpl .getTagNameClass () ) ) {
5757 createCharacterStateSet (stateSetElement );
5858 }
59-
59+
6060 for ( Element characterElement : getChildrenByTagName ( getFormatElement (), CharacterImpl .getTagNameClass () ) ) {
6161 createCharacter (characterElement );
6262 }
63-
63+
6464 for ( Element row : getChildrenByTagName ( getMatrixElement (), "row" ) ) {
6565 OTU otu = otus .getThingById (row .getAttribute ("otu" ));
6666 MatrixRowImpl <CharacterState > matrixRow = new MatrixRowImpl <CharacterState >(getDocument (),row , this , false );
@@ -87,7 +87,7 @@ protected CharacterStateSet createCharacterStateSet(Element statesElement) {
8787 mCharacterStateSets .add (charStateSet ); // XXX Make this into a setter?
8888 return charStateSet ;
8989 }
90-
90+
9191 /**
9292 * This is equivalent to creating a <states> element, i.e.
9393 * a container for state elements, polymorphic_state_set elements
@@ -96,6 +96,7 @@ protected CharacterStateSet createCharacterStateSet(Element statesElement) {
9696 * If the format element object doesn't exist yet it's created here
9797 * @author rvosa
9898 */
99+ @ Override
99100 public CharacterStateSet createCharacterStateSet () {
100101 CharacterStateSetImpl characterStateSet = new CharacterStateSetImpl (getDocument ());
101102 List <Element > currentCharElements = getChildrenByTagName (getFormatElement (), "char" );
@@ -119,10 +120,11 @@ else if ( ! currentSetElements.isEmpty() ) {
119120 * (non-Javadoc)
120121 * @see org.nexml.model.CategoricalMatrix#getCharacterStateSets()
121122 */
123+ @ Override
122124 public Set <CharacterStateSet > getCharacterStateSets () {
123125 return Collections .unmodifiableSet (mCharacterStateSets );
124126 }
125-
127+
126128 /**
127129 * This method creates a char element, i.e. a column definition.
128130 * Because NeXML requires for categorical matrices that these
@@ -131,14 +133,15 @@ public Set<CharacterStateSet> getCharacterStateSets() {
131133 * in here, from which the attribute's value is set.
132134 * @author rvosa
133135 */
136+ @ Override
134137 public Character createCharacter (CharacterStateSet characterStateSet ) {
135138 CharacterImpl character = new CharacterImpl (getDocument ());
136139 addThing (character );
137140 character .setCharacterStateSet (characterStateSet );
138141 attachFundamentalDataElement (getFormatElement (), character .getElement ());
139142 return character ;
140143 }
141-
144+
142145 protected Character createCharacter (Element element ) {
143146 CharacterImpl character = new CharacterImpl (getDocument (),element );
144147 addThing (character );
@@ -147,7 +150,7 @@ protected Character createCharacter(Element element) {
147150 character .setCharacterStateSet (stateSet );
148151 return character ;
149152 }
150-
153+
151154 protected CharacterStateSet lookupCharacterStateSetById (String stateSetId ) {
152155 if ( null == stateSetId ) {
153156 return null ;
@@ -161,68 +164,67 @@ protected CharacterStateSet lookupCharacterStateSetById(String stateSetId) {
161164 }
162165
163166 public CharacterStateSet getDNACharacterStateSet () {
164- if (mMolecularCharacterStates == null ){
165- mMolecularCharacterStates = new MolecularCharacterStateSetImpl (getDocument ());
166- }
167- CharacterStateSet result = mMolecularCharacterStates .getDNAStateSet ();
168- CharacterStateSetImpl characterStateSet = (CharacterStateSetImpl )result ;
169- if (mCharacterStateSets .add (characterStateSet )){
170- if ( null == getFormatElement () ) {
171- setFormatElement ( getDocument ().createElementNS (DEFAULT_NAMESPACE ,"format" ) );
172- getElement ().insertBefore ( getFormatElement (), getElement ().getFirstChild () );
173- }
174- getFormatElement ().insertBefore ( characterStateSet .getElement (), getFormatElement ().getFirstChild () );
175- }
176- return result ;
167+ if (mMolecularCharacterStates == null ){
168+ mMolecularCharacterStates = new MolecularCharacterStateSetImpl (getDocument ());
169+ }
170+ CharacterStateSet result = mMolecularCharacterStates .getDNAStateSet ();
171+ CharacterStateSetImpl characterStateSet = (CharacterStateSetImpl )result ;
172+ if (mCharacterStateSets .add (characterStateSet )){
173+ if ( null == getFormatElement () ) {
174+ setFormatElement ( getDocument ().createElementNS (DEFAULT_NAMESPACE ,"format" ) );
175+ getElement ().insertBefore ( getFormatElement (), getElement ().getFirstChild () );
176+ }
177+ getFormatElement ().insertBefore ( characterStateSet .getElement (), getFormatElement ().getFirstChild () );
178+ }
179+ return result ;
177180 }
178181
179182 public CharacterStateSet getRNACharacterStateSet () {
180- if (mMolecularCharacterStates == null ){
181- mMolecularCharacterStates = new MolecularCharacterStateSetImpl (getDocument ());
182- }
183- CharacterStateSet result = mMolecularCharacterStates .getRNAStateSet ();
184- CharacterStateSetImpl characterStateSet = (CharacterStateSetImpl )result ;
185- if (mCharacterStateSets .add (characterStateSet )){
186- if ( null == getFormatElement () ) {
187- setFormatElement ( getDocument ().createElementNS (DEFAULT_NAMESPACE ,"format" ) );
188- getElement ().insertBefore ( getFormatElement (), getElement ().getFirstChild () );
189- }
190- getFormatElement ().insertBefore ( characterStateSet .getElement (), getFormatElement ().getFirstChild () );
191- }
192- return result ;
193- }
194-
195- public CharacterStateSet getProteinCharacterStateSet (){
196- if (mMolecularCharacterStates == null ){
197- mMolecularCharacterStates = new MolecularCharacterStateSetImpl (getDocument ());
198- }
199- CharacterStateSet result = mMolecularCharacterStates .getProteinStateSet ();
200- CharacterStateSetImpl characterStateSet = (CharacterStateSetImpl )result ;
201- if (mCharacterStateSets .add (characterStateSet )){
202- if ( null == getFormatElement () ) {
203- setFormatElement ( getDocument ().createElementNS (DEFAULT_NAMESPACE ,"format" ) );
204- getElement ().insertBefore ( getFormatElement (), getElement ().getFirstChild () );
205- }
206- getFormatElement ().insertBefore ( characterStateSet .getElement (), getFormatElement ().getFirstChild () );
207- }
208- return result ;
209- }
210-
211- public CharacterState parseSymbol (String symbol ) {
212- CharacterStateSet lastSet = null ;
213- for ( CharacterStateSet stateSet : getCharacterStateSets () ) {
214- CharacterState state = stateSet .lookupCharacterStateBySymbol (symbol );
215- if ( null != state ) {
216- return state ;
183+ if (mMolecularCharacterStates == null ){
184+ mMolecularCharacterStates = new MolecularCharacterStateSetImpl (getDocument ());
185+ }
186+ CharacterStateSet result = mMolecularCharacterStates .getRNAStateSet ();
187+ CharacterStateSetImpl characterStateSet = (CharacterStateSetImpl )result ;
188+ if (mCharacterStateSets .add (characterStateSet )){
189+ if ( null == getFormatElement () ) {
190+ setFormatElement ( getDocument ().createElementNS (DEFAULT_NAMESPACE ,"format" ) );
191+ getElement ().insertBefore ( getFormatElement (), getElement ().getFirstChild () );
217192 }
218- lastSet = stateSet ;
193+ getFormatElement ().insertBefore ( characterStateSet .getElement (), getFormatElement ().getFirstChild () );
194+ }
195+ return result ;
196+ }
197+
198+ public CharacterStateSet getProteinCharacterStateSet (){
199+ if (mMolecularCharacterStates == null ){
200+ mMolecularCharacterStates = new MolecularCharacterStateSetImpl (getDocument ());
201+ }
202+ CharacterStateSet result = mMolecularCharacterStates .getProteinStateSet ();
203+ CharacterStateSetImpl characterStateSet = (CharacterStateSetImpl )result ;
204+ if (mCharacterStateSets .add (characterStateSet )){
205+ if ( null == getFormatElement () ) {
206+ setFormatElement ( getDocument ().createElementNS (DEFAULT_NAMESPACE ,"format" ) );
207+ getElement ().insertBefore ( getFormatElement (), getElement ().getFirstChild () );
208+ }
209+ getFormatElement ().insertBefore ( characterStateSet .getElement (), getFormatElement ().getFirstChild () );
210+ }
211+ return result ;
212+ }
213+
214+ @ Override
215+ public CharacterState parseSymbol (String symbol , Character character ) {
216+ CharacterStateSet stateSet = character .getCharacterStateSet ();
217+ CharacterState state = stateSet .lookupCharacterStateBySymbol (symbol );
218+ if ( null != state ) {
219+ return state ;
220+ } else {
221+ return stateSet .createCharacterState (symbol );
219222 }
220- return lastSet .createCharacterState (symbol );
221223 }
222224
223225 @ Override
224226 String getSplitString () {
225227 return "\\ s+" ;
226228 }
227229
228- }
230+ }
0 commit comments