📄 resourcemanager.java
字号:
/*******************************************************************************
* Copyright (c) 2004 Stefan Zeiger and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.novocode.com/legal/epl-v10.html
*
* Contributors:
* Stefan Zeiger (szeiger@novocode.com) - initial API and implementation
*******************************************************************************/
package com.novocode.naf.resource;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import javax.xml.parsers.*;
import javax.xml.transform.Transformer;
import org.w3c.dom.*;
import com.novocode.naf.app.*;
import com.novocode.naf.xml.DOMUtil;
/**
* Reads NAF resource files and creates NGComponent objects from them.
*
* @author Stefan Zeiger (szeiger@novocode.com)
* @since Nov 24, 2003
*/
public class ResourceManager
{
public static final String NAF_NAMESPACE_URI = "http://www.novocode.com/namespaces/naf";
private DocumentBuilder docbuilder;
private Map<String, NGComponent> resources;
private Map<String, Class> elementClasses;
private StyleManager styleManager = new StyleManager();
private NGComponent rootComponent;
/**
* Create a new ResourceManager.
*/
public ResourceManager() throws NAFException
{
this.resources = new HashMap<String, NGComponent>();
this.elementClasses = new HashMap<String, Class>();
setElementClass("group", NGGroup.class);
setElementClass("include", NGInclude.class);
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setIgnoringComments(true);
this.docbuilder = factory.newDocumentBuilder();
}
catch(Exception ex) { throw new NAFException("Error creating ResourceManager", ex); }
}
public void setElementClass(String name, Class clazz)
{
if(clazz == null) elementClasses.remove(name);
else elementClasses.put(name, clazz);
}
protected void setRootComponent(NGComponent c) { this.rootComponent = c; }
private void readResource(URL resUrl) throws NAFException
{
InputStream in = null;
try
{
in = resUrl.openStream();
ArrayList<Transformer> trList = null;
Document doc = docbuilder.parse(in);
for(Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
{
if(n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE && "xml-stylesheet".equals(n.getNodeName()))
{
ProcessingInstruction pi = (ProcessingInstruction)n;
Map<String, String> attrs = DOMUtil.parseProcessingInstructionAttributes(pi);
if("text/xsl".equals(attrs.get("type")))
{
String href = attrs.get("href");
if(href == null)
throw new NAFException("Style sheet processing instructions must have an \"href\" attribute");
try
{
Transformer t = styleManager.createTransformer(new URL(resUrl, href));
if(trList == null) trList = new ArrayList<Transformer>();
trList.add(t);
}
catch(Exception ex)
{
throw new NAFException("Error reading style sheet resource \""+href+"\"");
}
}
}
}
if(trList != null)
{
for(Transformer t : trList)
{
doc = (Document)styleManager.transform(t, doc);
if(LogSettings.RESOURCEMANAGER_DUMP_TRANSFORMED_STDOUT)
{
System.out.println("Transformed instance:");
DOMUtil.dumpNode(doc, System.out);
System.out.println();
}
}
}
Element rootE = doc.getDocumentElement();
if(!NAF_NAMESPACE_URI.equals(rootE.getNamespaceURI()))
throw new NAFException("Root element does not use the NAF namespace");
String nafid = rootE.getAttribute("id");
if(nafid != null && nafid.length() == 0) nafid = null;
NGComponent comp = createComponent(rootE, nafid, resUrl, rootComponent);
resources.put(resUrl.toExternalForm(), comp);
}
catch(Exception ex)
{
throw new NAFException("Error reading NAF resource \""+resUrl.toExternalForm()+"\"", ex);
}
finally
{
if(in != null) try { in.close(); } catch(Exception ignored) {}
}
}
public NGComponent getResource(URL absUrl) throws NAFException
{
String urlStr = absUrl.toExternalForm();
NGComponent comp = resources.get(urlStr);
if(comp != null) return comp;
String baseStr = urlStr;
int sep = baseStr.indexOf('#');
if(sep != -1) baseStr = baseStr.substring(0, sep);
URL baseUrl;
try
{
baseUrl = (baseStr == urlStr) ? absUrl : new URL(baseStr);
}
catch(MalformedURLException ex)
{
throw new NAFException("Error creating URL object for "+baseStr, ex);
}
readResource(baseUrl);
comp = resources.get(urlStr);
if(comp == null)
throw new NAFException("NAF resource file "+baseStr+" doesn't contain resource \""+urlStr+"\"");
return comp;
}
NGComponent createComponent(Element e, String id, URL resURL, NGComponent parent) throws NAFException
{
String name = e.getLocalName();
Class<?> referencedClass = null;
String using = null;
{
NGComponent c = parent;
findComponentClassName: while(c != null)
{
List<Import> l = c.getImports();
if(l != null)
{
for(Import im : l)
{
referencedClass = im.classFor(name);
if(referencedClass != null)
{
using = im.getUsing();
break findComponentClassName;
}
}
}
c = c.getParent();
}
}
NGComponent ngc = null;
if(referencedClass == null) referencedClass = elementClasses.get(name);
if(referencedClass != null)
{
if(using == null) using = referencedClass.getName();
try
{
Class<?> componentClass = (using == null) ? referencedClass : Class.forName(using);
ngc = (NGComponent)componentClass.newInstance();
}
catch(Exception ex)
{
throw new NAFException("Error instantiating element object from class "+using, ex);
}
}
else throw new NAFException("Unknown element tag \""+name+"\"");
ngc.init(referencedClass, e, resURL, id, this, parent);
if(id != null)
{
String key = resURL.toExternalForm() + '#' + id;
resources.put(key, ngc);
if(LogSettings.RESOURCEMANAGER_DEBUG_STDOUT) System.out.println("Storing resource \""+key+"\" (class "+ngc.getClass().getName()+")");
}
return ngc;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -