|
Apache CXF example source code file (ToolSpec.java)
The Apache CXF ToolSpec.java source code/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.cxf.tools.common.toolspec; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import javax.xml.xpath.XPathConstants; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.apache.cxf.common.i18n.Message; import org.apache.cxf.common.logging.LogUtils; import org.apache.cxf.helpers.DOMUtils; import org.apache.cxf.helpers.XPathUtils; import org.apache.cxf.tools.common.ToolException; import org.apache.cxf.tools.common.dom.ExtendedDocumentBuilder; public class ToolSpec { private static final Logger LOG = LogUtils.getL7dLogger(ToolSpec.class); private final ExtendedDocumentBuilder builder = new ExtendedDocumentBuilder(); private Document doc; private Tool handler; public ToolSpec() { } public ToolSpec(InputStream in) throws ToolException { this(in, true); } public ToolSpec(InputStream in, boolean validate) throws ToolException { if (in == null) { throw new NullPointerException("Cannot create a ToolSpec object from a null stream"); } try { builder.setValidating(validate); this.doc = builder.parse(in); } catch (Exception ex) { Message message = new Message("FAIL_TO_PARSING_TOOLSPCE_STREAM", LOG); throw new ToolException(message, ex); } } public ToolSpec(Document d) { if (d == null) { throw new NullPointerException("Cannot create a ToolSpec object from " + "a null org.w3c.dom.Document"); } this.doc = d; } public ExtendedDocumentBuilder getDocumentBuilder() { return builder; } public boolean isValidInputStream(String id) { Element streams = getStreams(); if (streams == null) { return false; } List<Element> elemList = DOMUtils.findAllElementsByTagNameNS(streams, Tool.TOOL_SPEC_PUBLIC_ID, "instream"); for (Element elem : elemList) { if (elem.getAttribute("id").equals(id)) { return true; } } return false; } public Element getElementById(String id) { Element ele = doc.getElementById(id); if (ele != null) { return ele; } XPathUtils xpather = new XPathUtils(new HashMap<String, String>()); NodeList nl = (NodeList) xpather.getValue("//*[@id='" + id + "']", doc, XPathConstants.NODESET); if (nl != null && nl.getLength() > 0) { return (Element)nl.item(0); } return null; } public boolean hasHandler() { return doc.getDocumentElement().hasAttribute("handler"); } public Tool getHandler() throws ToolException { if (!hasHandler()) { return null; } if (handler == null) { String handlerClz = doc.getDocumentElement().getAttribute("handler"); try { handler = (Tool)Class.forName(handlerClz).newInstance(); } catch (Exception ex) { Message message = new Message("FAIL_TO_INSTANTIATE_HANDLER", LOG, handlerClz); throw new ToolException(message, ex); } } return handler; } public Tool getHandler(ClassLoader loader) throws ToolException { if (!hasHandler()) { return null; } if (handler == null) { String handlerClz = doc.getDocumentElement().getAttribute("handler"); try { handler = (Tool)Class.forName(handlerClz, true, loader).newInstance(); } catch (Exception ex) { Message message = new Message("FAIL_TO_INSTANTIATE_HANDLER", LOG, handlerClz); throw new ToolException(message, ex); } } return handler; } public Element getStreams() { List<Element> elemList = DOMUtils.findAllElementsByTagNameNS(doc.getDocumentElement(), Tool.TOOL_SPEC_PUBLIC_ID, "streams"); if (elemList.size() > 0) { return elemList.get(0); } else { return null; } } public List getInstreamIds() { List<Object> res = new ArrayList |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.