KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > elementprocessor > impl > poi > hssf > elements > Anchors


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements;
18
19
20 import org.apache.cocoon.components.elementprocessor.types.NumericConverter;
21 import org.apache.cocoon.components.elementprocessor.types.Validator;
22
23 import java.io.IOException JavaDoc;
24
25 /**
26  * Anchors. This particular object is represented in gnumeric's XML as four
27  * integers, space separated. Presumably, each represents an anchor for a
28  * particular direction -- top, bottom, left, right -- but what the reference
29  * is for each anchor is not known, nor is it known which one is top, bottom,
30  * left, or right, or even whether that's the correct interpretation of the
31  * numbers. This is an area of the gnumeric XML that is not terribly well
32  * documented even in their code, and I don't think the features that use
33  * anchors are terribly mature yet.
34  *
35  * @author Marc Johnson (marc_johnson27591@hotmail.com)
36  * @version CVS $Id: Anchors.java 30932 2004-07-29 17:35:38Z vgritsenko $
37  */

38 public class Anchors
39 {
40     private static final int _component_count = 4;
41     private int[] _components =
42         new int[ _component_count ];
43
44     // Each element of an anchor has to be one of these values:
45
public static final int ANCHOR_UNKNOWN = 0;
46     public static final int ANCHOR_PERCENTAGE_FROM_COLROW_ST = 16;
47     public static final int ANCHOR_PERCENTAGE_FROM_COLROW_END = 17;
48     public static final int ANCHOR_PTS_FROM_COLROW_START = 32;
49     public static final int ANCHOR_PTS_FROM_COLROW_END = 33;
50     public static final int ANCHOR_PTS_ABSOLUTE = 48;
51     private static final Validator _validator =
52         new Validator()
53     {
54         public IOException JavaDoc validate(final Number JavaDoc number) {
55             switch (number.intValue()) {
56                 case ANCHOR_UNKNOWN :
57                 case ANCHOR_PERCENTAGE_FROM_COLROW_ST :
58                 case ANCHOR_PERCENTAGE_FROM_COLROW_END :
59                 case ANCHOR_PTS_FROM_COLROW_START :
60                 case ANCHOR_PTS_FROM_COLROW_END :
61                 case ANCHOR_PTS_ABSOLUTE :
62                     return null;
63
64                 default :
65                     return new IOException JavaDoc("\"" + number
66                                            + "\" is not a legal value");
67             }
68         }
69     };
70
71     /**
72      * construct the Anchors object
73      *
74      * @param value the string containing the anchor values
75      *
76      * @exception IOException if the string is badly formed
77      */

78
79     public Anchors(final String JavaDoc value) throws IOException JavaDoc {
80         if (value == null) {
81             throw new IOException JavaDoc("cannot process a null anchors string");
82         }
83         char[] input = value.trim().toCharArray();
84         int index = 0;
85
86         for (int j = 0; j < _component_count; j++) {
87             while (index < input.length
88                     && Character.isWhitespace(input[index])) {
89                 ++index;
90             }
91             if (index == input.length) {
92                 throw new IOException JavaDoc("insufficient anchors in string");
93             }
94             int tailIndex = index;
95
96             while (tailIndex < input.length
97                     && !Character.isWhitespace(input[tailIndex])) {
98                 ++tailIndex;
99             }
100             _components[j] = NumericConverter.extractInteger(new String JavaDoc(input,
101                     index, tailIndex - index), _validator).intValue();
102             index = tailIndex;
103         }
104         if (new String JavaDoc(input, index, input.length - index).trim().length() != 0) {
105             throw new IOException JavaDoc("Too much data in string for "
106                                   + _component_count + " anchors");
107         }
108     }
109
110     /**
111      * @return components
112      */

113
114     public int [] getComponents() {
115         return _components;
116     }
117 } // end public class Anchors
118
Popular Tags