9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.javafx.css;
27
28 import javafx.css.ParsedValue;
29 import javafx.css.StyleConverter;
30 import javafx.scene.paint.Color;
31 import javafx.scene.text.Font;
32
33 import java.io.DataInputStream;
34 import java.io.DataOutputStream;
35 import java.io.IOException;
36 import java.net.MalformedURLException;
37 import java.net.URL;
38
39 /**
40 * Implementation details behind a {@link ParsedValueImpl}.
41 */
42 public class ParsedValueImpl<V, T> extends ParsedValue<V,T> {
43
44 /**
45 * If value references another property, then the real value needs to
46 * be looked up.
47 */
48 final private boolean lookup;
49 public final boolean isLookup() { return lookup; }
50
51 /**
52 * If value is itself a ParsedValueImpl or sequence of values, and should any of
53 * those values need to be looked up, then this flag is set. This
54 * does not mean that this particular value needs to be looked up, but
55 * that this value contains a value that needs to be looked up.
56 */
57 final private boolean containsLookups;
58 public final boolean isContainsLookups() { return containsLookups; }
59
60 private static boolean getContainsLookupsFlag(Object obj) {
61
62 // Assume the value does not contain lookups
63 boolean containsLookupsFlag = false;
64
65 if (obj instanceof Size) {
66 containsLookupsFlag = false;
67 }
68
69 else if(obj instanceof ParsedValueImpl) {
70 ParsedValueImpl value = (ParsedValueImpl)obj;
71 containsLookupsFlag = value.lookup || value.containsLookups;
72 }
73
74 else if(obj instanceof ParsedValueImpl[]) {
75 ParsedValueImpl[] values = (ParsedValueImpl[])obj;
76 for(int v=0;
77 // Bail if value contains lookups
78 // Continue iterating as long as one of the flags is false
414 }
415
416
417 final static private byte NULL_VALUE = 0;
418 final static private byte VALUE = 1;
419 final static private byte VALUE_ARRAY = 2;
420 final static private byte ARRAY_OF_VALUE_ARRAY = 3;
421 final static private byte STRING = 4;
422 final static private byte COLOR = 5;
423 final static private byte ENUM = 6;
424 final static private byte BOOLEAN = 7;
425 final static private byte URL = 8;
426 final static private byte SIZE = 9;
427
428
429 public final void writeBinary(DataOutputStream os, StringStore stringStore)
430 throws IOException {
431
432 os.writeBoolean(lookup);
433
434 if (converter instanceof StyleConverterImpl) {
435 os.writeBoolean(true);
436 ((StyleConverterImpl)converter).writeBinary(os, stringStore);
437 } else {
438 os.writeBoolean(false);
439 if (converter != null) {
440 System.err.println("cannot writeBinary " + converter.getClass().getName());
441 }
442 }
443
444 if (value instanceof ParsedValue) {
445 os.writeByte(VALUE);
446 final ParsedValue pv = (ParsedValue)value;
447 if (pv instanceof ParsedValueImpl) {
448 ((ParsedValueImpl)pv).writeBinary(os, stringStore);
449 } else {
450 final ParsedValueImpl impl = new ParsedValueImpl(pv.getValue(), pv.getConverter());
451 impl.writeBinary(os, stringStore);
452 }
453
454 } else if (value instanceof ParsedValue[]) {
455 os.writeByte(VALUE_ARRAY);
456 final ParsedValue[] values = (ParsedValue[])value;
457 if (values != null) {
458 os.writeByte(VALUE);
459 } else {
460 os.writeByte(NULL_VALUE);
461 }
548
549 } else if (value instanceof URL) {
550 os.writeByte(URL);
551 final int index = stringStore.addString(value.toString());
552 os.writeShort(index);
553
554 } else if (value == null) {
555 os.writeByte(NULL_VALUE);
556
557 } else {
558 throw new InternalError("cannot writeBinary " + this);
559 }
560 }
561
562 public static ParsedValueImpl readBinary(int bssVersion, DataInputStream is, String[] strings)
563 throws IOException {
564
565 final boolean lookup = is.readBoolean();
566 final boolean hasType = is.readBoolean();
567
568 final StyleConverter converter = (hasType) ? StyleConverterImpl.readBinary(is, strings) : null;
569
570 final int valType = is.readByte();
571
572 if (valType == VALUE) {
573 final ParsedValueImpl value = ParsedValueImpl.readBinary(bssVersion, is, strings);
574 return new ParsedValueImpl(value, converter, lookup);
575
576 } else if (valType == VALUE_ARRAY) {
577 if (bssVersion >= 4) {
578 // This byte was used to denote whether or not array was all nulls.
579 // But really, just need to know nVals
580 is.readByte();
581 }
582 final int nVals = is.readInt();
583 final ParsedValueImpl[] values = (nVals > 0)
584 ? new ParsedValueImpl[nVals]
585 : null;
586 for (int v=0; v<nVals; v++) {
587 int vtype = is.readByte();
588 if (vtype == VALUE) {
668 String unitStr = strings[is.readShort()];
669 try {
670 units = Enum.valueOf(SizeUnits.class, unitStr);
671 } catch (IllegalArgumentException iae) {
672 System.err.println(iae.toString());
673 } catch (NullPointerException npe) {
674 System.err.println(npe.toString());
675 }
676 return new ParsedValueImpl<Size,Size>(new Size(val,units), converter, lookup);
677
678 } else if (valType == STRING) {
679 String str = strings[is.readShort()];
680 return new ParsedValueImpl(str, converter, lookup);
681
682 } else if (valType == URL) {
683 String str = strings[is.readShort()];
684 try {
685 URL url = new URL(str);
686 return new ParsedValueImpl(url, converter, lookup);
687 } catch (MalformedURLException malf) {
688 throw new InternalError("Excpeption in Value.readBinary: " + malf);
689 }
690
691 } else if (valType == NULL_VALUE) {
692 return new ParsedValueImpl(null, converter, lookup);
693
694 } else {
695 throw new InternalError("unknown type: " + valType);
696 }
697 }
698 }
|
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.javafx.css;
27
28 import javafx.css.ParsedValue;
29 import javafx.css.Size;
30 import javafx.css.SizeUnits;
31 import javafx.css.StyleConverter;
32 import javafx.css.StyleConverter.StringStore;
33 import javafx.scene.paint.Color;
34 import javafx.scene.text.Font;
35
36 import java.io.DataInputStream;
37 import java.io.DataOutputStream;
38 import java.io.IOException;
39 import java.net.MalformedURLException;
40 import java.net.URL;
41
42 /**
43 * Implementation details behind a {@link ParsedValueImpl}.
44 */
45 public class ParsedValueImpl<V, T> extends ParsedValue<V,T> {
46
47 /**
48 * If value references another property, then the real value needs to
49 * be looked up.
50 */
51 final private boolean lookup;
52 @Override public final boolean isLookup() { return lookup; }
53
54 /**
55 * If value is itself a ParsedValueImpl or sequence of values, and should any of
56 * those values need to be looked up, then this flag is set. This
57 * does not mean that this particular value needs to be looked up, but
58 * that this value contains a value that needs to be looked up.
59 */
60 final private boolean containsLookups;
61 @Override public final boolean isContainsLookups() { return containsLookups; }
62
63 private static boolean getContainsLookupsFlag(Object obj) {
64
65 // Assume the value does not contain lookups
66 boolean containsLookupsFlag = false;
67
68 if (obj instanceof Size) {
69 containsLookupsFlag = false;
70 }
71
72 else if(obj instanceof ParsedValueImpl) {
73 ParsedValueImpl value = (ParsedValueImpl)obj;
74 containsLookupsFlag = value.lookup || value.containsLookups;
75 }
76
77 else if(obj instanceof ParsedValueImpl[]) {
78 ParsedValueImpl[] values = (ParsedValueImpl[])obj;
79 for(int v=0;
80 // Bail if value contains lookups
81 // Continue iterating as long as one of the flags is false
417 }
418
419
420 final static private byte NULL_VALUE = 0;
421 final static private byte VALUE = 1;
422 final static private byte VALUE_ARRAY = 2;
423 final static private byte ARRAY_OF_VALUE_ARRAY = 3;
424 final static private byte STRING = 4;
425 final static private byte COLOR = 5;
426 final static private byte ENUM = 6;
427 final static private byte BOOLEAN = 7;
428 final static private byte URL = 8;
429 final static private byte SIZE = 9;
430
431
432 public final void writeBinary(DataOutputStream os, StringStore stringStore)
433 throws IOException {
434
435 os.writeBoolean(lookup);
436
437 if (converter != null) {
438 os.writeBoolean(true);
439 converter.writeBinary(os, stringStore);
440 } else {
441 os.writeBoolean(false);
442 }
443
444 if (value instanceof ParsedValue) {
445 os.writeByte(VALUE);
446 final ParsedValue pv = (ParsedValue)value;
447 if (pv instanceof ParsedValueImpl) {
448 ((ParsedValueImpl)pv).writeBinary(os, stringStore);
449 } else {
450 final ParsedValueImpl impl = new ParsedValueImpl(pv.getValue(), pv.getConverter());
451 impl.writeBinary(os, stringStore);
452 }
453
454 } else if (value instanceof ParsedValue[]) {
455 os.writeByte(VALUE_ARRAY);
456 final ParsedValue[] values = (ParsedValue[])value;
457 if (values != null) {
458 os.writeByte(VALUE);
459 } else {
460 os.writeByte(NULL_VALUE);
461 }
548
549 } else if (value instanceof URL) {
550 os.writeByte(URL);
551 final int index = stringStore.addString(value.toString());
552 os.writeShort(index);
553
554 } else if (value == null) {
555 os.writeByte(NULL_VALUE);
556
557 } else {
558 throw new InternalError("cannot writeBinary " + this);
559 }
560 }
561
562 public static ParsedValueImpl readBinary(int bssVersion, DataInputStream is, String[] strings)
563 throws IOException {
564
565 final boolean lookup = is.readBoolean();
566 final boolean hasType = is.readBoolean();
567
568 final StyleConverter converter = (hasType) ? StyleConverter.readBinary(is, strings) : null;
569
570 final int valType = is.readByte();
571
572 if (valType == VALUE) {
573 final ParsedValueImpl value = ParsedValueImpl.readBinary(bssVersion, is, strings);
574 return new ParsedValueImpl(value, converter, lookup);
575
576 } else if (valType == VALUE_ARRAY) {
577 if (bssVersion >= 4) {
578 // This byte was used to denote whether or not array was all nulls.
579 // But really, just need to know nVals
580 is.readByte();
581 }
582 final int nVals = is.readInt();
583 final ParsedValueImpl[] values = (nVals > 0)
584 ? new ParsedValueImpl[nVals]
585 : null;
586 for (int v=0; v<nVals; v++) {
587 int vtype = is.readByte();
588 if (vtype == VALUE) {
668 String unitStr = strings[is.readShort()];
669 try {
670 units = Enum.valueOf(SizeUnits.class, unitStr);
671 } catch (IllegalArgumentException iae) {
672 System.err.println(iae.toString());
673 } catch (NullPointerException npe) {
674 System.err.println(npe.toString());
675 }
676 return new ParsedValueImpl<Size,Size>(new Size(val,units), converter, lookup);
677
678 } else if (valType == STRING) {
679 String str = strings[is.readShort()];
680 return new ParsedValueImpl(str, converter, lookup);
681
682 } else if (valType == URL) {
683 String str = strings[is.readShort()];
684 try {
685 URL url = new URL(str);
686 return new ParsedValueImpl(url, converter, lookup);
687 } catch (MalformedURLException malf) {
688 throw new InternalError("Exception in Value.readBinary: " + malf);
689 }
690
691 } else if (valType == NULL_VALUE) {
692 return new ParsedValueImpl(null, converter, lookup);
693
694 } else {
695 throw new InternalError("unknown type: " + valType);
696 }
697 }
698 }
|