下面列出了org.xml.sax.ext.Locator2#org.xml.sax.helpers.AttributesImpl 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Output the supported lock types XML element
*
* @param xml XMLWriter
*/
protected void writeLockTypes(XMLWriter xml)
{
try
{
AttributesImpl nullAttr = getDAVHelper().getNullAttributes();
xml.startElement(WebDAV.DAV_NS, WebDAV.XML_SUPPORTED_LOCK, WebDAV.XML_NS_SUPPORTED_LOCK, nullAttr);
// Output exclusive lock
// Shared locks are not supported, as they cannot be supported by the LockService (relevant to ALF-16449).
writeLock(xml, WebDAV.XML_NS_EXCLUSIVE);
xml.endElement(WebDAV.DAV_NS, WebDAV.XML_SUPPORTED_LOCK, WebDAV.XML_NS_SUPPORTED_LOCK);
}
catch (Exception ex)
{
throw new AlfrescoRuntimeException("XML write error", ex);
}
}
public void toXML(ContentHandler aContentHandler, boolean aWriteDefaultNamespaceAttribute)
throws SAXException {
// write the element's start tag
AttributesImpl attrs = new AttributesImpl();
attrs.addAttribute("", "syntax", "syntax", "", getSyntax());
// start element
aContentHandler.startElement("", "filter", "filter", attrs);
// write content
String expr = getExpression();
aContentHandler.characters(expr.toCharArray(), 0, expr.length());
// end element
aContentHandler.endElement("", "filter", "filter");
}
public void startNode(NodeRef nodeRef)
{
try
{
AttributesImpl attrs = new AttributesImpl();
Path path = nodeService.getPath(nodeRef);
if (path.size() > 1)
{
// a child name does not exist for root
Path.ChildAssocElement pathElement = (Path.ChildAssocElement)path.last();
QName childQName = pathElement.getRef().getQName();
attrs.addAttribute(NamespaceService.REPOSITORY_VIEW_1_0_URI, CHILDNAME_LOCALNAME, CHILDNAME_QNAME.toPrefixString(), null, toPrefixString(childQName));
}
QName type = nodeService.getType(nodeRef);
contentHandler.startElement(type.getNamespaceURI(), type.getLocalName(), toPrefixString(type), attrs);
}
catch (SAXException e)
{
throw new ExporterException("Failed to process start node event - node ref " + nodeRef.toString(), e);
}
}
private void addValueElement(final String element, final String name,
final String desc, final String value) {
AttributesImpl att = new AttributesImpl();
if (name != null) {
att.addAttribute("", "name", "name", "", name);
}
if (desc != null) {
att.addAttribute("", "desc", "desc", "", desc);
}
if (value != null) {
att.addAttribute("", "value", "value", "",
SAXClassAdapter.encode(value));
}
sa.addElement(element, att);
}
private static void appendXAttrsToXml(ContentHandler contentHandler,
List<XAttr> xAttrs) throws SAXException {
for (XAttr xAttr: xAttrs) {
contentHandler.startElement("", "", "XATTR", new AttributesImpl());
XMLUtils.addSaxString(contentHandler, "NAMESPACE",
xAttr.getNameSpace().toString());
XMLUtils.addSaxString(contentHandler, "NAME", xAttr.getName());
if (xAttr.getValue() != null) {
try {
XMLUtils.addSaxString(contentHandler, "VALUE",
XAttrCodec.encodeValue(xAttr.getValue(), XAttrCodec.HEX));
} catch (IOException e) {
throw new SAXException(e);
}
}
contentHandler.endElement("", "", "XATTR");
}
}
/**
* Generates XML for the client-subscription tag
* @param bridge instance of <code>CacheServer</code>
*
* @since 5.7
*/
private void generateClientHaQueue(CacheServer bridge) throws SAXException {
AttributesImpl atts = new AttributesImpl();
ClientSubscriptionConfigImpl csc = (ClientSubscriptionConfigImpl)bridge.getClientSubscriptionConfig();
try {
atts.addAttribute("", "", CLIENT_SUBSCRIPTION_EVICTION_POLICY, "", csc.getEvictionPolicy());
atts.addAttribute("", "", CLIENT_SUBSCRIPTION_CAPACITY, "", String.valueOf(csc.getCapacity()));
if (this.version.compareTo(VERSION_6_5) >= 0) {
String dsVal = csc.getDiskStoreName();
if (dsVal != null) {
atts.addAttribute("", "", DISK_STORE_NAME, "", dsVal);
}
}
if (csc.getDiskStoreName() == null && csc.hasOverflowDirectory()) {
atts.addAttribute("", "", OVERFLOW_DIRECTORY, "", csc.getOverflowDirectory());
}
handler.startElement("", CLIENT_SUBSCRIPTION, CLIENT_SUBSCRIPTION, atts);
handler.endElement("", CLIENT_SUBSCRIPTION, CLIENT_SUBSCRIPTION);
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Test
public void testStartElement() throws SAXException {
handler.startDocument();
Locator2 locator = Mockito.mock(Locator2.class);
handler.setDocumentLocator(locator);
Mockito.when(locator.getLineNumber()).thenReturn(1);
Mockito.when(locator.getColumnNumber()).thenReturn(7);
handler.startElement("", "", "element", new AttributesImpl());
assertEquals(1, handler.getElementStack().size());
Element element = handler.getElementStack().pop();
DocumentLocation location = (DocumentLocation) element.getUserData("location");
assertEquals(1, location.getLineNumber());
assertEquals(7, location.getColumnNumber());
}
@Override
public FieldVisitor visitField(final int access, final String name,
final String desc, final String signature, final Object value) {
StringBuilder sb = new StringBuilder();
appendAccess(access | ACCESS_FIELD, sb);
AttributesImpl att = new AttributesImpl();
att.addAttribute("", "access", "access", "", sb.toString());
att.addAttribute("", "name", "name", "", name);
att.addAttribute("", "desc", "desc", "", desc);
if (signature != null) {
att.addAttribute("", "signature", "signature", "",
encode(signature));
}
if (value != null) {
att.addAttribute("", "value", "value", "", encode(value.toString()));
}
return new SAXFieldAdapter(sa, att);
}
private void writePermission(ManifestPermission permission) throws SAXException
{
AttributesImpl attributes = new AttributesImpl();
attributes.addAttribute(TransferModel.TRANSFER_MODEL_1_0_URI, "status", "status", "String",
permission.getStatus());
attributes.addAttribute(TransferModel.TRANSFER_MODEL_1_0_URI, "authority", "authority", "String",
permission.getAuthority());
attributes.addAttribute(TransferModel.TRANSFER_MODEL_1_0_URI, "permission", "permission", "String",
permission.getPermission());
writer.startElement(TransferModel.TRANSFER_MODEL_1_0_URI,
ManifestModel.LOCALNAME_ELEMENT_ACL_PERMISSION, PREFIX + ":"
+ ManifestModel.LOCALNAME_ELEMENT_ACL_PERMISSION, attributes);
writer.endElement(TransferModel.TRANSFER_MODEL_1_0_URI,
ManifestModel.LOCALNAME_ELEMENT_ACL_PERMISSION, PREFIX + ":"
+ ManifestModel.LOCALNAME_ELEMENT_ACL_PERMISSION);
}
@SuppressWarnings("unchecked")
private void writeCategory (NodeRef nodeRef, ManifestCategory value) throws SAXException
{
AttributesImpl attributes = new AttributesImpl();
attributes.addAttribute(TransferModel.TRANSFER_MODEL_1_0_URI, "path", "path", "String",
value.getPath());
writer.startElement(TransferModel.TRANSFER_MODEL_1_0_URI,
ManifestModel.LOCALNAME_ELEMENT_CATEGORY, PREFIX + ":"
+ ManifestModel.LOCALNAME_ELEMENT_CATEGORY, attributes);
writeValue(nodeRef);
writer.endElement(TransferModel.TRANSFER_MODEL_1_0_URI,
ManifestModel.LOCALNAME_ELEMENT_CATEGORY, PREFIX + ":"
+ ManifestModel.LOCALNAME_ELEMENT_CATEGORY);
}
public void testCategory() throws Exception {
char[] text;
AttributesImpl attributes = new AttributesImpl();
handler.startElement(null, null, "category", attributes);
handler.startElement(null, null, "pattern", attributes);
handler.characters(text = toCharArray("HELLO ALICE I AM *"), 0,
text.length);
handler.endElement(null, null, "pattern");
handler.startElement(null, null, "that", attributes);
handler.characters(text = toCharArray("TEST"), 0, text.length);
handler.endElement(null, null, "that");
handler.startElement(null, null, "template", attributes);
handler.characters(text = toCharArray("Hello "), 0, text.length);
handler.startElement(null, null, "star", attributes);
handler.characters(text = toCharArray(", nice to meet you."), 0,
text.length);
handler.endElement(null, null, "template");
handler.endElement(null, null, "category");
Category actual = (Category) stack.peek();
Category expected = new Category(new Pattern("HELLO ALICE I AM *"),
new That("TEST"), new Template("Hello ", new Star(1),
", nice to meet you."));
assertEquals(expected, actual);
}
public void testFormal() throws Exception {
char[] text;
AttributesImpl attributes = new AttributesImpl();
handler.startElement(null, null, "formal", attributes);
handler.characters(
text = toCharArray("change this input case to title case"), 0,
text.length);
handler.endElement(null, null, "formal");
Formal expected = new Formal("change this input case to title case");
Formal actual = (Formal) stack.peek();
assertEquals(expected, actual);
assertEquals("Change This Input Case To Title Case",
actual.process(null));
}
public void addMissingAttributes(String elemName, AttributesImpl attributes){
Map<String, DTDAttribute> attList = this.attributes.get(elemName);
if(attList==null)
return;
for(DTDAttribute dtdAttr: attList.values()){
switch(dtdAttr.valueType){
case DEFAULT:
case FIXED:
if(attributes.getIndex(dtdAttr.name)==-1 && !dtdAttr.isNamespace()){
AttributeType type = dtdAttr.type==AttributeType.ENUMERATION ? AttributeType.NMTOKEN : dtdAttr.type;
String namespaceURI = "";
String localName = dtdAttr.name;
String qname = localName;
int colon = qname.indexOf(':');
if(colon!=-1){
localName = qname.substring(colon+1);
String prefix = qname.substring(0, colon);
if(prefix.length()>0)
namespaceURI = reader.getNamespaceURI(prefix);
}
attributes.addAttribute(namespaceURI, localName, qname, type.name(), dtdAttr.value);
}
}
}
}
public void testJavascript() throws Exception {
char[] text;
AttributesImpl attributes = new AttributesImpl();
handler.startElement(null, null, "javascript", attributes);
handler.characters(text = toCharArray("Anything can go here"), 0,
text.length);
handler.endElement(null, null, "javascript");
Javascript expected = new Javascript("Anything can go here");
Javascript actual = (Javascript) stack.peek();
assertEquals(expected, actual);
assertFalse(expected
.equals(new Javascript("Anything else can go here")));
// assertEquals("", actual.process(null));
}
private void write(AssociationIF association, int number) {
AttributesImpl attributes = reifier(association);
attributes.addAttribute("", "", AT_NUMBER, null, "" + number);
startElement(EL_ASSOCIATION, attributes);
attributes.clear();
writeType(association);
Object[] roles = association.getRoles().toArray();
Arrays.sort(roles, associationRoleComparator);
for (int ix = 0; ix < roles.length; ix++)
write((AssociationRoleIF) roles[ix], ix + 1);
write(association.getScope());
writeLocators(association.getItemIdentifiers(), EL_ITEMIDENTIFIERS);
endElement(EL_ASSOCIATION);
}
public void testTemplate() throws Exception {
char[] text;
AttributesImpl attributes = new AttributesImpl();
handler.startElement(null, null, "template", attributes);
handler.characters(text = toCharArray("Hello "), 0, text.length);
handler.startElement(null, null, "star", attributes);
handler.characters(text = toCharArray(",\nnice to meet you, too."), 0,
text.length);
handler.endElement(null, null, "template");
Template tag = (Template) stack.peek();
assertEquals(new Template("Hello ", new Star(1),
", nice to meet you, too."), tag);
}
private static void saxCapabilityProperty(String n, String t, String v, ContentHandler handler)
throws SAXException {
AttributesImpl atts = new AttributesImpl();
addAttr(atts, CapabilityPropertyHandler.NAME, n);
if (t != null) {
addAttr(atts, CapabilityPropertyHandler.TYPE, t);
}
addAttr(atts, CapabilityPropertyHandler.VALUE, v);
handler.startElement("", CapabilityPropertyHandler.CAPABILITY_PROPERTY,
CapabilityPropertyHandler.CAPABILITY_PROPERTY, atts);
handler.endElement("", CapabilityPropertyHandler.CAPABILITY_PROPERTY,
CapabilityPropertyHandler.CAPABILITY_PROPERTY);
handler.characters("\n".toCharArray(), 0, 1);
}
private void generateGatewayReceiver(Cache cache) throws SAXException{
Set<GatewayReceiver> receiverList = cache.getGatewayReceivers();
for (GatewayReceiver receiver : receiverList) {
AttributesImpl atts = new AttributesImpl();
//start port
if (generateDefaults() || receiver.getStartPort() != GatewayReceiver.DEFAULT_START_PORT)
atts.addAttribute("", "", START_PORT, "", String.valueOf(receiver.getStartPort()));
//end port
if (generateDefaults() || receiver.getEndPort() != GatewayReceiver.DEFAULT_END_PORT)
atts.addAttribute("", "", END_PORT, "", String.valueOf(receiver.getEndPort()));
//bind-address
if (generateDefaults() || (receiver.getBindAddress() != null && !receiver.getBindAddress().equals(GatewayReceiver.DEFAULT_BIND_ADDRESS)))
atts.addAttribute("", "", BIND_ADDRESS, "", receiver.getBindAddress());
//maximum-time-between-pings
if (generateDefaults() || receiver.getMaximumTimeBetweenPings() != GatewayReceiver.DEFAULT_MAXIMUM_TIME_BETWEEN_PINGS)
atts.addAttribute("", "", MAXIMUM_TIME_BETWEEN_PINGS, "", String.valueOf(receiver.getMaximumTimeBetweenPings()));
//socket-buffer-size
if (generateDefaults() || receiver.getSocketBufferSize() != GatewayReceiver.DEFAULT_SOCKET_BUFFER_SIZE)
atts.addAttribute("", "", SOCKET_BUFFER_SIZE, "", String.valueOf(receiver.getSocketBufferSize()));
handler.startElement("", GATEWAY_RECEIVER, GATEWAY_RECEIVER, atts);
for (GatewayTransportFilter gsf : receiver.getGatewayTransportFilters()) {
generateGatewayTransportFilter(gsf);
}
handler.endElement("", GATEWAY_RECEIVER, GATEWAY_RECEIVER);
}
}
public void testStartElement() {
try {
h.startElement("uri", "name", "qname", new AttributesImpl());
} catch (SAXException e) {
throw new RuntimeException(e);
}
}
/**
* Create an instance of the <code>Stylesheet</code> class,
* and then parse, typecheck and compile the instance.
* Must be called after <code>parse()</code>.
*/
public Stylesheet makeStylesheet(SyntaxTreeNode element)
throws CompilerException {
try {
Stylesheet stylesheet;
if (element instanceof Stylesheet) {
stylesheet = (Stylesheet)element;
}
else {
stylesheet = new Stylesheet();
stylesheet.setSimplified();
stylesheet.addElement(element);
stylesheet.setAttributes((AttributesImpl) element.getAttributes());
// Map the default NS if not already defined
if (element.lookupNamespace(EMPTYSTRING) == null) {
element.addPrefixMapping(EMPTYSTRING, EMPTYSTRING);
}
}
stylesheet.setParser(this);
return stylesheet;
}
catch (ClassCastException e) {
ErrorMsg err = new ErrorMsg(ErrorMsg.NOT_STYLESHEET_ERR, element);
throw new CompilerException(err.toString());
}
}
/**
* Create an instance of the <code>Stylesheet</code> class,
* and then parse, typecheck and compile the instance.
* Must be called after <code>parse()</code>.
*/
public Stylesheet makeStylesheet(SyntaxTreeNode element)
throws CompilerException {
try {
Stylesheet stylesheet;
if (element instanceof Stylesheet) {
stylesheet = (Stylesheet)element;
}
else {
stylesheet = new Stylesheet();
stylesheet.setSimplified();
stylesheet.addElement(element);
stylesheet.setAttributes((AttributesImpl) element.getAttributes());
// Map the default NS if not already defined
if (element.lookupNamespace(EMPTYSTRING) == null) {
element.addPrefixMapping(EMPTYSTRING, EMPTYSTRING);
}
}
stylesheet.setParser(this);
return stylesheet;
}
catch (ClassCastException e) {
ErrorMsg err = new ErrorMsg(ErrorMsg.NOT_STYLESHEET_ERR, element);
throw new CompilerException(err.toString());
}
}
@Override
public final void visitMethodInsn(final int opcode, final String owner, final String name, final String desc, final boolean itf) {
AttributesImpl attrs = new AttributesImpl();
attrs.addAttribute("", "owner", "owner", "", owner);
attrs.addAttribute("", "name", "name", "", name);
attrs.addAttribute("", "desc", "desc", "", desc);
attrs.addAttribute("", "itf", "itf", "", itf ? "true" : "false");
sa.addElement(Printer.OPCODES[opcode], attrs);
}
/**
* Javadoc says getLocalName returns null if the index if out of range.
*/
@Test
public void testcase09() {
AttributesImpl attr = new AttributesImpl();
attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE);
attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE,
JEEP_VALUE);
attr.removeAttribute(1);
assertNull(attr.getLocalName(1));
}
/**
* Basic test for getLength().
*/
@Test
public void testcase08() {
AttributesImpl attr = new AttributesImpl();
assertEquals(attr.getLength(), 0);
attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE);
attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE,
JEEP_VALUE);
assertEquals(attr.getLength(), 2);
}
private void fillIsotopePatternElement(IsotopePattern isotopePattern, TransformerHandler hd)
throws SAXException, IOException {
AttributesImpl atts = new AttributesImpl();
DataPoint isotopes[] = isotopePattern.getDataPoints();
for (DataPoint isotope : isotopes) {
hd.startElement("", "", PeakListElementName.ISOTOPE.getElementName(), atts);
String isotopeString = isotope.getMZ() + ":" + isotope.getIntensity();
hd.characters(isotopeString.toCharArray(), 0, isotopeString.length());
hd.endElement("", "", PeakListElementName.ISOTOPE.getElementName());
}
}
/**
* Get the attributes associated with the given START_ELEMENT or ATTRIBUTE
* StAXevent.
*
* @return the StAX attributes converted to an org.xml.sax.Attributes
*/
private Attributes getAttributes() {
AttributesImpl attrs = new AttributesImpl();
int eventType = staxStreamReader.getEventType();
if (eventType != XMLStreamConstants.ATTRIBUTE
&& eventType != XMLStreamConstants.START_ELEMENT) {
throw new InternalError(
"getAttributes() attempting to process: " + eventType);
}
// in SAX, namespace declarations are not part of attributes by default.
// (there's a property to control that, but as far as we are concerned
// we don't use it.) So don't add xmlns:* to attributes.
// gather non-namespace attrs
for (int i = 0; i < staxStreamReader.getAttributeCount(); i++) {
String uri = staxStreamReader.getAttributeNamespace(i);
if(uri==null) uri="";
String localName = staxStreamReader.getAttributeLocalName(i);
String prefix = staxStreamReader.getAttributePrefix(i);
String qName;
if(prefix==null || prefix.length()==0)
qName = localName;
else
qName = prefix + ':' + localName;
String type = staxStreamReader.getAttributeType(i);
String value = staxStreamReader.getAttributeValue(i);
attrs.addAttribute(uri, localName, qName, type, value);
}
return attrs;
}
private void saveRates(HumanResource p, TransformerHandler handler) throws SAXException {
if (!BigDecimal.ZERO.equals(p.getStandardPayRate())) {
AttributesImpl attrs = new AttributesImpl();
addAttribute("name", "standard", attrs);
addAttribute("value", p.getStandardPayRate().toPlainString(), attrs);
emptyElement("rate", attrs, handler);
}
}
public void writeTo(ContentHandler h, ErrorHandler errorHandler) throws SAXException {
String nsUri = name.getNamespaceURI();
String ln = name.getLocalPart();
h.startPrefixMapping("",nsUri);
if(mustUnderstand) {
AttributesImpl attributes = new AttributesImpl();
attributes.addAttribute(soapVersion.nsUri,MUST_UNDERSTAND,"S:"+MUST_UNDERSTAND,"CDATA", getMustUnderstandLiteral(soapVersion));
h.startElement(nsUri,ln,ln,attributes);
} else {
h.startElement(nsUri,ln,ln,EMPTY_ATTS);
}
h.characters(value.toCharArray(),0,value.length());
h.endElement(nsUri,ln,ln);
}
public static void permissionStatusToXml(ContentHandler contentHandler,
PermissionStatus perm) throws SAXException {
contentHandler.startElement("", "", "PERMISSION_STATUS", new AttributesImpl());
XMLUtils.addSaxString(contentHandler, "USERNAME", perm.getUserName());
XMLUtils.addSaxString(contentHandler, "GROUPNAME", perm.getGroupName());
fsPermissionToXml(contentHandler, perm.getPermission());
contentHandler.endElement("", "", "PERMISSION_STATUS");
}
protected Attributes getMinMax(CardinalityConstraintIF constraint) {
AttributesImpl atts = new AttributesImpl();
if (constraint.getMinimum() != 0)
atts.addAttribute("", "", "min", "CDATA",
Integer.toString(constraint.getMinimum()));
if (constraint.getMaximum() != CardinalityConstraintIF.INFINITY)
atts.addAttribute("", "", "max", "CDATA",
Integer.toString(constraint.getMaximum()));
return atts;
}