org.nuxeo.common.xmap.XMap类的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(196)

本文整理了Java中org.nuxeo.common.xmap.XMap类的一些代码示例,展示了XMap类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。XMap类的具体详情如下:
包路径:org.nuxeo.common.xmap.XMap
类名称:XMap

XMap介绍

[英]XMap maps an XML file to a java object.

The mapping is described by annotations on java objects.

The following annotations are supported:

  • XObject Mark the object as being mappable to an XML node

  • XNode Map an XML node to a field of a mappable object

  • XNodeList Map an list of XML nodes to a field of a mappable object

  • XNodeMap Map an map of XML nodes to a field of a mappable object

  • XContent Map an XML node content to a field of a mappable object

  • XParent Map a field of the current mappable object to the parent object if any exists The parent object is the mappable object containing the current object as a field
    The mapping is done in 2 steps:

  • The XML file is loaded as a DOM document

  • The DOM document is parsed and the nodes mapping is resolved
    [中]XMap将XML文件映射到java对象。
    映射由java对象上的注释描述。
    支持以下注释:
    *XObject将对象标记为可映射到XML节点
    *XNode将XML节点映射到可映射对象的字段
    *XNodeList将XML节点列表映射到可映射对象的字段
    *XNodeMap将XML节点映射到可映射对象的字段
    *XContent将XML节点内容映射到可映射对象的字段
    *XParent将当前可映射对象的字段映射到父对象(如果存在)。父对象是包含当前对象作为字段的可映射对象
    映射分两步完成:
    *XML文件作为DOM文档加载
    *解析DOM文档并解析节点映射

代码示例

代码示例来源:origin: org.nuxeo.ecm.core/nuxeo-core-api

/**
 * Gets existing descriptor or creates a default one.
 */
protected BinaryManagerRootDescriptor getDescriptor(File configFile) throws IOException {
  BinaryManagerRootDescriptor desc;
  if (configFile.exists()) {
    XMap xmap = new XMap();
    xmap.register(BinaryManagerRootDescriptor.class);
    desc = (BinaryManagerRootDescriptor) xmap.load(new FileInputStream(configFile));
  } else {
    desc = new BinaryManagerRootDescriptor();
    // TODO fetch from repo descriptor
    desc.digest = getDefaultDigestAlgorithm();
    desc.depth = DEFAULT_DEPTH;
    desc.write(configFile); // may throw IOException
  }
  return desc;
}

代码示例来源:origin: org.nuxeo.runtime/nuxeo-connect-standalone

