1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 * 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 package org.mozilla.javascript.xml; 8 9 import org.mozilla.javascript.*; 10 11 public abstract class XMLLib 12 { 13 private static final Object XML_LIB_KEY = new Object(); 14 15 /** 16 An object which specifies an XMLLib implementation to be used at runtime. 17 18 This interface should be considered experimental. It may be better 19 (and certainly more flexible) to write an interface that returns an 20 XMLLib object rather than a class name, for example. But that would 21 cause many more ripple effects in the code, all the way back to 22 {@link ScriptRuntime}. 23 */ 24 public static abstract class Factory { create(final String className)25 public static Factory create(final String className) { 26 return new Factory() { 27 @Override 28 public String getImplementationClassName() { 29 return className; 30 } 31 }; 32 } 33 getImplementationClassName()34 public abstract String getImplementationClassName(); 35 } 36 37 public static XMLLib extractFromScopeOrNull(Scriptable scope) 38 { 39 ScriptableObject so = ScriptRuntime.getLibraryScopeOrNull(scope); 40 if (so == null) { 41 // If library is not yet initialized, return null 42 return null; 43 } 44 45 // Ensure lazily initialization of real XML library instance 46 // which is done on first access to XML property 47 ScriptableObject.getProperty(so, "XML"); 48 49 return (XMLLib)so.getAssociatedValue(XML_LIB_KEY); 50 } 51 52 public static XMLLib extractFromScope(Scriptable scope) 53 { 54 XMLLib lib = extractFromScopeOrNull(scope); 55 if (lib != null) { 56 return lib; 57 } 58 String msg = ScriptRuntime.getMessage0("msg.XML.not.available"); 59 throw Context.reportRuntimeError(msg); 60 } 61 62 protected final XMLLib bindToScope(Scriptable scope) 63 { 64 ScriptableObject so = ScriptRuntime.getLibraryScopeOrNull(scope); 65 if (so == null) { 66 // standard library should be initialized at this point 67 throw new IllegalStateException(); 68 } 69 return (XMLLib)so.associateValue(XML_LIB_KEY, this); 70 } 71 72 public abstract boolean isXMLName(Context cx, Object name); 73 74 public abstract Ref nameRef(Context cx, Object name, 75 Scriptable scope, int memberTypeFlags); 76 77 public abstract Ref nameRef(Context cx, Object namespace, Object name, 78 Scriptable scope, int memberTypeFlags); 79 80 /** 81 * Escapes the reserved characters in a value of an attribute. 82 * 83 * @param value Unescaped text 84 * @return The escaped text 85 */ 86 public abstract String escapeAttributeValue(Object value); 87 88 /** 89 * Escapes the reserved characters in a value of a text node. 90 * 91 * @param value Unescaped text 92 * @return The escaped text 93 */ 94 public abstract String escapeTextValue(Object value); 95 96 97 /** 98 * Construct namespace for default xml statement. 99 */ 100 public abstract Object toDefaultXmlNamespace(Context cx, Object uriValue); 101 102 public void setIgnoreComments(boolean b) { 103 throw new UnsupportedOperationException(); 104 } 105 106 public void setIgnoreWhitespace(boolean b) { 107 throw new UnsupportedOperationException(); 108 } 109 110 public void setIgnoreProcessingInstructions(boolean b) { 111 throw new UnsupportedOperationException(); 112 } 113 114 public void setPrettyPrinting(boolean b) { 115 throw new UnsupportedOperationException(); 116 } 117 118 public void setPrettyIndent(int i) { 119 throw new UnsupportedOperationException(); 120 } 121 122 public boolean isIgnoreComments() { 123 throw new UnsupportedOperationException(); 124 } 125 126 public boolean isIgnoreProcessingInstructions() { 127 throw new UnsupportedOperationException(); 128 } 129 130 public boolean isIgnoreWhitespace() { 131 throw new UnsupportedOperationException(); 132 } 133 134 public boolean isPrettyPrinting() { 135 throw new UnsupportedOperationException(); 136 } 137 138 public int getPrettyIndent() { 139 throw new UnsupportedOperationException(); 140 } 141 } 142