|
| 1 | +package org.jvnet.jaxb2_commons.plugin.customizations; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.text.MessageFormat; |
| 8 | +import java.util.LinkedList; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import javax.xml.namespace.QName; |
| 12 | +import javax.xml.parsers.DocumentBuilder; |
| 13 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 14 | +import javax.xml.parsers.ParserConfigurationException; |
| 15 | + |
| 16 | +import org.jvnet.jaxb2_commons.plugin.AbstractParameterizablePlugin; |
| 17 | +import org.jvnet.jaxb2_commons.util.ClassUtils; |
| 18 | +import org.w3c.dom.Document; |
| 19 | +import org.w3c.dom.Element; |
| 20 | +import org.w3c.dom.Node; |
| 21 | +import org.w3c.dom.NodeList; |
| 22 | +import org.xml.sax.ErrorHandler; |
| 23 | +import org.xml.sax.SAXException; |
| 24 | + |
| 25 | +import com.sun.tools.xjc.model.CClassInfo; |
| 26 | +import com.sun.tools.xjc.model.CEnumConstant; |
| 27 | +import com.sun.tools.xjc.model.CEnumLeafInfo; |
| 28 | +import com.sun.tools.xjc.model.CPluginCustomization; |
| 29 | +import com.sun.tools.xjc.model.CPropertyInfo; |
| 30 | +import com.sun.tools.xjc.model.Model; |
| 31 | + |
| 32 | +public class CustomizationsPlugin extends AbstractParameterizablePlugin { |
| 33 | + |
| 34 | + private final DocumentBuilderFactory documentBuilderFactory; |
| 35 | + { |
| 36 | + documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
| 37 | + documentBuilderFactory.setNamespaceAware(true); |
| 38 | + } |
| 39 | + |
| 40 | + private File directory; |
| 41 | + |
| 42 | + public File getDirectory() { |
| 43 | + return directory; |
| 44 | + } |
| 45 | + |
| 46 | + public void setDirectory(File customizationsDirectory) { |
| 47 | + this.directory = customizationsDirectory; |
| 48 | + } |
| 49 | + |
| 50 | + private boolean verbose; |
| 51 | + |
| 52 | + public boolean isVerbose() { |
| 53 | + return verbose; |
| 54 | + } |
| 55 | + |
| 56 | + public void setVerbose(boolean verbose) { |
| 57 | + this.verbose = verbose; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String getOptionName() { |
| 62 | + return "Xcustomizations"; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public String getUsage() { |
| 67 | + return " -Xcustomizations : reads and adds customizations from files"; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void postProcessModel(Model model, ErrorHandler errorHandler) { |
| 72 | + if (getDirectory() == null) { |
| 73 | + logger.warn( |
| 74 | + "Customizations directory is not provided, please use the -Xcustomizations-directory=<directory> command line argument to provide it."); |
| 75 | + } else if (!getDirectory().exists()) { |
| 76 | + logger.warn(MessageFormat.format("Customizations directory [{0}] does not exist.", |
| 77 | + getDirectory().getAbsolutePath())); |
| 78 | + } else if (!getDirectory().isDirectory()) { |
| 79 | + logger.warn(MessageFormat.format("Customizations directory [{0}] is not a directory.", |
| 80 | + getDirectory().getAbsolutePath())); |
| 81 | + } else { |
| 82 | + postProcessModel(model); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void postProcessModel(Model model) { |
| 87 | + for (final CClassInfo classInfo : model.beans().values()) { |
| 88 | + postProcessClassInfo(model, classInfo); |
| 89 | + } |
| 90 | + for (final CEnumLeafInfo enumLeafInfo : model.enums().values()) { |
| 91 | + postProcessEnumLeafInfo(model, enumLeafInfo); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private void postProcessClassInfo(Model model, CClassInfo classInfo) { |
| 96 | + final String packagedClassName = ClassUtils.getPackagedClassName(classInfo); |
| 97 | + final String customizationsFileName = packagedClassName.replace(".", "/") + ".xml"; |
| 98 | + |
| 99 | + final List<CPluginCustomization> customizations = readCustomizations(customizationsFileName); |
| 100 | + |
| 101 | + classInfo.getCustomizations().addAll(customizations); |
| 102 | + |
| 103 | + for (CPropertyInfo propertyInfo: classInfo.getProperties()) |
| 104 | + { |
| 105 | + postProcessPropertyInfo(model, classInfo, propertyInfo); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private void postProcessPropertyInfo(Model model, CClassInfo classInfo, CPropertyInfo propertyInfo) { |
| 110 | + final String packagedClassName = ClassUtils.getPackagedClassName(classInfo); |
| 111 | + final String customizationsFileName = packagedClassName.replace(".", "/") + "." +propertyInfo.getName(false) + ".xml"; |
| 112 | + final List<CPluginCustomization> customizations = readCustomizations(customizationsFileName); |
| 113 | + propertyInfo.getCustomizations().addAll(customizations); |
| 114 | + } |
| 115 | + |
| 116 | + private void postProcessEnumLeafInfo(Model model, CEnumLeafInfo enumLeafInfo) { |
| 117 | + final String packagedClassName = ClassUtils.getPackagedClassName(enumLeafInfo); |
| 118 | + final String customizationsFileName = packagedClassName.replace(".", "/") + ".xml"; |
| 119 | + |
| 120 | + final List<CPluginCustomization> customizations = readCustomizations(customizationsFileName); |
| 121 | + |
| 122 | + enumLeafInfo.getCustomizations().addAll(customizations); |
| 123 | + for (CEnumConstant enumConstant : enumLeafInfo.getConstants()) { |
| 124 | + postProcessEnumConstant(model, enumLeafInfo, enumConstant); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private void postProcessEnumConstant(Model model, CEnumLeafInfo enumLeafInfo, CEnumConstant enumConstant) { |
| 129 | + final String packagedClassName = ClassUtils.getPackagedClassName(enumLeafInfo); |
| 130 | + final String customizationsFileName = packagedClassName.replace(".", "/") + "." +enumConstant.getName() + ".xml"; |
| 131 | + final List<CPluginCustomization> customizations = readCustomizations(customizationsFileName); |
| 132 | + enumConstant.getCustomizations().addAll(customizations); |
| 133 | + } |
| 134 | + |
| 135 | + private List<CPluginCustomization> readCustomizations(String fileName) { |
| 136 | + final List<CPluginCustomization> customizations = new LinkedList<CPluginCustomization>(); |
| 137 | + DocumentBuilder documentBuilder = null; |
| 138 | + |
| 139 | + try { |
| 140 | + documentBuilder = documentBuilderFactory.newDocumentBuilder(); |
| 141 | + } catch (ParserConfigurationException pcex) { |
| 142 | + throw new UnsupportedOperationException("Could not created the DOM parser.", pcex); |
| 143 | + } |
| 144 | + |
| 145 | + final File file = new File(getDirectory(), fileName); |
| 146 | + if (!file.exists()) { |
| 147 | + if (isVerbose()) { |
| 148 | + logger.debug(MessageFormat.format("File [{0}] does not exist.", file.getAbsolutePath())); |
| 149 | + } |
| 150 | + } else if (!file.isFile()) { |
| 151 | + logger.warn(MessageFormat.format("File [{0}] is not a file.", file.getAbsolutePath())); |
| 152 | + } else { |
| 153 | + InputStream is = null; |
| 154 | + try { |
| 155 | + is = new FileInputStream(file); |
| 156 | + final Document document = documentBuilder.parse(is); |
| 157 | + final Element documentElement = document.getDocumentElement(); |
| 158 | + logger.debug(MessageFormat.format("Loaded customizations from [{0}].", file.getAbsolutePath())); |
| 159 | + final QName documentElementName = new QName(documentElement.getNamespaceURI(), |
| 160 | + documentElement.getLocalName()); |
| 161 | + if (Customizations.CUSTOMIZATIONS_ELEMENT_NAME.equals(documentElementName)) { |
| 162 | + final NodeList childNodes = documentElement.getChildNodes(); |
| 163 | + for (int index = 0; index < childNodes.getLength(); index++) { |
| 164 | + final Node childNode = childNodes.item(index); |
| 165 | + if (childNode.getNodeType() == Node.ELEMENT_NODE) { |
| 166 | + final Element childElement = (Element) childNode; |
| 167 | + customizations.add(new CPluginCustomization(childElement, null)); |
| 168 | + } |
| 169 | + } |
| 170 | + } else { |
| 171 | + customizations.add(new CPluginCustomization(documentElement, null)); |
| 172 | + } |
| 173 | + |
| 174 | + } catch (IOException ioex) { |
| 175 | + logger.warn(MessageFormat.format("Could not parse [{0}].", file.getAbsolutePath()), ioex); |
| 176 | + } catch (SAXException saxex) { |
| 177 | + logger.warn(MessageFormat.format("Could not parse [{0}].", file.getAbsolutePath()), saxex); |
| 178 | + } finally { |
| 179 | + if (is != null) { |
| 180 | + try { |
| 181 | + is.close(); |
| 182 | + } catch (IOException ignored) { |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + return customizations; |
| 188 | + } |
| 189 | +} |
0 commit comments