public static XMap createXmap() {
  @SuppressWarnings("hiding")
  XMap xmap = new XMap();
  xmap.setValueFactory(PackageType.class, new XValueFactory() {
    @Override
    public String serialize(Context arg0, Object arg1) {
  xmap.setValueFactory(Version.class, new XValueFactory() {
    @Override
    public String serialize(Context arg0, Object arg1) {
  xmap.setValueFactory(PackageDependency.class, new XValueFactory() {
  xmap.register(PackageDefinitionImpl.class);
  xmap.register(FormsDefinition.class);
  return xmap;

代码示例来源:origin: org.nuxeo.common/nuxeo-common

/**
 * Processes the XML file at the given URL and using the given contexts.
 *
 * @param ctx the context to use
 * @param url the XML file url
 * @return the first registered top level object that is found in the file.
 */
public Object load(Context ctx, URL url) throws IOException {
  return load(ctx, url.openStream());
}

代码示例来源:origin: org.nuxeo.common/nuxeo-common

public XAnnotatedContent(XMap xmap, XAccessor setter, XContent anno) {
  super(xmap, setter);
  path = new Path(anno.value());
  type = setter.getType();
  valueFactory = xmap.getValueFactory(type);
  xao = xmap.register(type);
}

代码示例来源:origin: org.nuxeo.common/nuxeo-common

public XAnnotatedMember(XMap xmap, XAccessor setter, XNode anno) {
  this.xmap = xmap;
  accessor = setter;
  path = new Path(anno.value());
  trim = anno.trim();
  type = setter.getType();
  valueFactory = xmap.getValueFactory(type);
  if (valueFactory == null && type.isEnum()) {
    valueFactory = new XValueFactory() {
      @Override
      public String serialize(Context arg0, Object arg1) {
        return ((Enum<?>) arg1).name();
      }
      @SuppressWarnings("unchecked")
      @Override
      public Object deserialize(Context arg0, String arg1) {
        return Enum.valueOf(type, arg1);
      }
    };
    xmap.setValueFactory(type, valueFactory);
  }
  xao = xmap.register(type);
}

代码示例来源:origin: org.nuxeo.common/nuxeo-common

/**
 * Processes the XML content from the given input stream using the given context.
 *
 * @param ctx the context to use
 * @param in the input stream
 * @return the first registered top level object that is found in the file.
 */
public Object load(Context ctx, InputStream in) throws IOException {
  try {
    DocumentBuilderFactory factory = getFactory();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(in);
    return load(ctx, document.getDocumentElement());
  } catch (ParserConfigurationException | SAXException e) {
    throw new IOException(e);
  } finally {
    if (in != null) {
      try {
        in.close();
      } catch (IOException e) {
        // do nothing
      }
    }
  }
}

代码示例来源:origin: org.nuxeo.common/nuxeo-common

private void scanClass(XAnnotatedObject xob, Class<?> aClass) {
  Field[] fields = aClass.getDeclaredFields();
  for (Field field : fields) {
    Annotation anno = checkMemberAnnotation(field);
    if (anno != null) {
      XAnnotatedMember member = createFieldMember(field, anno);
      xob.addMember(member);
    }
  }
  Method[] methods = aClass.getDeclaredMethods();
  for (Method method : methods) {
    // we accept only methods with one parameter
    Class<?>[] paramTypes = method.getParameterTypes();
    if (paramTypes.length != 1) {
      continue;
    }
    Annotation anno = checkMemberAnnotation(method);
    if (anno != null) {
      XAnnotatedMember member = createMethodMember(method, anno, aClass);
      xob.addMember(member);
    }
  }
  // scan superClass annotations
  if (aClass.getSuperclass() != null) {
    scanClass(xob, aClass.getSuperclass());
  }
}

代码示例来源:origin: org.nuxeo.common/nuxeo-common

/**
 * Registers a mappable object class.
 * <p>
 * The class will be scanned for XMap annotations and a mapping description is created.
 *
 * @param klass the object class
 * @return the mapping description
 */
public XAnnotatedObject register(Class<?> klass) {
  XAnnotatedObject xao = objects.get(klass);
  if (xao == null) { // avoid scanning twice
    XObject xob = checkObjectAnnotation(klass);
    if (xob != null) {
      xao = new XAnnotatedObject(this, klass, xob);
      objects.put(xao.klass, xao);
      scan(xao);
      String key = xob.value();
      if (key.length() > 0) {
        roots.put(xao.path.path, xao);
      }
    }
  }
  return xao;
}

代码示例来源:origin: org.nuxeo.common/nuxeo-common

/**
 * Processes the XML file at the given URL using a default context.
 *
 * @param url the XML file url
 * @return the first registered top level object that is found in the file, or null if no objects are found.
 */
public Object load(URL url) throws IOException {
  return load(new Context(), url.openStream());
}

代码示例来源:origin: org.nuxeo.common/nuxeo-common

public XAnnotatedList(XMap xmap, XAccessor setter, XNodeList anno) {
  super(xmap, setter);
  path = new Path(anno.value());
  trim = anno.trim();
  type = anno.type();
  componentType = anno.componentType();
  valueFactory = xmap.getValueFactory(componentType);
  xao = xmap.register(componentType);
  isNullByDefault = anno.nullByDefault();
}

代码示例来源:origin: org.nuxeo.ecm.core/nuxeo-core-persistence

public static HibernateConfiguration load(URL location) {
  XMap map = new XMap();
  map.register(HibernateConfiguration.class);
  try {
    return (HibernateConfiguration) map.load(location);
  } catch (IOException e) {
    throw new PersistenceError("Cannot load hibernate configuration from " + location, e);
  }
}

代码示例来源:origin: org.nuxeo.common/nuxeo-common

/**
 * Processes the XML content from the given input stream using a default context.
 *
 * @param in the XML input source
 * @return the first registered top level object that is found in the file.
 */
public Object load(InputStream in) throws IOException {
  return load(new Context(), in);
}

代码示例来源:origin: org.nuxeo.common/nuxeo-common

public XAnnotatedMap(XMap xmap, XAccessor setter, XNodeMap anno) {
  super(xmap, setter);
  path = new Path(anno.value());
  trim = anno.trim();
  key = new Path(anno.key());
  type = anno.type();
  componentType = anno.componentType();
  valueFactory = xmap.getValueFactory(componentType);
  xao = xmap.register(componentType);
  isNullByDefault = anno.nullByDefault();
}

代码示例来源:origin: org.nuxeo.ecm.webengine/nuxeo-webengine-core

public static ModuleConfiguration readConfiguration(final WebEngine engine, File file) throws IOException {
  XMap xmap = new XMap();
  xmap.register(ModuleConfiguration.class);
  InputStream in = new BufferedInputStream(new FileInputStream(file));
  ModuleConfiguration mc = (ModuleConfiguration) xmap.load(createXMapContext(engine), in);
  return mc;
}

代码示例来源:origin: org.nuxeo.common/nuxeo-common

/**
 * Processes the given DOM element and return the first mappable object found in the element.
 * <p>
 * A default context is used.
 *
 * @param root the element to process
 * @return the first object found in this element or null if none
 */
public Object load(Element root) {
  return load(new Context(), root);
}

代码示例来源:origin: org.nuxeo.ecm.platform/nuxeo-platform-forms-layout-core

@XContent("selectOptions")
public void setSelectOptions(DocumentFragment selectOptionsDOM) {
  XMap xmap = new XMap();
  xmap.register(WidgetSelectOptionDescriptor.class);
  xmap.register(WidgetSelectOptionsDescriptor.class);
  Node p = selectOptionsDOM.getFirstChild();
  List<WidgetSelectOption> options = new ArrayList<WidgetSelectOption>();
  while (p != null) {
    if (p.getNodeType() == Node.ELEMENT_NODE) {
      Object desc = xmap.load((Element) p);
      if (desc instanceof WidgetSelectOptionDescriptor) {
        options.add(((WidgetSelectOptionDescriptor) desc).getWidgetSelectOption());
      } else if (desc instanceof WidgetSelectOptionsDescriptor) {
        options.add(((WidgetSelectOptionsDescriptor) desc).getWidgetSelectOption());
      } else {
        log.error("Unknown resolution of select option");
      }
    }
    p = p.getNextSibling();
  }
  selectOptions = options.toArray(new WidgetSelectOption[0]);
}

代码示例来源:origin: org.nuxeo.runtime/nuxeo-connect-standalone

@Override
public PackageDefinition loadPackage(InputStream in) throws PackageException {
  try {
    return (PackageDefinition) xmap.load(in);
  } catch (IOException e) {
    throw new PackageException("Failed to parse XML package definition", e);
  }
}

代码示例来源:origin: org.nuxeo.common/nuxeo-common

/**
 * Processes the given DOM element and return the first mappable object found in the element.
 * <p>
 * The given context is used.
 *
 * @param ctx the context to use
 * @param root the element to process
 * @return the first object found in this element or null if none
 */
public Object load(Context ctx, Element root) {
  // check if the current element is bound to an annotated object
  String name = root.getNodeName();
  XAnnotatedObject xob = roots.get(name);
  if (xob != null) {
    return xob.newInstance(ctx, root);
  } else {
    Node p = root.getFirstChild();
    while (p != null) {
      if (p.getNodeType() == Node.ELEMENT_NODE) {
        // Recurse in the first child Element
        return load((Element) p);
      }
      p = p.getNextSibling();
    }
    // We didn't find any Element
    return null;
  }
}

代码示例来源:origin: org.nuxeo.runtime/nuxeo-connect-standalone

public Form[] getForms(String path) throws PackageException {
  File file = data.getEntry(path);
  if (file.isFile()) {
    try (FileInputStream in = new FileInputStream(file)) {
      FormsDefinition forms = (FormsDefinition) StandaloneUpdateService.getXmap().load(in);
      return forms.getForms();
    } catch (IOException e) {
      throw new PackageException("Failed to load forms file: " + file);
    }
  }
  return null;
}

代码示例来源:origin: org.nuxeo.runtime/nuxeo-connect-standalone

/**
 * @since 5.8
 */
public LocalPackageImpl(ClassLoader parent, File file, PackageState state, PackageUpdateService pus)
    throws PackageException {
  this.state = state;
  service = pus;
  XMap xmap = StandaloneUpdateService.getXmap();
  if (xmap == null) { // for tests
    xmap = StandaloneUpdateService.createXmap();
  }
  try {
    data = new LocalPackageData(parent, file);
    InputStream in = new FileInputStream(data.getManifest());
    def = (PackageDefinitionImpl) xmap.load(in);
  } catch (FileNotFoundException e) {
    throw new PackageException("Invalid package - no package.xml file found in package " + file.getName());
  } catch (IOException e) {
    throw new PackageException("Failed to load package.xml descriptor for package " + file.getName(), e);
  }
  id = def.getId();
}

相关文章